Results 1 to 8 of 8

Thread: QPainter problem - migration from qt3 [SOLVED]

  1. #1
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainter problem - migration from qt3 [SOLVED]

    Hi,
    I had the following code in qt3 and it worked perfectly (I removed resize and refresh procedures)
    whiteboard.h
    Qt Code:
    1. class Whiteboard : public QFrame
    2. {
    3. Q_OBJECT
    4. public:
    5. QPoint qt_currMosePos;
    6. QPoint qt_prevMousePos;
    7. QPen *pen;
    8. QPainter * paint;
    9.  
    10. Whiteboard( QWidget *parent=0, const char* name = 0);
    11. ~Whiteboard()
    12.  
    13. protected:
    14. void mouseMoveEvent(QMouseEvent *);
    15. void mousePressEvent(QMouseEvent *);
    16. };
    To copy to clipboard, switch view to plain text mode 

    whiteboard.cpp
    Qt Code:
    1. #include "whiteboard.h"
    2.  
    3. Whiteboard::Whiteboard( QWidget *parent, const char* name) : QFrame(parent,name)
    4. {
    5. paint = new QPainter(this);
    6. pen = new QPen(Qt::red,2);
    7. paint->setPen(*pen);
    8. this->setBackgroundMode(Qt::NoBackground);
    9. }
    10. Whiteboard::~Whiteboard()
    11. {
    12. delete paint;
    13. }
    14.  
    15. void Whiteboard::mouseMoveEvent(QMouseEvent *e)
    16. {
    17. qt_currMousePos = e->pos();
    18. paint->drawLine(qt_prevMousePos,qt_currMosePos);
    19. qt_prevMousePos = qt_currMousePos;
    20. }
    21.  
    22. void Whiteboard::mousePressEvent(QMouseEvent *e)
    23. {
    24. if(e->button() == Qt::LeftButton) {
    25. qt_prevMousePos = e->pos();
    26. paint->drawLine(qt_prevMousePos,qt_prevMousePos);
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    Basically it allows you to plot on the widget with your mouse.
    I compiled it under Solaris and Linux.

    Now I migrated to qt4. I ran qt3to4 and recompiled it. No problems.
    But when I launch the program I cannot paint and I have the following message in the console:
    QPainter::begin: Widget painting can only begin as a result of a paintEvent

    What has happend? Are there some new limitations in qt4?
    I know that migration from qt3 has been discussed since long time ago but I cannot find any mention of this problem.
    Thanks for help

    Tomek
    Last edited by tpomorsk; 28th August 2006 at 14:37.

  2. #2
    Join Date
    Aug 2006
    Posts
    77
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter problem - migration from qt3

    Try to use Q3Painter. Maybe this will help.

  3. #3
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter problem - migration from qt3

    Thanks for reply.
    It didn't help.
    Even if I use Q3Frame and Q3Painter I still have the same message in the conosle and still I cannot paint.

    Tomek

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter problem - migration from qt3

    What has happend? Are there some new limitations in qt4?
    Yes.
    Well, not really limitations, but new ways to do things (and new structure)
    And this:
    Widget painting can only begin as a result of a paintEvent
    being one of them.
    read all about it here:
    http://doc.trolltech.com/4.0/qt4-arthur.html

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

    tpomorsk (28th August 2006)

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPainter problem - migration from qt3

    Painting using a QPainter can only take place in a paintEvent() or a function called by a paintEvent().

    Something like this should be more appropriate for Qt4 (don't expect it to compile nor fully work, it's written on the fly):
    Qt Code:
    1. Whiteboard::Whiteboard( QWidget *parent) : QFrame(parent)
    2. {
    3. setBackgroundMode(Qt::NoBackground);
    4. }
    5.  
    6. void Whiteboard::mouseMoveEvent(QMouseEvent *e)
    7. {
    8. qt_currMousePos = e->pos();
    9. update(); // schedule a paint event
    10. }
    11.  
    12. void Whiteboard::mousePressEvent(QMouseEvent *e)
    13. {
    14. if(e->button() == Qt::LeftButton) {
    15. qt_currMousePos = e->pos();
    16. qt_prevMousePos = qt_currMousePos;
    17. update(); // schedule a paint event
    18. }
    19. }
    20.  
    21. void Whiteboard::paintEvent(QPaintEvent *e)
    22. {
    23. QPainter painter(this);
    24. painter.setPen(QPen(Qt::red, 2));
    25. painter.drawLine(qt_prevMousePos, qt_currMosePos);
    26. qt_prevMousePos = qt_currMousePos;
    27. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    tpomorsk (28th August 2006)

  8. #6
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter problem - migration from qt3

    High_flyer,
    Thanks for the link. I read it and now I understand the reason for this change.

    Jpn,
    Thanks for the solution. I tried it but it's too slow. The line which is drawn is not smooth enough. It's much much worse than with qt3 where QPainter could be initialized only once (in a constructor of a widget). I think that the function begin() of QPainter takes too much time.
    I tried to initialize QPainter in the constructor and run only begin() in paintEvent but it's still very slow.

    Qt Code:
    1. Whiteboard::Whiteboard( QWidget *parent) : QFrame(parent)
    2. {
    3. setBackgroundMode(Qt::NoBackground);
    4. paint = new QPainter();
    5. }
    6.  
    7. ...
    8.  
    9. void Whiteboard::paintEvent(QPaintEvent *e)
    10. {
    11. paint->begin(this);
    12. paint->setPen(QPen(Qt::red, 2));
    13. paint->drawLine(qt_prevMousePos, qt_currMosePos);
    14. qt_prevMousePos = qt_currMousePos;
    15. paint->end();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Is there another way to optimize it or do I have to play with QPaintEngine?

    Tomek

  9. #7
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: QPainter problem - migration from qt3

    Problem solved.
    In fact paintEvent() was slow because of double-buffering, which is turned on by default in Qt4. All I had to do is to turn it off.
    I used JPN's code and added the following line in the constructor of my main class (whiteboard)
    Qt Code:
    1. this->setAttribute(Qt::WA_PaintOnScreen);
    To copy to clipboard, switch view to plain text mode 
    After that there is no difference in performance between qt4 and qt3.


    Thanks for help,
    Tomek

  10. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter problem - migration from qt3 [SOLVED]

    In fact paintEvent() was slow because of double-buffering, which is turned on by default in Qt4.
    Thanks for pointing that out.
    Another handy point to check in the future, if painting is slow.

Similar Threads

  1. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  2. Replies: 7
    Last Post: 20th March 2006, 20:03
  3. Replies: 16
    Last Post: 7th March 2006, 15:57
  4. QPainter rotate function
    By ir210 in forum Newbie
    Replies: 3
    Last Post: 17th January 2006, 04:31
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

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.