[SOLVED] Newbie is confused... how to connect different classes in Qt (structure of program)
-
Hello,
I am now writing my first GUI applications having more than one window and I am really confused with the structure of my application.
For example I have the following classes:
MainWindow, PrepareData, ProcessData, SubProcessing1, SubProcessing2, SubProcessing1Dialog, SubProcessing2Dialog
Main Window is my mainWindow and creates instances of PrepareData and ProcessData.
Whenever new data is prepared in PrepareData it gets delivered to ProcessData using the SignalSlot mechanism.The instance of ProcessData creates instances of SubProcessing1 and SubProcessing2.
These instances need values from the corresponding classes SubProcessing1Dialog and SubProcessing2Dialog.
The instances of the Dialogs are also created in the mainWindow as they are GUI windows.Now my question is what is the best way to deliver values from the GUI Dialogs to the SubProcessing classes. The problem is that I don’t know how to make the in the ProcessData created instances available to the Dialog classes.
My first solution was to use static class variables because I can access them without having an instance available. I know this is rubbish because this works not if I have more than one instance of the Dialog.So how should I structure my program and how can I make the instances available?
Thank you very much :-)
-
Hi @RolBri, and welcome to the Qt Dev Net!
Now my question is what is the best way to deliver values from the GUI Dialogs to the SubProcessing classes.
In Qt, the best way is to transfer data using signals and slots. See http://doc.qt.io/qt-5/signalsandslots.html to get started.
-
Hi and welcome to devnet,
One solution is to make MainWindow establish connections between the dialogs and corresponding SubProcessing objects. That way, neither classes need to know anything about the other.
Hope it helps
-
There is no any magic in transferring data.
In any case chain of the events/calls have to lead to target object receive that data.
That means that another object which had access to both - target object and data to be passed uinitiated the call (one way or another - event, function call or signal/slot) .Difference is only in how the chain is organized.
The simplest ( does not mean the right) way to organize transfer data from GuiDialog to Subprocessing would be at the time MainWindow is creating GuiDialog to request a pointer to Subprocessing from ProcessData and make GuiDialog aware of it.
This approach is common in standard C++ even though the chain of the calls and awareness one object about another may vary.
In Qt you can decrease level one object knows about another using signal/slots mechanics.
It does come in cost of efficiency.One of the possible implementation
add a signal MyDataChanged ( myData data ) to GuiDialog,
connect it to slot in SubProcessing directly or through a chain of signals.For example MainWindow knows about ProcessData and GuiDialog
So it can connect GuiDialog signal with signal or slot in ProcessData.
Process Data can actually either connect its own signal with slot in Subprocessing or call that slot directly.Hope that was helpfull.
-
Sure,
I assume that you know how to use Signals and Slots.So I just write the relevant relevant variables, signals and slots.
Maybe there is a typing error in in, but the principle works.
Please see the image above to get to know the structure and the goal.I hope that this helps.
Don't wonder about the QThread.... QObject is also fineProcessData.h: class ProcessData : public QThread { Q_OBJECT public: explicit ProcessData(QObject *parent=0); signals: sendPointer(SubProcessing1 *ptr); public slots: void triggerPointerSending() {emit sendPointer(ptrSubPro);} private: SubProcessing1 SubPro; SubProcessing1 *ptrSubPro; };
ProcessData.cpp: ProcessData::ProcessData(QObject *parent) : QThread(parent) { ptrSubPro = &SubPro}
GuiDialog.h private: SubProcessing1 *ptrSubProcessing; private slots: void receivePointer(SubProcessing*) {ptrSubProcessing = ptr}
MainWindow.h #include “subprocessing.h” private: ProcessData* myProcessedData SubProcessingDialog* mySubProcessingDialog private slots: void DEBUGSLOT(SubProcessing *ptr);
MainWindow.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); mySubProcessingDialog = new SubProcessing(); myProcessedData = new ProcessData(); QObject::connect(myProcessedDate, SIGNAL(sendPointer(SubProcessing1*)),mySubProcessingDialog, SLOT(receivePointer(SubProcessing1*))); myProcessedData.triggerPointerSending(); }