Solved Connecting 2 different classes together
-
@GunkutA said in Connecting 2 different classes together:
Because I was planning to know button presses from GUI ( which is protocolForm) in the USB class. Then I would connect to the USB port.
That is exactly what signals and slots are for. Your
USB
class does not need to know anything about any UI. It will simply have some slot, like for exampleonButtonPressed()
orsomethingHappened()
and it will respond to such event (signal). It does not need to know any more details. And if it does, then you can send the necessary information as argument in your signal and slot.Then, if
USB
needs to inform the UI about something, it emits a signal as well. -
protocolFormInstance = new protocolForm; // protocolFormInstance = new protocolForm(); connect(this,&USB::sendCOMPort(QString),protocolFormInstance , &protocolForm::receiveCOMPort(QString));
This is all in the wrong place/wrong way round. The
USB
class handles serial/USB activity. It should know nothing aboutprotocolForm
, it should notnew
it. As I wrote above, theconnect()
belongs in theprotocolForm
class.You need to stop typing/commenting out stuff and take time to understand the necessary approach correctly. I leave this to you/ @sierdzio now, as I have tried to explain and give examples of all I can....
-
@sierdzio Well the thing is, it needs to know the selected COM Port from the GUI. So there will be 3 COM Ports for example and when the button pressed, USB class will know the which COM Port is selected and then it will connect to it. So I believe I need to send COM Port info from GUI to USB class.
-
@GunkutA said in Connecting 2 different classes together:
@sierdzio Well the thing is, it needs to know the selected COM Port from the GUI. So there will be 3 COM Ports for example and when the button pressed, USB class will know the which COM Port is selected and then it will connect to it. So I believe I need to send COM Port info from GUI to USB class.
Yes, do it by argument. USB class does not need to know anything more. So, a minimal example:
// UI UiClass : public QWidget { Q_OBJECT USB *usb = nullptr; signals: void openPort(const int port) const; public: UiClass(QWidget *parent = nullptr) : public QWidget(parent) { usb = new USB; connect(this, &UiClass::openPort, usb, &USB::onOpenPort); } }; // USB class class USB : public QObject { Q_OBJECT public slots: void onOpenPort(const int port) { // Do something with the port } };
-
@sierdzio I got it, thank you for all the effort.