Results 1 to 6 of 6

Thread: inheritance issue

  1. #1
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default inheritance issue

    Hello,
    i have some strange behaviours in my code:

    1- I have cWindowBase class, where it has the public member Qtimer *timer;. Now i have 2 derived classes, Class1 and Class2 that inherit cWindowBase with public keyword. everything works fine in these 2 derived classes. but when i set-up and start the timer in Class1 and i try from Class2 timer->isActive() the app crashes.

    2- i have QProgressDialog *progress; as private member in ClassA. when i call the class A from ClassB (no inheritance) like
    Qt Code:
    1. ClassA test;
    2. test.ShowProg("Ttile", "Label")
    To copy to clipboard, switch view to plain text mode 
    the progressdialog works fine the first time i call it, the rest attempts progressdialog works but the Label is not displayed (only title is OK), the only way to get round it is to go out from ClassB.

    hints plz?
    Last edited by raedbenz; 4th August 2010 at 11:38.

  2. #2
    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: inheritance issue

    Step 1: get and read a good C++ book.

    For problem 1: It's obvious the program will crash at timer->isActive since timer is an uninitialised pointer (as far as I understand your description).
    What is it you want to do? Do you want to get information of class 1 in class 2?

    Problem 2: Can you give more information, like source code?

  3. #3
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: inheritance issue

    Quote Originally Posted by tbscope View Post
    For problem 1: It's obvious the program will crash at timer->isActive since timer is an uninitialised pointer (as far as I understand your description).
    hello,

    The Timer is already initialized and started in Class1. and works fine i.e. it times out and class the slot peridiocically. but if i try to stop or check if it is Active from Class2 it doesnt work. But yes it is not initialized in Class2.

    What is it you want to do? Do you want to get information of class 1 in class 2?
    Yes.. note, the Timer is defined in the base class that is inherited in both Class1 and 2.

    Problem 2
    Qt Code:
    1. void SysClass::ProgDlg(QString title, QString text)
    2. {
    3. progress = new QProgressDialog(0, Qt::Dialog);
    4.  
    5. progress->setWindowTitle(title);
    6. progress->setLabelText(text);
    7. progress->setRange(0,100);
    8. progress->setAutoClose(true);
    9. progress->setAutoReset(true);
    10.  
    11. progress->show();
    12. QCoreApplication::processEvents();
    13. }
    To copy to clipboard, switch view to plain text mode 
    and i call it every 3 seconds.
    Qt Code:
    1. SysClass tmp;
    2. SysClass.ProgDlg("Hello","Wait")
    To copy to clipboard, switch view to plain text mode 
    the progress bas appears but the string Wait appears only in the first call only..
    Thanks

  4. #4
    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: inheritance issue

    Quote Originally Posted by raedbenz View Post
    The Timer is already initialized and started in Class1. and works fine i.e. it times out and class the slot peridiocically. but if i try to stop or check if it is Active from Class2 it doesnt work. But yes it is not initialized in Class2.

    Yes.. note, the Timer is defined in the base class that is inherited in both Class1 and 2.
    Like I said, try to find a good C++ book that explains pointers and class inheritance in detail.
    This is not how C++ works.

    Yes, you derived from a common base class where you define a pointer to a timer. But, the pointer in class 1 is completely different than the pointer in class 2.
    What you can do is assign the timer pointer of class 1 to the timer pointer of class 2 so that both point to the same memory address. If you don't, timer->isActive() in class 2 WILL crash.

    If you have a common base class, it doesn't automatically become the one and only initialised base class of all your subclasses. Look into singletons. Or just use setters/getters, signals/slots, events... to get information from one object to the other.

    Qt Code:
    1. void SysClass::ProgDlg(QString title, QString text)
    2. {
    3. progress = new QProgressDialog(0, Qt::Dialog);
    4.  
    5. progress->setWindowTitle(title);
    6. progress->setLabelText(text);
    7. progress->setRange(0,100);
    8. progress->setAutoClose(true);
    9. progress->setAutoReset(true);
    10.  
    11. progress->show();
    12. QCoreApplication::processEvents();
    13. }
    To copy to clipboard, switch view to plain text mode 
    and i call it every 3 seconds.
    Qt Code:
    1. SysClass tmp;
    2. SysClass.ProgDlg("Hello","Wait")
    To copy to clipboard, switch view to plain text mode 
    the progress bas appears but the string Wait appears only in the first call only..
    Thanks
    I have no idea about this problem.
    Try this:
    Qt Code:
    1. SysClass *tmp = new SysClass;
    2. SysClass->ProgDlg("Hello","Wait");
    To copy to clipboard, switch view to plain text mode 
    Does that solve the problem?

  5. #5
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: inheritance issue

    Quote Originally Posted by tbscope View Post
    What you can do is assign the timer pointer of class 1 to the timer pointer of class 2 so that both point to the same memory address. If you don't, timer->isActive() in class 2 WILL crash.
    How do we assign the timer pointer of class 1 to the timer pointer of class 2 (without singleton) ?

    problem 2 was solved by adding
    Qt Code:
    1. progress->setMinimumDuration(0);
    To copy to clipboard, switch view to plain text mode 

    Thanks alot
    Last edited by raedbenz; 6th August 2010 at 08:02.

  6. #6
    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: inheritance issue

    You can have this for example:

    Qt Code:
    1. class MyClass1
    2. {
    3. public:
    4. void setTimer(Timer *newTimer)
    5. {
    6. t = newTimer;
    7. }
    8.  
    9. private:
    10. Timer *t;
    11. };
    12.  
    13. class MyClass2
    14. {
    15. public:
    16. Timer *timer() const
    17. {
    18. return t;
    19. }
    20.  
    21. private:
    22. Timer *t;
    23. };
    24.  
    25. ...
    26.  
    27. MyClass1 *c1 = new MyClass1;
    28. MyClass2 *c2 = new Myclass2;
    29.  
    30. c1->setTimer(c2->timer());
    To copy to clipboard, switch view to plain text mode 

    HOWEVER!
    This should be done with care. Class 1 doesn't have any idea when class 2 deletes the timer. At least not if you don't code some handling for this situation.

    I suggest working with signals and slots.

Similar Threads

  1. C++ inheritance
    By yyiu002 in forum Newbie
    Replies: 3
    Last Post: 29th June 2010, 11:38
  2. inheritance
    By steiner in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2007, 20:17
  3. inheritance
    By mickey in forum General Programming
    Replies: 11
    Last Post: 28th September 2007, 21:54
  4. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 22:29
  5. QSA and inheritance
    By jwintz in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 14:05

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.