Results 1 to 11 of 11

Thread: QPainter ouside of paintEvent with a pixmap

  1. #1
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QPainter ouside of paintEvent with a pixmap

    I'm porting a Qt 3.3.2 application to Qt 4.1.1

    In Qt 3 you can create a QPainter p(&pixmap) and paint to the pixmap anywhere in the code.

    In Qt 4 it says that all painting must take place inside the paintEvent.

    Why is that the following will work in Qt 4 outside of paintEvent

    Qt Code:
    1. void someFunc()
    2. {
    3. Pixmap pix(size());
    4. pix.fill(this, 0, 0);
    5. QPainter p(&pix); // no error
    6. // paint away
    7. }
    To copy to clipboard, switch view to plain text mode 

    but, the following will not.

    Qt Code:
    1. // here ptrPix was filled by another function
    2. void someFunc()
    3. {
    4. QPainter p(ptrPix); // QPainter::begin(), paintdevice returned engine == 0, type: 2
    5. // paint away
    6. }
    To copy to clipboard, switch view to plain text mode 

    am i missing a concept here?

    I will move all of my painting to the paintEvent so that I can take advantage of the double buffering built in to Qt 4, I just want to know what is going on with the above statements.

    Thanks.
    Last edited by bitChanger; 21st March 2006 at 21:46.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    From Qt docs:
    Unless a widget has the Qt::WA_PaintOutsidePaintEvent attribute set. A QPainter can only be used on a widget inside a paintEvent() or a function called by a paintEvent().
    This is about painting on a widget, not on a pixmap.

  3. #3
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    The first example creates a QPixmap of the widgets size and fills it with the widgets background color or pixmap. Then you can paint on the pixmap with that QPainter.

    The second one assigns a prefilled pixmap pointer to a QPainter and then paints on the pixmap.

    They seem to be doing the same thing, which neither is painting to a widget.

    So, I'm still confused why one would work and one would not.

    Please explain more if I'm missing a point.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    Do you use threads?

  5. #5
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    Yes 5 of them in this application.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    Do you modify QPixmaps in non-GUI thread? If yes, then you will have to switch to QImages.

  7. #7
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    After trying some more things I'm confused as to why the following don't work in Qt 4.1.1
    This is an application not using threads.

    Qt Code:
    1. void MyType::paintEvent(QPaintEvent*)
    2. {
    3. QPainter painter;
    4. someFunc(&painter);
    5.  
    6. painter.begin(this);
    7. painter.drawPixmap(QPoint(0, 0), *pmap);
    8. painter.end();
    9. }
    10.  
    11. void MyType::someFunc(QPainter *painter)
    12. {
    13. painter->begin(pmap);
    14. painter->setPen(dark);
    15. painter->drawLine(0, 0, 50, 50);
    16. // .. do something useful
    17. painter->end();
    18. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    How do you create that pmap pixmap?

  9. #9
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    Qt Code:
    1. class MyType : public QWidget
    2. {
    3. public:
    4. // ... methods
    5.  
    6. private:
    7. QPixmap* pmap;
    8. QImage* image;
    9. QColor* backgroundColor;
    10. };
    11.  
    12. void MyType::createImage()
    13. {
    14. image = new QImage(50, 50, QImage::Format_RGB32);
    15. image->fill(backgroundColor->rgb());
    16.  
    17. for (int i=0; i<50; i++)
    18. {
    19. QRgb rgb = qRgb(i, i+25, i+50);
    20. for (int x=0; x<50; ++x)
    21. image->setPixel(x, i, rgb);
    22. }
    23.  
    24. convertImage();
    25. }
    26.  
    27. void MyType::convertImage()
    28. {
    29. pmap = new QPixmap();
    30. pmap->fromImage(*image, Qt::PreferDither);
    31. update();
    32. }
    To copy to clipboard, switch view to plain text mode 

    Hope this is enough sudo code for you.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    QPixmap::fromImage() is static.

    It should be something like:
    Qt Code:
    1. void MyType::convertImage()
    2. {
    3. pmap = new QPixmap( QPixmap::fromImage(*image, Qt::PreferDither) );
    4. update();
    5. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to jacek for this useful post:

    bitChanger (22nd March 2006)

  12. #11
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter ouside of paintEvent with a pixmap

    Good catch, now it makes sense. Since static member functions have no "this" pointer my usage was wrong. That fixed it.

    Thanks again.

Similar Threads

  1. QPainter reuse within a paintEvent
    By Micawber in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2008, 17:51
  2. Replies: 12
    Last Post: 5th February 2006, 11:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.