Results 1 to 20 of 23

Thread: Inheritance and QpaintEvent

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    6

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by jacek
    Why do you create new widgets if Son is a widget too? Wouldn't it be enough to redraw Son?
    But I don t want to create new ones ! Qt does ...
    The architecture is :
    I've got a class Bck1 and Bck2. In both classes I do some drawing with QPaintEvent (the 2 classes are Widgets).
    I ve got then the class Mother with his Son.
    And I want the Son to do some drawings in bot he Bck1 and Bck2.
    My problem is : Son is not a Widget but he must inherit from QWidget to know where he must paint ?



    Qt Code:
    1. #include <QWidget>
    2.  
    3. class Bck1: public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public :
    8. Bck1(QWidget *parent = 0);
    9. ~Bck2();
    10. ...
    11. protected :
    12. void paintEvent(QPaintEvent *event);
    13. void mousePressEvent(QMouseEvent *event);
    14. void keyPressEvent(QKeyEvent *event);
    15. ...}
    16.  
    17. #include <QWidget>
    18.  
    19. class Bck2: public QWidget
    20. {
    21. Q_OBJECT
    22.  
    23. public :
    24. Bck2(QWidget *parent = 0);
    25. ~Bck2();
    26.  
    27. protected :
    28. void paintEvent(QPaintEvent *event);
    29. }
    30.  
    31.  
    32. #include <QWidget>
    33.  
    34.  
    35. class Mother: public QWidget
    36. {
    37. Q_OBJECT
    38.  
    39. public :
    40. Mother(QWidget *parent=0);
    41. virtual ~Mother();
    42. protected :
    43. virtual void paintEvent(QPaintEvent *event)=0;
    44.  
    45.  
    46. };
    47.  
    48. #include <QWidget>
    49.  
    50. class Son : public Mother
    51. {
    52.  
    53. Q_OBJECT
    54.  
    55. public :
    56. Son(.....);//do I need to put Son(QWidget *parent=0) ... ?????
    57. ~Son();
    58.  
    59. protected :
    60. void paintEvent(QPaintEvent *event);
    61. }
    To copy to clipboard, switch view to plain text mode 

    Does it seems ok ? I never know if I need to specify *parent or *parent=0, and if I need to put Qwidget *parent in the Son constructor ???

  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: Inheritance and QpaintEvent

    Quote Originally Posted by djoul
    But I don t want to create new ones ! Qt does ...
    Qt does only what you tell it to do.

    Indeed, the problem is in constructor. If you create a widget without a parent, it will be created as a standalone window. So your constructor should be implemented like this:
    Qt Code:
    1. Son::Son( QWidget *parent )
    2. : Mother( parent )
    3. {
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    And the Mother class must pass this parent to QWidget's constructor.

    I never know if I need to specify *parent or *parent=0
    That "= 0" part only indicates a default value for the parameter.

  3. #3
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    6

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by jacek
    Qt does only what you tell it to do.

    Indeed, the problem is in constructor. If you create a widget without a parent, it will be created as a standalone window. So your constructor should be implemented like this:
    Qt Code:
    1. Son::Son( QWidget *parent )
    2. : Mother( parent )
    3. {
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    And the Mother class must pass this parent to QWidget's constructor.


    That "= 0" part only indicates a default value for the parameter.
    Ok now my paintEvent is called (the File is created).
    But if try to do some Basics drawings nothing appears on the screen.
    My Son is not a Widget, it just writes on the widget passed in the argument, so I don t need all the setVisible(true) or autofillBackgound stuff ...
    But I m happy the paintEvent is now called, it smells the end of the problem.
    Last edited by djoul; 5th July 2006 at 10:00.

  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: Inheritance and QpaintEvent

    Quote Originally Posted by djoul
    But if try to do some Basics drawings nothing appears on the screen.
    When do you draw on that widget? And how do you create QPainter?

    Quote Originally Posted by djoul
    My Son is not a Widget,
    It is a widget, since it inherits from Mother which is derived from QWidget.

  5. #5
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    6

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by jacek
    When do you draw on that widget? And how do you create QPainter?
    In my Son paintEvent method. I create it y doing QPainter painter(this) or QPAinter * painter = new QPainter(this).
    As my son inherit from Mother (that inherit from Qwidget) this is suppose to have a link with the widget I want to draw on ? I can t do painter(parent).


    Quote Originally Posted by jacek
    It is a widget, since it inherits from Mother which is derived from QWidget.
    Ok, in my vision Son is an Object that is not a widget because it's not a "window", it s just a class that can draw on widgets (so it must inherit from QWidget indirectly with Mother).

    That s really weird, in my Son paintEvent method I tried doing
    painter.fillRect(2,2,width()-4, height()-4, QBrush(QColor(0,0,0)));
    and in my Widget (Bck1) in top left corner a little rectangle, maybe 50*30 pixels has become black instead of red !!! and I never gave such dimensions for the widget

  6. #6
    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: Inheritance and QpaintEvent

    Quote Originally Posted by djoul
    Ok, in my vision Son is an Object that is not a widget because it's not a "window"
    If it's just "an object" then you shouldn't derive it from QWidget. Everything what inherits QWidget is a widget.

    In Qt4 you can draw on widgets only in their paintEvent() method. This means that you have to invoke Son::draw() from your widget's paintEvent().

    Quote Originally Posted by djoul
    That s really weird [...] has become black instead of red !!! and I never gave such dimensions for the widget
    No it's not, you just see the Son widget.

  7. #7
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    6

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by jacek
    If it's just "an object" then you shouldn't derive it from QWidget. Everything what inherits QWidget is a widget.

    In Qt4 you can draw on widgets only in their paintEvent() method. This means that you have to invoke Son::draw() from your widget's paintEvent().
    Ok, so here what I don t understand :
    In my Current class, where I do all the drawing in the Current:aintEvent.
    I create a Son(which is not a Widget), and I call Son->draw().
    I want this function for example to draw an ellipse on the Current (which is the widget).
    So in Son::draw I just do update(), which calls the Son:aintEvent which will draw the Ellipse.
    Then, in my Son:aintEvent what do I do ???? that s the point that I do not understand !!! I can t do QPainter painter(this)
    son.cpp(106): error C2664: 'QPainter::QPainter(QPaintDevice *)'*: impossible de convertir le paramètre 1 de Son*const ' en 'QPaintDevice *'

    That s why I was doing all the thing with passing the Widget to the Son so he knows where to draw !!!!!

  8. #8
    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: Inheritance and QpaintEvent

    Quote Originally Posted by djoul
    Ok, so here what I don t understand :
    In my Current class, where I do all the drawing in the Current::paintEvent.
    I create a Son(which is not a Widget), and I call Son->draw().
    So in this case you should have something like this:
    Qt Code:
    1. class Son
    2. {
    3. public:
    4. Son() { ... }
    5. void draw( QWidget *widget ) const
    6. {
    7. QPainter painter( widget ); // <--
    8. painter.drawEllipse( ... );
    9. ...
    10. }
    11. ...
    12. };
    13.  
    14. void Current::paintEvent( QPaintEvent * )
    15. {
    16. ...
    17. Son s;
    18. s.draw( this );
    19. ...
    20. }
    To copy to clipboard, switch view to plain text mode 

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

    djoul (5th July 2006)

  10. #9
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    6

    Default Re: Inheritance and QpaintEvent

    Jacek, A statue of you should be built for the help you gave me, it WORKS.
    THANK YOU,
    YYYYYYYYYYYHHHHHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

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.