Results 1 to 12 of 12

Thread: paintEvent help

  1. #1
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default paintEvent help

    I want to draw an ellipse when i click to pushbutton.

    i write this signal-slots:
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(paintEvent()));


    then as paintevent:
    void MainWindow:aintEvent(QPaintEvent *pe)
    {
    QPainter painter(this);
    painter.drawEllipse(20,20,100,100);
    }


    when i run program. it builds successfully.
    but it draws ellipse automatically. program doesn't need to click pushbutton.
    and gives that message "QPainter::begin: Paint device returned engine == 0, type: 1"


    what should i do to perform painting when i click push button.
    thank you very much

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: paintEvent help

    paintEvent() gets called automatically by the underlying Qt framework for all sorts of reasons. If you only want to paint something when a specific action takes place, move it to another routine, do your painting just as you've done, and call update() when you're finished. That way, the routine will only be invoked when whatever actions you connect to it are triggered.

  3. #3
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent help

    ok i made code like this.
    i can draw an ellipse by clicking push button.
    but when i click button it draws the last ellipse.
    i want to draw them one on the top of the other.
    how can i do this?
    thank you very much.

    Qt Code:
    1. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(Drawit()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::Drawit()
    2. {
    3. drawEllipse=1;
    4. positionx=positionx+1;
    5. positiony=positiony+1;
    6. update();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *pe)
    2. {
    3. if( drawEllipse )
    4. {
    5. QPainter painter( this );
    6. painter.drawEllipse(positionx,positiony,100,100);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: paintEvent help

    You have to append the positions to a member variable. Then in the event use the informations of that member variable and draw your ellipses. Or you can use Qt::WA_OpaquePaintEvent to "save" the previous drawn ellipses.

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

    seltra (18th September 2010)

  6. #5
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent help

    Quote Originally Posted by Lykurg View Post
    You have to append the positions to a member variable. Then in the event use the informations of that member variable and draw your ellipses. Or you can use Qt::WA_OpaquePaintEvent to "save" the previous drawn ellipses.
    dear lykurg, thank you for your help.
    "positionx" and "positiony" are my member variables, i declare them as:

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. ...
    4. int positionx;
    5. int positiony;
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    how can i append the member variables?
    should i define them as "vector"s and "push back"?
    thank you

  7. #6
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent help

    i don't know if it is an efficient solution, i have found this solution.
    what do you suggest?
    thank you.




    Qt Code:
    1. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(Drawit()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::Drawit()
    2. {
    3. drawEllipse=1;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *pe)
    2. {
    3. if( drawEllipse )
    4. {
    5. QPainter painter( this );
    6. for (int i=1; i<50; i=i+6)
    7. {
    8. positionx=i;
    9. positiony=i;
    10. update();
    11. painter.drawEllipse(positionx,positiony,80,80);
    12. }
    13.  
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  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: paintEvent help

    You can use whatever you like. I'd go probably for a QList:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. ...
    4. QList<int> positionx;
    5. QList<int> positiony;
    6. ...
    7. }
    8.  
    9. void MainWindow::Drawit()
    10. {
    11. drawEllipse=1;
    12. // check if you list is not empty
    13. positionx.append(positionx.last()+1);
    14. positiony.append(positiony.last()+1);
    15. update();
    16. }
    To copy to clipboard, switch view to plain text mode 

    And then loop the list in you paint event.

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

    seltra (18th September 2010)

  10. #8
    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: paintEvent help

    Quote Originally Posted by seltra View Post
    i don't know if it is an efficient solution, i have found this solution.
    No, do not call update() in a paint event!

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

    seltra (18th September 2010)

  12. #9
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent help

    Quote Originally Posted by Lykurg View Post
    No, do not call update() in a paint event!
    When i remove update() from the code below, it doesn't draw multiple ellipses.
    makes a weird behaviour.

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *pe)
    2. {
    3. if( drawEllipse )
    4. {
    5. QPainter painter( this );
    6. for (int i=1; i<50; i=i+6)
    7. {
    8. positionx=i;
    9. positiony=i;
    10. update();
    11. painter.drawEllipse(positionx,positiony,80,80);
    12. }
    13.  
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  13. #10
    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: paintEvent help

    Quote Originally Posted by seltra View Post
    makes a weird behaviour.
    Please post a screen or describe it.

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

    seltra (18th September 2010)

  15. #11
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent help

    Quote Originally Posted by Lykurg View Post
    Please post a screen or describe it.
    when i start program and i don't press to "draw it" push button.


    when i use update() in paintEvent method, and click to pushbutton:


    when i don't use update() in paintEvent method, and click to pushbutton:

  16. #12
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: paintEvent help

    I suggest you create a new widget to do they painting on.
    Place everything nicely in layouts.

    This will prevent the weird behavior you see now.

  17. The following user says thank you to tbscope for this useful post:

    seltra (18th September 2010)

Similar Threads

  1. QAbstractScrollArea and paintEvent
    By babu198649 in forum Newbie
    Replies: 13
    Last Post: 4th August 2008, 10:56
  2. paintEvent problem
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 29th June 2008, 14:18
  3. PaintEvent warning
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2008, 15:37
  4. Paintevent and update
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2008, 07:09
  5. Replies: 12
    Last Post: 5th February 2006, 10:34

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.