Results 1 to 6 of 6

Thread: paintEvent problem

  1. #1
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question paintEvent problem

    look at this code, it only draw ONE ellipse.
    I want to draw TWO ellipses.
    What is wrong here??

    shape.cpp
    Qt Code:
    1. void shape::paintEvent( QPaintEvent * )
    2. {
    3. QPainter painter( this );
    4.  
    5. QRectF rectangle1(10.0, 20.0, 80.0, 60.0);
    6. painter.drawEllipse(rectangle1);
    7.  
    8. QRectF rectangle2(100.0, 200.0, 80.0, 60.0);
    9. painter.drawEllipse(rectangle2);
    10.  
    11. }
    12.  
    13. void shape::drawMyCircle()
    14. {
    15.  
    16. QPainter painter( this );
    17. QRectF rectangle(150.0, 250.0, 80.0, 60.0);
    18. painter.drawEllipse(rectangle);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "shape.h"
    2. #include <QApplication>
    3.  
    4.  
    5. int main( int argc, char **argv )
    6. {
    7.  
    8. QApplication a ( argc, argv );
    9.  
    10. shape myShape;
    11. myShape.resize(640, 480);
    12. myShape.show();
    13. myShape.drawMyCircle ();
    14. myShape.show(); //<--- why dont refresh?
    15.  
    16. return a.exec ();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent problem

    What is wrong here??
    Almost everything!

    You can't paint on a widget from outside its paint event. So the method drawMyCircle doesn't do do anything.

    Remove the call to drawMyCircle from main.cpp and also remove the second call to show.
    You will then see two ellipses(the ones from paintEvent), provided that you don't draw off-widget.

  3. #3
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent problem

    thnx!!

    so if only the widget :: painEvent() can effective draw, how can i encapsulate the draw definition of any entity that is not a widget?? for example "myShape()"...

    each entitie must to be a widget??

    i desire each entity draw itself....

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent problem

    In that case maybe QGraphicsView Framework is more suitable for you.
    You can create custom QGraphicsItem's to draw your custom shapes, although basic shapes as rectangle and ellipse are already covered by the framework.

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

    MoaLaiSkirulais (12th February 2008)

  6. #5
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paintEvent problem

    thnx...

    btw suppose i still want to use QPainter's and QWidgets schema....

    is it possible from QWidget :: paintEvent() to trigger using SIGNALs and SLOTs each myCustomPaintEvent of each entity?? ex. myShape :: myCustomPaintEvent() ???


  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent problem

    You can create one widget subclass for each shape you want to draw.
    This is the easiest way if you want to use widgets for this.

    Or you can create a single QWidget subclass with methods that draw all the shapes you need, taking as parameter a QPainter pointer. Also you have to add an enum specifying the shape type.
    In the paint event of this widget you can test the shape type and call the right method.

Similar Threads

  1. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  2. Replies: 3
    Last Post: 27th November 2006, 09:56
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Problem with QScrollArea updating from 4.0.1 to 4.1.0
    By SkripT in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2006, 22:35

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.