Results 1 to 2 of 2

Thread: QObject inheritance

  1. #1
    Join Date
    Oct 2011
    Posts
    51
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default QObject inheritance

    Hi!

    My application uses nanopb protocol to communicate with my µC application via TCP/IP, simplified structure of my messages is as follows

    message MsgText{
    enum Type {
    debug = 0; // info messages
    warning = 1; // something go wrong
    fatal = 2; // something goes very wrong
    }
    required int32 ID = 1 [default = 0xff];
    required Type type = 2;
    required string txt = 3;
    }

    message MsgMeasurements{
    required int32 ID = 1 [default = 0x02];
    required int32 meassurement1 = 2;
    required int32 meassurement2 = 3;
    }

    In my application I create a class named MyProtocol and there implement a method that reads data from TCP socket and emits signals for recived fields in messages
    Qt Code:
    1. class MyProtocol : public QObject
    2. Q_OBJECT
    3.  
    4. slots:
    5. void readData();
    6.  
    7. signals:
    8. void dataRecived( int ); // all recived data
    9. void messagesRecived( int ); //all recived messages
    10.  
    11. /// text messages
    12. void dbgMsgReceived( QString txt) ;
    13. void warnMsgReceived( QString msg );
    14. vaoi infoMsgReceived( QString txt );
    15.  
    16. // measurements
    17. void measure1Received( int );
    18. void measure2Received( int );
    19. }
    To copy to clipboard, switch view to plain text mode 

    so in my mainwindow class i can just connect a dataRecived signal to a slot and everything is fine, the same with rest of signals. But now I want to move different messages types to different classes, and if possible provide a way not to change my mainwindow class. So i thought that this structure will by OK

    Qt Code:
    1. class MyText : public QObject {
    2. Q_OBJECT
    3. signals:
    4. /// text messages
    5. void dbgMsgReceived( QString txt) ;
    6. void warnMsgReceived( QString msg );
    7. vaoi infoMsgReceived( QString txt );
    8. }
    9.  
    10. class MyMeasurements : public QObject
    11. {
    12. Q_OBJECT
    13. signals:
    14. // measurements
    15. void measure1Received( int );
    16. void measure2Received( int );
    17. }
    18.  
    19. class MyProtocol : public MyMeasurements, MyText
    20. Q_OBJECT
    21. void readData();
    22. signals:
    23. void dataRecived( int ); // all recived data
    24. void messagesRecived( int ); //all recived messages
    25. }
    To copy to clipboard, switch view to plain text mode 

    But unfortunately I can't inherit QObject from meany sources... I know that without inherit I can just create a object of MyText and MyMeasurements in MyProtocol and then connect signals from them to signals in MyProtocol, but it seams to by not a good idea.
    What are a typical approaches to such problems? Can somebody tell me what hierarchy I should chose?

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

    Default Re: QObject inheritance

    Quote Originally Posted by cszawisza View Post
    I know that without inherit I can just create a object of MyText and MyMeasurements in MyProtocol and then connect signals from them to signals in MyProtocol, but it seams to by not a good idea.
    Doesn't sound that bad to me given your goal of not changing any code where MyProtocol is being used.

    Alternatively MyText and MyMeasurements could just specify the signals as virtuals in non-QObject classes

    Qt Code:
    1. class MyText {
    2. // Will be implemented as signals in derived class MyProtocol
    3. /// text messages
    4. virtual void dbgMsgReceived( QString txt) = 0;
    5. virtual void warnMsgReceived( QString msg ) = 0;
    6. virtual void infoMsgReceived( QString txt ) = 0;
    7. }
    8.  
    9. class MyMeasurements
    10. {
    11. // Will be implemented as signals in derived class MyProtocol
    12. // measurements
    13. virtual void measure1Received( int ) = 0;
    14. virtual void measure2Received( int ) = 0;
    15. }
    16.  
    17. class MyProtocol : public QObject, public MyMeasurements, public MyText
    18. Q_OBJECT
    19. void readData();
    20. signals:
    21. void dataRecived( int ); // all recived data
    22. void messagesRecived( int ); //all recived messages
    23.  
    24. // signals from MyMeasurements
    25. void measure1Received( int );
    26. void measure2Received( int );
    27.  
    28. // signals from MyText
    29. void dbgMsgReceived( QString txt);
    30. void warnMsgReceived( QString msg );
    31. void infoMsgReceived( QString txt );
    32. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    cszawisza (17th April 2014)

Similar Threads

  1. Replies: 2
    Last Post: 9th October 2012, 20:26
  2. Inheritance confusion - which class made inheritating from QObject
    By kornicameister in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2010, 10:54
  3. Multiple inheritance with QObject
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 17:40
  4. Possibly an inheritance problem with QObject?
    By chadkeck in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2008, 02:26
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 18:49

Tags for this Thread

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.