Results 1 to 10 of 10

Thread: Problem of inheritance

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem of inheritance

    Hello everyone, I am a inheritance problem with Qt 5.0.1. I have a class declared as:
    Qt Code:
    1. class WorldGui : public World, public MainWindow
    To copy to clipboard, switch view to plain text mode 
    in the MainWindow class I have a function:
    Qt Code:
    1. void MainWindow::fileLoadCb()
    To copy to clipboard, switch view to plain text mode 
    that is called through the mechanism of Signal/Slot of MainWindow.
    Within this function I would want to call a function of WordlGui
    Qt Code:
    1. Load( filename );
    To copy to clipboard, switch view to plain text mode 
    how can I do ?
    I could get an example of code that tell me how to do ?
    I'm going crazy with this problem, which I think is not that difficult, but I can not find the solution.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem of inheritance

    Well your starting point is wrong. How could MainWindow know that it will be base class to WorldGui? So there is no direct possibility to do so.

    If you know in MainWindow that it should use a function of WorldGui, then direct implement it there. Or use a complete different design approach.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem of inheritance

    that is called through the mechanism of Signal/Slot of MainWindow.
    Within this function I would want to call a function of WordlGui
    When you say signal/slot of MainWindow, it should mean that MainWindow is a QObject based class (if not QWidget), which will lay a restiction on the inheritance sequence.

    Please show us the class definition of "World" and "MainWindow", or at-least tell us what those are based on.

    how can I do ?
    I could get an example of code that tell me how to do ?
    I'm going crazy with this problem, which I think is not that difficult, but I can not find the solution.
    There are things which one cannot do in Qt (at-least out of box). Please show us more about what are you really looking for.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of inheritance

    Quote Originally Posted by giorgik View Post
    Hello everyone, I am a inheritance problem ...
    No, you have a design problem. MainWindow should not need to directly call anything on WorldGui. If MainWindow cannot do its job without using WorldGui, then MainWindow is not complete and should not form part of WorldGui - this would be a circular dependency.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem of inheritance

    Thank you all for the answers. In fact, I poorly designed classes. The solution is to use a class WorldGui that does not derive from MainWindow, but use a pointer to MainWindow as a member variable of WorldGui. Then use the technique of signal/slot to communicate MainWindow with WorldGui.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of inheritance

    no, just no.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem of inheritance

    The solution is to use a class WorldGui that does not derive from MainWindow, but use a pointer to MainWindow as a member variable of WorldGui. Then use the technique of signal/slot to communicate MainWindow with WorldGui.
    I don't see any problem with this approach, go ahead and do it. One note is to inherit WorldGui from QObject

  8. #8
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem of inheritance

    Quote Originally Posted by amleto View Post
    no, just no.
    Could you please elaborate on that? I use similar approach sometimes and it would be good to hear about downsides.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem of inheritance

    I would probably do it the other way around, i.e. letting the UI class have a pointer to the non-UI/logic class. This way the logic is more independent of the concrete UI implementation and UI can be changed more freely.

    Cheers,
    _

  10. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of inheritance

    Quote Originally Posted by lanz View Post
    Could you please elaborate on that? I use similar approach sometimes and it would be good to hear about downsides.
    by changing inheritance for composition the overall design hasn't changed, just the implementation. It appears that the two concrete classes still cannot do a job by themselves, and still require each other to do a job.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    lanz (25th February 2013)

Similar Threads

  1. Problem with inheritance and object share use?
    By tonnot in forum General Programming
    Replies: 2
    Last Post: 1st August 2011, 19:08
  2. problem with Inheritance and QTreeView
    By Kicer in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 16:10
  3. Inheritance problem
    By brevleq in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2008, 07:27
  4. problem in widget inheritance
    By wagmare in forum Qt Programming
    Replies: 2
    Last Post: 10th December 2008, 08:35
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 18:49

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.