Results 1 to 11 of 11

Thread: QPainter ouside of paintEvent with a pixmap

Hybrid View

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

    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 

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

    Default Re: QPainter ouside of paintEvent with a pixmap

    How do you create that pmap pixmap?

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

    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.

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

    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 

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

    bitChanger (22nd March 2006)

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

    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, 16:51
  2. Replies: 12
    Last Post: 5th February 2006, 10: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
  •  
Qt is a trademark of The Qt Company.