How to share Serial port object between two classes

  • Dear Developers,

    How to share a Serial port between MainWindow(QWidget *parent) and Dialog Class ?

    In my application I have one MainWindow class and another Dialog Box (Dialog Class).

    I open Dialog Box from MainWindow by pressing a button.

    Additionally In order to communicate with hardware I open Serial port in my MainWindow.

    My question is what is correct method to use same serial port(opened in MainWindow) to send commands to hardware, for example if i press some button on dialog box ?

    In other words what is right way to share a serial port object between two classes MainWindow and Dialog Class ?

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

    Thank a lot !!



  • Lifetime Qt Champion

    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.

  • I suggest signals and slots as your first approach.

    You probably want to create a new custom Dialog class which inherits Qts class, this way you can define your new custom signals and emit them when you need to.

    Just as @mrjj said you could pass a pointer to the constructor but again you will need to declare a new custom class for the custom constructor behavior.

    The signals and slots is probably your cleanest approach because you are not passing pointers around and the new custom class would not depend on the serial port.

    Good luck

  • Thank you very much mrjj and MrShawn I will try signal and slot approach and will update you.


  • Hello mrjj and MrShawn,

    signals and slot approach has solved my problem cleanly.

    Thank you very much for help :)

    kind regards
    Saurabh Jain

  • Hello Developers,

    As mentioned above I was to send command using signals and slot approach to main window.

    Now I want also to read data received from serial port in MainWindow from Dialog box.

    I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

    thanks you very much :)

  • Lifetime Qt Champion

    You mean after serial have read all in main, you want to
    send it to a dialog you open ?
    Or do you mean that dialog all reads from serial while data is coming in ?

  • Qt Champions 2018

    @saurabh162 Add a getter method to your dialog:

    Dialog dialog(this);
    MyData data =;

  • @mrjj

    Hello Mrji,

    thank you for fast reply

    Yes, I my application data received from serial port is first stored in buffer in Main Window. I want read this saved data from Dialog box.

    In dialog box I am sending serial port commands to Main Window using signal and slot. But do not know what is right way to read serial data saved in Main window from Dialog Box.

  • @jsulm

    Hello jsulm

    Thank you for your fast response.

    I am sorry but i could not understand your solution. I have serial data collected in Main window. This i want to read inside Dialog box.

    So can you please explain your answer in little details

    Thank you :)

  • Qt Champions 2018

    @saurabh162 said in How to share Serial port object between two classes:

    I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?

    Where have you read this? You can surely use signals&slots to exchange data. That's what signals&slots are all about :)

  • Qt Champions 2018

    @saurabh162 You should explain better what you want to do.
    Pass the data from main window to dialog via constructor:

    Dialog dialog(this, mySerialData);

  • @aha_1980

    Thank you aha for reply

    I have read it in following posts

    According to above posts we should be cautious when we are returning data from a slot.

    Additionally I have tried it and got segmentation fault. Following are the steps I have taken.

    1. I have a array " char receive_buffer[RBSIZE]" declared in Main window class with private access. This buffer is always filled with new data when we send serial command from Dialog Box using signal and slot.
    2. In order to read this buffer, I declared char pointer in my Dialog Box and intialize this pointer to address of "receive_buffer" in Serial communication slot in Main Window.
    3. After this when I try to read data in receive_buffer[RBSIZE] from Dialog Box using char pointer I get segementation fault.

    Please let me know if you need any other information.

    thank you :)

  • Qt Champions 2018


    Yes, thats bad design. But you don't need to do it like this. You can emit another signal from your dialog when data is ready, and connect it to a slot in your main window.


  • @aha_1980

    ok aha_1980 I try this and will update you ..thanks :)

  • hello aha_1980,

    Thank you very much it worked.

    I have taken following steps to achieve it. It is working but I also want to ensure whether it is correct way of programming.

    1. I have created signal (in Dialog Box) and slot (in main window) with same name
      "void ReadSerialBuffer(void);"

    2. Declared a array char serialReceivebuffer[RBSIZE]; in dialog box. Additionally a public function "UpdateSerialReadBuffer"in Dialog box to copy data from source buffer to serialReceivebuffer in Dialog Box.

    3. After this I defined slot in main window which is calling "UpdateSerialReadBuffer" function of dialog box to copy data from buffer in Main window to buffer in Dialog box.

    4. In end I connected both signal and slot using "connect" method

    Thank you very much :)

  • @saurabh162

    I also came across a similar situation. I'm a beginner of Qt and c++. It will be very helpful if you could share a sample code to communicate the serial port object in two forms.

    many thanks

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.