PDA

View Full Version : QObject inheritance



cszawisza
15th April 2014, 11:14
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


class MyProtocol : public QObject
Q_OBJECT

slots:
void readData();

signals:
void dataRecived( int ); // all recived data
void messagesRecived( int ); //all recived messages

/// text messages
void dbgMsgReceived( QString txt) ;
void warnMsgReceived( QString msg );
vaoi infoMsgReceived( QString txt );

// measurements
void measure1Received( int );
void measure2Received( int );
}


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




class MyText : public QObject {
Q_OBJECT
signals:
/// text messages
void dbgMsgReceived( QString txt) ;
void warnMsgReceived( QString msg );
vaoi infoMsgReceived( QString txt );
}

class MyMeasurements : public QObject
{
Q_OBJECT
signals:
// measurements
void measure1Received( int );
void measure2Received( int );
}

class MyProtocol : public MyMeasurements, MyText
Q_OBJECT
void readData();
signals:
void dataRecived( int ); // all recived data
void messagesRecived( int ); //all recived messages
}


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?

anda_skoa
15th April 2014, 13:03
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



class MyText {
// Will be implemented as signals in derived class MyProtocol
/// text messages
virtual void dbgMsgReceived( QString txt) = 0;
virtual void warnMsgReceived( QString msg ) = 0;
virtual void infoMsgReceived( QString txt ) = 0;
}

class MyMeasurements
{
// Will be implemented as signals in derived class MyProtocol
// measurements
virtual void measure1Received( int ) = 0;
virtual void measure2Received( int ) = 0;
}

class MyProtocol : public QObject, public MyMeasurements, public MyText
Q_OBJECT
void readData();
signals:
void dataRecived( int ); // all recived data
void messagesRecived( int ); //all recived messages

// signals from MyMeasurements
void measure1Received( int );
void measure2Received( int );

// signals from MyText
void dbgMsgReceived( QString txt);
void warnMsgReceived( QString msg );
void infoMsgReceived( QString txt );
}


Cheers,
_