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

    You mean I call virtual even my Son and Daughter methods ? I thought it was just for the Mother class !

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by djoul
    You mean I call virtual even my Son and Daughter methods ? I thought it was just for the Mother class !
    At this point thoughtq won't make your code work... Try it and see...
    Current Qt projects : QCodeEdit, RotiDeCode

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

    Default Re: Inheritance and QpaintEvent

    It still doesn t work ...

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: Inheritance and QpaintEvent

    Qt Code:
    1. class Mother : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Mother();
    7. virtual ~Mother();
    8.  
    9. protected:
    10. virtual paintEvent(QPaintEvent *e) {}
    11. };
    12.  
    13. class Son : public Mother
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Son();
    19. virtual ~Son();
    20.  
    21. protected:
    22. virtual paintEvent(QPaintEvent *e) { qDebug("Son : paintEvent"); }
    23. }
    24.  
    25. // somewhere in the code :
    26. Son *pSon = ...
    27. pSon->update();
    To copy to clipboard, switch view to plain text mode 

    What does such a code outputs?
    Current Qt projects : QCodeEdit, RotiDeCode

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

    djoul (4th July 2006)

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

    Default Re: Inheritance and QpaintEvent

    In my code, I added the Q_OBJECT that I had forgotten, I tried what you wrote and this is weird, the Son's paintEvent method is never called !!!
    I even tried adding a public method draw in my Son class,with just update() in its body.
    And then trying son->draw() but nothing happens.
    And, if I put just virtual to the Son and Mother paintEvent method I get the error :

    V4 error LNK2001: symbole externe non rÚsolu "protected: virtual void __thiscall Mother:aintEvent(class QPaintEvent *)" (?paintEvent@Weather@@MAEXPAVQPaintEvent@@@Z)

    I must add =0 to the virtual paintEvent in the Mother class,
    or delete the virtual in virtual paintEvent in my Son class.

    For me, the natural code would be in the Mother class : virtual void paintEvent(...)=0 and the destructor virtual.
    And in My son class : void paintEvent(...){.........} with the destructor not virtual.
    (But I almost tried all the possible configurations)
    Last edited by djoul; 3rd July 2006 at 10:04.

  7. #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
    And then trying son->draw() but nothing happens.
    What should happen? How did you try to invoke it? How did you create that object?
    Could you prepare minimal compilable example that reproduces the problem?

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

    djoul (4th July 2006)

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

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by jacek
    What should happen? How did you try to invoke it? How did you create that object?
    Could you prepare minimal compilable example that reproduces the problem?
    In my Son paintEvent I create a file and write some stuff in it and I try to draw some text on my widget to be sure the function is called but the file is never created, so my paintEvent method is never called.
    In anoter class I just do Son *son = new Son(...);
    son->draw() and as I specified draw as public it should work.

    I'll try to prepare a minimal example

  10. #8
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    55
    Thanked 12 Times in 11 Posts

    Default Re: Inheritance and QpaintEvent

    Might doublecheck that your Son isVisible() or has updatesEnabled() ?
    Software Engineer



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

    djoul (4th July 2006)

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

    Default Re: Inheritance and QpaintEvent

    Quote Originally Posted by gfunk
    Might doublecheck that your Son isVisible() or has updatesEnabled() ?
    Hehe, some news : updtatesEnabled do nothing but I tried some Son *son = new Son(...).
    son->setVisible(true); and then son->update();
    And now my paintEvent is called, but I ve new problems : My class Son is supposed to write on a widget but now (I've got a timer that calls update() every second) it creates a new widget each seconds and writes on it.
    But that s a different problem, I think it comes from passing the original widget to the Son, I ll dig on it.
    Thank you all for your help !

  13. #10
    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
    it creates a new widget each seconds and writes on it.
    But that s a different problem, I think it comes from passing the original widget to the Son, I ll dig on it.
    Why do you create new widgets if Son is a widget too? Wouldn't it be enough to redraw Son?

  14. #11
    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 ???

  15. #12
    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.

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.