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

    Hi, I'm a Qt beginner.
    I've got problems dealing with inheritance and qwidget :
    I've got a class that does all the painting stuff on a widget : class Current.
    I've got a class Mother, and a class Son and a class Daugther that inherit from her.

    In my class Current, I've got a static list of Mother elements(that can either be Son or Daughter) : static QList<Mother*> mother_list.
    I do some : mother_list.append(new Son(attributes));

    What I want is : for all the elements of my mother_list, I throw a method draw (virtual) so that the Son and Daughter can do some painting stuff themselves. So they have to have some link with the widget created in the Current Class.

    The problem I have is that when I call "draw", the method is never called in the class Son or Daughter.
    I think I've done big mistakes with passing the arguments, with the inheritance. Here is a part of the .h :

    Qt Code:
    1. class Mother: public QWidget
    2. {
    3. public :
    4. Mother(QWidget *parent, const Obj1&o1, const Obj2&ob2);
    5. virtual ~Mother();
    6. virtual void draw()=0;
    7. ......
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class Son: public Mother
    2. {
    3.  
    4.  
    5. public :
    6. Son(QWidget *parent, const Obj1&ob1, const Obj2&ob1, const Obj3&Ob3, const Obj4&Ob4);
    7. ~Son();
    8. void draw();
    9. ...
    10. protected :
    11. void paintEvent(QPaintEvent *);
    To copy to clipboard, switch view to plain text mode 



    Daugher is the same as son.

    So when in my current class I call mother_list.at(i)->draw();
    Nothing is called.
    Can you help me please !!!

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: Inheritance and QpaintEvent

    Just call mother_list.at(i)->repaint() which will automatically call the paintEvent().

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

    Default Re: Inheritance and QpaintEvent

    I tried it, but I've got the same result : the paintEvent is not called
    It even doesn't call my class Mother paintEvent if I implement him !!!
    Last edited by djoul; 29th June 2006 at 15:16.

  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

    try declaring the destructor, the paintEvent and the draw method as "virtual"...
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    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 !

  6. #6
    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

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

    Default Re: Inheritance and QpaintEvent

    It still doesn t work ...

  8. #8
    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

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

    djoul (4th July 2006)

  10. #9
    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.

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

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

    djoul (4th July 2006)

  13. #11
    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

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.