PDA

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



saurabh162
31st July 2018, 09: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

saurabh162
31st July 2018, 11: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.
http://doc.qt.io/qt-5/signalsandslots.html
So define new slots in Mainwindow that forms your interface the dialog would use.
Like
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.