View Full Version : How to share Serial port opened in Main Window class with Dialog box class

31st July 2018, 08:30
Dear Developers,

In my Qt project I have two C++ classes named "MainWindow" and "DialogBox"

I used serial port class object of QT to open a serial port in MainWindow class and now I want to send and receive data from "DialogBox" class using same serial port opened in MainWindow class.

Please tell me what is correct approach to share same serial port object between MainWindow and DialogBox Class.

Kindly inform me if you need any other information from me.

Thank and Regards

Saurabh Jain

31st July 2018, 10:46
This problem has been solved. Using following answer received on other forum for Qt.

You could use signal and slot instead of sharing the actual QSerial object.
So define new slots in Mainwindow that forms your interface the dialog would use.
void SendCmd(QByteArray data);

and in dialog ,
define new signals for buttons to use
void SendCmd(QByteArray data); ( yes same name as the slot in main)

then hook up dialogs signals to mainwindows slot before u call exec() on dialog and
then u can use serialport from dialog.
by simply doing
emit SendCmd(xxxx)

in buttons click or where u need it.

Alternatively, just add a parameter to the Dialogs constructor and pass the
QSerialPort from mainwindow when you construct the Dialog.