Results 1 to 12 of 12

Thread: Multiple inheritance & Qt

  1. #1
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Multiple inheritance & Qt

    This post is because of QQ15.

    I have an existing application which has several object, all related, but not yet using a single unifying object to command a universal interface across these objects. There are 4 different main types, one table object (which inherits QTable), one graph object (which inherits QWidget) and two text objects (which both inherit QTextView).
    All these object thusly have a common ancestor, namely QWidget.
    Using single inheritance would be a solution, but would require the reimplementing of Qt code, which would make it harder to maintain and which should be needless in the first place.

    As far as I can read from the article, the solution is to create an interface and a wrapper, as follows:

    Qt Code:
    1. class Interface {
    2. public:
    3. virtual void method() = 0;
    4. };
    5.  
    6. class Wrapper : public QWidget, public Interface {
    7. Q_OBJECT
    8. Interface *wrapped;
    9. public:
    10. Wrapper(QWidget *p) : QWidget(p) {
    11. wrapped = cast_stuff<Interface *>(p);
    12. }
    13. ~Wrapper();
    14. public slots:
    15. void method() { wrapped->method(); }
    16. };
    To copy to clipboard, switch view to plain text mode 

    Then you create an object structure based on the interface:

    Qt Code:
    1. class Document : public QWidget, public Interface {
    2. Q_OBJECT // Is this even necessary?
    3. Wrapper *wrapper;
    4. public:
    5. Document(QWidget *p = 0) : QWidget(p) { }
    6. virtual ~Document() { }
    7. virtual void method() { generic_implementation; }
    8. };
    9.  
    10. class Table : public QTable, public Document {
    11. public:
    12. Table(QWidget *p = 0) : QTable(p), Document(0) {
    13. }
    14. ~Table() {
    15. }
    16. virtual void method() { specific_implementation; }
    17. };
    To copy to clipboard, switch view to plain text mode 


    This is completely based on the example declarations. Now I have a few questions.
    First of all, how would you go about creating a specific object?
    Is it like this:
    Qt Code:
    1. Interface *d = new Table();
    To copy to clipboard, switch view to plain text mode 
    (But then you wouldn't need the wrapper?)
    Or would you use the wrapper, like so:
    Qt Code:
    1. Wrapper *w = new Wrapper(new Table());
    To copy to clipboard, switch view to plain text mode 
    Second, why would you include the Wrapper object in the Document, does the wrapped object need to know if it's being wrapped and if so, why?

    I hope someone can help me with the problem, as Voker's example code didn't include any usage tips.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance & Qt

    There are several things I don't understand in what you wrote:
    First, the article you linked to is dicussing inheriting QObject from a template calss, and the problematic it has with moc.
    However you are talking about general inharitance problems that has nothing to do with moc and templates.

    Now, regarding your inheritance design:
    A wrapper is made, when you want to have a part of a code that always does the same work with the same API, but that the objects it operates on might change (polymorphysem).
    So in your case I would say you need three such wrappers:
    -A Table object interface
    -A graph object interface
    -A text object interface
    - If there are any custom (i.e not QWidget) methods all these object share then you can also make an interface that binds all these classes in a common wrapper for these operations
    NOTE: such interfcae design schemes make sence for larger applications or for code that is often used in various situations, not that much for small and specific applications)
    You can use the fact they all derive from QWidget for all operations that are derived from QWidget, and in these places use a QWidget pointer (so you will be able to access all objects on these oprations regardless of which derived type the object is currently).
    Using single inheritance would be a solution, but would require the reimplementing of Qt code, which would make it harder to maintain and which should be needless in the first place.
    I didn't understand this in respect to the code you posted.
    You will have to write your own code for each custom class, thats what deriving is all about.
    What Qt code would need to REimplement?
    your Interface class is not needed, your wrapper class can offer that functionality.

  3. #3
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Multiple inheritance & Qt

    Quote Originally Posted by high_flyer
    There are several things I don't understand in what you wrote:
    First, the article you linked to is dicussing inheriting QObject from a template calss, and the problematic it has with moc.
    However you are talking about general inharitance problems that has nothing to do with moc and templates.
    Sorry for not being more clear on this, as the article actually treats three different subjects. My question is regarding the last article, called "Multiple inheritance".

    Now, regarding your inheritance design:
    A wrapper is made, when you want to have a part of a code that always does the same work with the same API, but that the objects it operates on might change (polymorphysem).
    So in your case I would say you need three such wrappers:
    -A Table object interface
    -A graph object interface
    -A text object interface
    - If there are any custom (i.e not QWidget) methods all these object share then you can also make an interface that binds all these classes in a common wrapper for these operations
    Three different wrappers? Why would they be necessary, as there only needs to be one general wrapper which provided for various of the shared functions (plus the added advantage of being able to treat and manage them as the same object)?

    NOTE: such interfcae design schemes make sence for larger applications or for code that is often used in various situations, not that much for small and specific applications)
    This is a rather large application.
    You can use the fact they all derive from QWidget for all operations that are derived from QWidget, and in these places use a QWidget pointer (so you will be able to access all objects on these oprations regardless of which derived type the object is currently).
    That's what's happening already, but there are numerous situations where the three different object classes have to reimplement the same functions to provide certain functionality, but there is no interface to mandate the implementation of them.

    I didn't understand this in respect to the code you posted.
    You will have to write your own code for each custom class, thats what deriving is all about.
    What Qt code would need to REimplement?
    your Interface class is not needed, your wrapper class can offer that functionality.
    Well, a single inheritance solution using a shared Document class would be a solution, but as such you will need to create a "has a" type of interface, which passes the function calls to the object (i.e. from Table to the QTable). This carries several problems as you need to hijack a lot of things to make it appear as though it actually is a QTable. Reimplementing may be a harsh word, as you "pass the buck", but effectively you need to provide the complete QTable interface, for starters, and a QTextEdit interface for the Text documents.

    The problem stems from the face that the three major objects are in fact already base classes for more specific classes.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance & Qt

    Three different wrappers? Why would they be necessary, as there only needs to be one general wrapper which provided for various of the shared functions (plus the added advantage of being able to treat and manage them as the same object)?
    Well, it was not clear to me what was common to all objects.
    So I assumed each object has its own functionlity and diefferent API.
    But in this post you made things more clear.
    Then if what you are looking for is an inteface class that will bind only the common functionalities of the various objects, I would make one such interface and use virtual or pure virtual methods.
    Where is the problem then?
    That's what's happening already, but there are numerous situations where the three different object classes have to reimplement the same functions to provide certain functionality, but there is no interface to mandate the implementation of them.
    I see.
    This is hard to answer with out knowing exactly the specifics of your implementation or exactly what it is you want to do.

  5. #5
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Multiple inheritance & Qt

    Quote Originally Posted by high_flyer
    Then if what you are looking for is an inteface class that will bind only the common functionalities of the various objects, I would make one such interface and use virtual or pure virtual methods.
    Where is the problem then?
    Signals and slots.

    Rather more specifically, the MOC. The Q_OBJECT macro does some C style casting which the compiler won't accept in the multiple inheritance scenario. It would work perfectly if we didn't use either signals nor slots, but that would kind of remove most of the functionality of the program.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance & Qt

    I don't understand why signal and slots are a problem.
    I use multiple inheritance with Qt, many times in order to give external non Qt classes the ability to use signal and slots and I have no problem.
    Could you give an example for a class and a compiler erros it gives you?
    You have to make sure you have Q_OBJECT macro in your class decleration.

  7. #7
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Multiple inheritance & Qt

    Quote Originally Posted by high_flyer
    I don't understand why signal and slots are a problem.
    I use multiple inheritance with Qt, many times in order to give external non Qt classes the ability to use signal and slots and I have no problem.
    Could you give an example for a class and a compiler erros it gives you?
    You have to make sure you have Q_OBJECT macro in your class decleration.
    Ok, first we define a base class:
    Qt Code:
    1. class Document : virtual public QWidget {
    2. Q_OBJECT
    3.  
    4. public:
    5. Document(QWidget *p, const char *c) : QWidget(p, c) { }
    6. virtual ~Document() {}
    7. public slots:
    8. virtual void method() {QMessageBox::warning(0, "bla", "doc");}
    9. };
    To copy to clipboard, switch view to plain text mode 
    Then we define a derived class:
    Qt Code:
    1. class Table : virtual public QTable, virtual public Document {
    2. public:
    3. Table(QWidget *p, const char *n) : QTable(p, n), Document(NULL, NULL) {
    4. }
    5. virtual ~Table() {
    6. }
    7. virtual void method() { QMessageBox::warning(0, "bla", "table"); }
    8. };
    To copy to clipboard, switch view to plain text mode 
    Then we make some test code:
    Qt Code:
    1. Document *doc = new Table(this, "");
    2.  
    3. action = new QAction( tr("Test"), tr("&Test"), CTRL+Key_T, this );
    4. connect( action, SIGNAL( activated() ), doc, SLOT( method() ) );
    To copy to clipboard, switch view to plain text mode 
    This works fine. But what if you want to add slots and signals to the Table class? Well, we add the Q_OBJECT macro, and add a public slot thingy with a specific method, like so:
    Qt Code:
    1. class Table : virtual public QTable, virtual public Document {
    2. Q_OBJECT
    3. public:
    4. Table(QWidget *p, const char *n) : QTable(p, n), Document(NULL, NULL) {
    5. }
    6. virtual ~Table() {
    7. }
    8. virtual void method() { QMessageBox::warning(0, "bla", "table"); }
    9. public slots:
    10. void specific_slot() { QMessageBox::warning(0, "bla", "slotty"); }
    11. };
    To copy to clipboard, switch view to plain text mode 
    However, when compiling this, Visual Studio throws a hissy fit, saying
    c:\projects\mi\table.h(7) : error C2594: 'type cast' : ambiguous conversions from 'class Table *const ' to 'class QObject *'
    This is the very line in the table.h file, which contains the Q_OBJECT macro.
    It can't be good design if you have to include all the slots from derived classes in the base class. :|

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance & Qt

    I get it now.
    You where talking about multiple QObject inheritance, where as I was talking about general multiple inheritance of non Qt class with a QObject class.
    As far as I know this is not possible due to moc.
    With out thinking too deep about it at the moment, it could be that a scheme of "between classes" might work, where in your inheritance tree you split the functionalities in to QObject and non QObject calsses, and then fuse these togeather, where in each "fusion" you get only one QObject inheritance. (Hope I was clear, but I am not sure this is doable for you, it depends on your inheritance needs and structure)

    EDIT:
    You could achieve the above by adding the "slots" in your non QObject classes as normal methods, and then when you drive your QObject-NonQObject class, to wrap the intended slot methods in slots.
    This way Document class wont need to be a QObject and it will eliminate the QObject ambiguity
    Last edited by high_flyer; 7th March 2006 at 10:31.

  9. #9
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Multiple inheritance & Qt

    If there was a quick and easy way to do this, I would have done so already. The major issue with writing all these wrapper classes is that it would take at least 24 of them, possibly up to 34. In addition to this, 1533 connect() calls would have to be scrutinised (or adapted), and countless other blurps of code would need to be adapted through over 260.000 lines of code (excluding blank and comment lines).

    Hope you can see this is not really something I want to do. There is an existing object structure, which I keep wanting to use, just improve on it by taking advantage of inheritance and polymorphism more (I'm not the original creator, this is maintenance work).

    Is this an impossibility?

    I get it now.
    You where talking about multiple QObject inheritance, where as I was talking about general multiple inheritance of non Qt class with a QObject class.
    As far as I know this is not possible due to moc.
    Yes, that's what I'm talking about. The third article in QQ15 is specifically about a certain method on solving this, except it's a bit unclear about the details.
    Last edited by dublet; 7th March 2006 at 12:00.

  10. #10
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance & Qt

    Do you need to use 'Qt' signals and slots specifically? Maybe you can take a look at boost.org and make use of the signals library offered there?
    Save yourself some pain. Learn C++ before learning Qt.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple inheritance & Qt

    Yes, that's what I'm talking about. The third article in QQ15 is specifically about a certain method on solving this, except it's a bit unclear about the details.
    Now that I have read fully the text in the link you gave I see that what the trolls offer in the link you offered is the same thing I did, only they formulated it much better
    Notice that they are using a wrapper to wrap the methods that are intended to be used as slots from the non Qt class in slots of the wrapper.

  12. #12
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Multiple inheritance & Qt

    Quote Originally Posted by high_flyer
    Now that I have read fully the text in the link you gave I see that what the trolls offer in the link you offered is the same thing I did, only they formulated it much better
    Notice that they are using a wrapper to wrap the methods that are intended to be used as slots from the non Qt class in slots of the wrapper.
    If you'll then read my first post again, you can see that's what I tried to implement in the first place.

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 16:29
  2. Multiple Inheritance for QGraphicsItem
    By pankaj.patil in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 15:49
  3. QThread and Multiple inheritance
    By ^NyAw^ in forum General Programming
    Replies: 6
    Last Post: 10th January 2008, 11:50
  4. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 19:37
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 05:00

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.