A question about paintEvent function



  • I know Qt draw the window in the paintEvent function. I want to draw a line out of the paintEvent, how to do that?
    I want to draw a line in a function,like:
    @
    void MyClass::someFunction()
    {
    drawLine(...);
    }
    @

    not in the paintEvent, how to do ?

    Edit: fixed up question, spelling and formatting a bit. Please use @ tags around code, and "pay some attention to your language":http://www.catb.org/~esr/faqs/smart-questions.html#writewell. It really is important; Andre



  • Have a look at QPainter documentation.

    inside paint event, it looks something like this:

    @
    void MyClass::paintEvent(QPaintEvent* event)
    {
    QPainter paint(this);
    paint.drawLine(pt1, pt2);
    }
    @



  • [quote author="udrnrso" date="1302160348"]i know -QT- Qt draw the window in the paintEvent function.i want to draw a line out of the paintEvent,how to do that?[/quote]

    Very simple: you don't.

    Well, that's a little bit to extreme. An approach could be to do your drawing on a QPixmap, and use a QLabel to display that pixmap. That would allow you to draw the pixmap at your own time. However, you will need to re-set the pixmap on the label each time you change it. Otherwise it won't update on screen.

    Another approach would be to look into QGraphicsView instead. It allows you to draw lines by just putting a QGraphicsLineItem on the scene.



  • bq. Andre wrote Very simple: you don’t.

    why? Sometimes a custom drawn widget is needed, and this is just to start on how to do it? It's possible and easy.



  • [quote author="Gerolf" date="1302163343"]bq. Andre wrote Very simple: you don’t.

    why? Sometimes a custom drawn widget is needed, and this is just to start on how to do it? It's possible and easy.[/quote]

    Let me repeat the original question:

    [quote author="udrnrso" date="1302160348"]I know Qt draw the window in the paintEvent function. I want to draw a line out of the paintEvent, how to do that?
    i want to draw a line in a function,like:
    @
    void MyClass::someFunction()
    {
    drawLine(...);
    }
    @
    not in the paintEvent, how to do ?[/quote]

    I have made some minor edits and added some highlights to make the question clearer. He wants to draw outside the paintEvent function on a widget. In your answer, you tell him to... use the paintEvent.



  • If you are neither on Windows nor on Mac OS X, you can try Qt::WA_PaintOutsidePaintEvent

    bq. From "QWidget":http://doc.qt.nokia.com/4.7/qwidget.html docs:
    Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

    bq. From "Qt namespace":http://doc.qt.nokia.com/4.7/qt.html docs:
    Qt::WA_PaintOutsidePaintEvent - Makes it possible to use QPainter to paint on the widget outside paintEvent(). This flag is not supported on Windows, Mac OS X or Embedded Linux. We recommend that you use it only when porting Qt 3 code to Qt 4.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.