I have two classes under QT, one to make a form, the other to send the collected data on the serial port. There is a button to submit and send the information on the serial port. The first class “myform” contains the file descriptor variable and the slot function for the submit button.
class myform: public QWidget
{
Q_OBJECT
private slots:
void submitclicked(void);
public:
myform(QWidget *parent = 0);
// some vars...
QPushButton *submit;
int serialfd;
};
The second class “serialcom” inherits the first class, since I want to implement the slot function in that “serialcom” class.
class serialcom : public myform
{
Q_OBJECT
public:
int serialdev_init(void);
serialcom(myform *parent=0);
private:
// some vars...
};
The serialfd file descriptor is getting initialized in the constructor for serialcom through serialdev_init(). I have checked, it is initialized properly. The problem is that when the SLOT for submit button is called, serialfd has garbage value (I mean inside the submitclicked() slot ), not the initialized value.
Isn’t it supposed to preserve the value, or am I wrong to assume that ? I am pretty new to this QT or even C++ business, so please mind my stupidities if any…
Here is the main function…
int main(int argc,char **argv)
{
QApplication app(argc,argv);
myform *trial = new myform;
serialcom *serial = new serialcom(trial);
trial->show();
return app.exec();
}
Any other suggestions are also welcome.
Derived class construction involves base class sub-object construction first followed by derived class sub-object. So, derived class object has two sub-objects.
trailhas a sub-objects of typeQWidget, myform. Now thismyformsub-object has it’s own member variable(s)serialfdwhich is uninitialized. Now,trialcannot accessserialcommembers because a derived class can access base class members but the otherwise is not true.The same happens with this statement too except that
serialhas it’s own sub-objectsQWidget, myform, serial.Now the two
myformsub-objects has no relation. You are settingserialfdof this sub-object but seeing theserialfdoftrailsub-object.Hope you understood what you are doing wrong.