Results 1 to 5 of 5

Thread: Multiple inheritance question

  1. #1
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple inheritance question

    Hi,

    I'm writing some classes to handle text that is transmitted over a serial communication link. There will be classes to 'observe' what is passing over the link. They are like textedits or linedits that show the data as it is received. Other classes will do logging etc.

    I defined a base class for the observers with the necessary slots for receiving data. Then I derived a QUI object from this, based on a QPlainTextEdit. Like this :

    Qt Code:
    1. class CComLineObserver : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit CComLineObserver(QObject *parent = 0);
    6.  
    7. public slots:
    8. virtual void OnTxLine( QString sLine );
    9. virtual void OnRxLine( QString sLine );
    10. virtual void OnRxChar( QString sLine );
    11.  
    12. };
    13.  
    14. class CComLineTextEdit : public QPlainTextEdit, virtual public CComLineObserver
    15. {
    16. Q_OBJECT
    17. public:
    18. explicit CComLineTextEdit( QWidget * parent = 0 );
    19.  
    20. public slots:
    21. virtual void OnRxLine( QString sLine );
    22. };
    To copy to clipboard, switch view to plain text mode 

    When I use this CComLineTextEdit in a form, I get the following error message in setupUI of the UI_....H file that moc builds (teLog is the name of the gui object) :

    Qt Code:
    1. ambiguous acces of 'setObjectName'
    2. teLog->setObjectName(QString::fromUtf8("teLog"));
    To copy to clipboard, switch view to plain text mode 

    I think it has to do with the fact that both QPlainTextEdit and CComLineTextEdit are derived from QObject (the 'diamond problem'). I already used the 'virtual' keyword before 'CComLineTextEdit', but this doesn't help. I'm sure it's just a little syntax problem, but I just can't put my finger on it.

    Does anyone know the solution ?

    Thanks,
    Marc

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiple inheritance question

    According to the Qt docs, you can only derive from QObject once. Thats a hardcore rule u must follow in Qt.

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple inheritance question

    Really ??

    I found an article now in the QQ : Academic Solutions to Academic Problems.

    So we have a great toolkit with the great mechanism of signal and slots, but when I want to implement a common thing like an observer, I cannot use this mechanism ?

    That is very disappointing. The workaround mentioned in QQ15 to have a wrapper member object is really really overengineered for most things I use. Maybe a todo for Trolltech : "allow a STRAIGHTFORWARD way to define interfaces that use the signal and slot mechanism".

    Best regards,
    Marc

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple inheritance question

    There's no real reason for CComListener to be a QObject if it is just an abstract interface. The public functions in the interface class can be treated as slots in the derived class For example, this seems to work:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. // Class is abstract by virtue of having at least one pure virtual
    5. class IComLineObserver
    6. {
    7. public:
    8. explicit IComLineObserver() {}
    9. virtual ~IComLineObserver() = 0;
    10.  
    11. // these can be slots in derived classes
    12. virtual void OnTxLine( QString sLine ) {}
    13. virtual void OnRxLine( QString sLine ) {}
    14. virtual void OnRxChar( QString sLine ) {}
    15. virtual void test() {}
    16. };
    17. IComLineObserver::~IComLineObserver() {}; // must have an implementation
    18.  
    19. class A: public QObject, public IComLineObserver
    20. {
    21. Q_OBJECT
    22. public:
    23. A() { }
    24. ~A() { }
    25. public slots:
    26. void test() { qDebug() << "Test"; }
    27. };
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QApplication app(argc, argv);
    32.  
    33. A a;
    34. QTimer t;
    35. QObject::connect(&t, SIGNAL(timeout()), &a, SLOT(test()));
    36. t.start(1000);
    37. return app.exec();
    38. }
    39. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple inheritance question

    Hi ChrisW67,

    Well, CComListener wouldn't be purely abstract. I would like to put some basic functionality in it like connecting to a serial port object and handling stuff when a port is opened / closed / deleted / .... Some of this 'stuff' involves responding to signals of the serial port object.

    But your suggestion leads me to a viable option : I could put the interface in an abstract class as you said, and define a member that is a QObject. This member could then interface to the serial port object and use callbacks to CComListener when it receives a signal from the serial port object. I'll give it a try.

    Thanks,
    Marc

    P.S. Still, it would be great if it just worked plain and simple with signals and slots in the interface definition.

Similar Threads

  1. Multiple inheritance with QObject
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 16:40
  2. Multiple Inheritance for QGraphicsItem
    By pankaj.patil in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 14:49
  3. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  4. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 04: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
  •  
Qt is a trademark of The Qt Company.