Results 1 to 7 of 7

Thread: Help regarding QPainter issue on QMainWindow

  1. #1
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question Help regarding QPainter issue on QMainWindow

    Hai friends,
    I am new to qt4 and i have a problem in using QPainter to draw lines when mouse is moving by reimplementing mouseMoveEvent.
    Problem is that the old drawings are erased after paintEvent.
    So only the last drawing is retained on screen after paintEvent. Is there any way to save the old drawings in Qt4 ? Please help?

    code is as follows :-

    void MainWindow:: paintEvent(QPaintEvent* event)
    {
    QPainter p;
    p.begin(this);
    p.drawLine(oldX, oldY, X, Y); // where oldX and oldY are older position of mouse before moving it and X and Y are new positions of mouse.
    p.end();
    }

    void MainWindow::mouseMoveEvent(QMouseEvent* e)
    {
    oldX = X;
    oldY = Y;
    X=e->x();
    Y=e-Y();
    repaint(); // To repaint after mouseMoveEvent()
    }

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help regarding QPainter issue on QMainWindow

    Put all the coordinates in a QVector<QLineF> and use QPainter::drawLines ( const QLineF * lines, int lineCount ) to draw all the lines.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Help regarding QPainter issue on QMainWindow

    Storing all points can be damn huge. The magic is window attribute Qt::WA_StaticContents! See the Scribble Example which comes with Qt.

  4. The following user says thank you to Lykurg for this useful post:

    toutarrive (25th March 2010)

  5. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help regarding QPainter issue on QMainWindow

    How the magic works? Does it mean that the painting is cached somehow and that the paint event occurs only for new parts?

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Help regarding QPainter issue on QMainWindow

    Hey, I was to fast by writing that reply. It's NoSystemBackground which is responsible that previous things are not cleaned. See this small example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class test : public QWidget
    4. {
    5. public:
    6. test(QWidget *parent = 0) : QWidget(parent)
    7. {
    8. setAttribute(Qt::WA_NoSystemBackground, true);
    9. setAttribute(Qt::WA_StaticContents, true);
    10. first = true;
    11. QTimer *timer = new QTimer(this);
    12. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    13. timer->start(500);
    14. }
    15. void paintEvent(QPaintEvent */*event*/)
    16. {
    17. QPainter painter(this);
    18. if (first)
    19. {
    20. painter.setBrush(Qt::yellow);
    21. painter.drawRect(this->rect());
    22. first = false;
    23. }
    24. else
    25. {
    26. painter.setPen(Qt::red);
    27. QPoint p(qrand() % this->width(), qrand() % this->height());
    28. painter.drawRect(QRect(p.x(), p.y(), 10, 10));
    29. }
    30. }
    31.  
    32. bool first;
    33. };
    34.  
    35.  
    36.  
    37. int main(int argc, char **argv)
    38. {
    39. QApplication app(argc, argv);
    40. test t;
    41. t.show();
    42. return app.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 

    (Resizing is not working. For that you have to alter the paint method and react on the resize event.)

  7. #6
    Join Date
    Nov 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help regarding QPainter issue on QMainWindow

    This is great! Since I also got this kind of problem, the reply from Qt developer is very meaningful and it helps me alot either. thank

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Help regarding QPainter issue on QMainWindow

    You are welcome!

Similar Threads

  1. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  2. QPainter problem (a HW issue perhaps?)
    By jonks in forum Qt Programming
    Replies: 10
    Last Post: 10th June 2009, 03:05
  3. Replies: 1
    Last Post: 28th October 2008, 16:29
  4. QMainWindow Maximization Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 15th March 2007, 13:30
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

Tags for this Thread

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.