I have two windows: mainwindow.ui and dialog.ui. In the mainwindow.cpp I initialise some objects on the heap of another class for Ethernet-communication and use functions of these objects to read date from the bus and show the values in the mainwindow.ui. In dialog.ui I would like to set the values on the bus, but the problem is to access the communication-functions and objects in mainwindow.cpp.
I wanted to define dialog.cpp as friend class, but I make something wrong. Here is some code:
mainwindow.h
class MainWindow : public QMainWindow
{protected: DateReg *myPosReg;...}
mainwindow.cpp
...
myPosReg = new DateReg(DateValue->AddReg());
...
myPosReg->GetValue(a,b,c);
myPosReg->setValue(a,b,c);
...
Can I somehow access setValue() function in dialog.cpp? Should I allways use dialog.cpp for the dialog.ui or there is some possibility work with dialog.ui-date in mainwindow.cpp? To set the values in dialog.ui I use QDoubleSpinBox.
In the mainwindow.ui I call dialog.ui.
Excuse me for my English.
As far as I understand, you have two classes:
MainWindowandDialog. AsMainWindow::myPosRegis protected, you can only access it inDialog, ifDialogderives fromMainWindowor if it is a friend.The following design should work:
As an alternative, I would suggest a more Qt-ish way by using signals and slots. When the spin-boxes in
Dialogchange, emit a signal to whichMainWindowlistens.We start with the dialog:
So the dialog has an internal slot
onSpinBoxValueChanged(), which is called whenone of the spinboxes changes its value. The sole purpose of this slot is to emit
another signal
valuesChangedto which we’ll connect the main window (i.e. thedialog’s internal slot combines three values into one signal).
The main window could be the following:
And the code, which connects both
When the user changes the value in a spinbox, the following chain-reaction happens:
The advantage is that you decouple
MainWindowandDialog:Dialogjust signals that the values have changed but does not care what happens with them andMainWindowchanges itsmyPosRegbut does not care, where the values are coming from.