Funky Graphics with QtRuby

As seen in the previous entry, you can override the paintEvent method for a custom widget if you want to control the drawing yourself. By request, here are a few more examples of things you can accomplish when doing those custom drawings:


require 'Qt'
class FunkyWidget < Qt::Widget
  def paintEvent(e)
    p = Qt::Painter.new(self)

    # Vertical blue line
    p.setPen( Qt::Pen.new(Qt::blue, 4) )
    p.drawLine(width / 5, 0, width / 5, height)

    # Reddish arch
    p.setPen( Qt::Pen.new( Qt::Color.new( 200, 24, 116) , 2) )
    p.drawArc( width / 5 * 2, height / 2, width / 2, height / 4, 1760, 1760 )

    # Light yellow ellipse with blue/green dotted border
    p.setPen( Qt::Pen.new( Qt::Color.new(100, 200, 150), 1, Qt::DotLine ) )
    p.setBrush( Qt::Brush.new( Qt::yellow, Qt::Dense6Pattern ) )
    p.drawEllipse( width / 5 * 4, height / 2, width / 5, height / 2)

    # Some text
    p.drawText( width / 5 * 2, height / 3, “Some Text”)
  end
end

app = Qt::Application.new(ARGV)
fw = FunkyWidget.new(nil)
app.setMainWidget(fw)
fw.show
app.exec

See this page for a more complete list of all of the Qt::Painter methods you can call.

Comments are closed.