Custom paint events make me smile

When creating our own QtRuby GUI widget, we can override the paintEvent method to make it look however we like:

require 'Qt'

class Smiley < Qt::Widget
  def paintEvent(e)
    p = Qt::Painter.new(self)
    p.setPen(Qt::blue)

    # Face
    p.drawEllipse(0,0, width-2, height-2)

    # Eyes
    p.drawEllipse(width / 3, height / 4,10,10)
    p.drawEllipse(width / 3 * 2, height / 4,10,10)

    # Nose
    p.drawEllipse(width / 2, height / 5 * 2, 10, 10)

    # Mouth
    p.drawArc(width/4, height / 3 * 2, width / 2, 50, -2880, 2880)

    p.dispose
    end
end

app = Qt::Application.new(ARGV)
s = Smiley.new(nil)
app.setMainWidget(s)
s.show
app.exec

gives us:

3 Responses to “Custom paint events make me smile”

  1. Phlip Says:

    you got a test for that (-:

  2. Steve Says:

    Could you give us some more examples like line thickness and filling ellipse’s with a solid color?

    :)

  3. somekool Says:

    check http://doc.trolltech.com/ you’ll find it very easy to fill it with a solid color or do whatever you like….