Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is data received from serial port possible to view on two forms?
Forum Updated to NodeBB v4.3 + New Features

Is data received from serial port possible to view on two forms?

Scheduled Pinned Locked Moved Unsolved General and Desktop
34 Posts 6 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Shivam Sharma

    Here there is an error in connect, says: Setup doesn't refer to a value.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #8

    @Shivam-Sharma

    connect(this, &MainWindow:: newSerialData, Setup, &Setup:: onNewSerialData);

    Here there is an error in connect, says: Setup doesn't refer to a value.

    As the message states. The Setup as the 3rd parameter is a class. You need an instance (just like you have for the 1st parameter). See the docs.

    1 Reply Last reply
    0
    • S Shivam Sharma

      Here there is an error in connect, says: Setup doesn't refer to a value.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #9

      @Shivam-Sharma
      I said where you create Setup, you put the connect into the constructor of Setup

      here:

      void MainWindow::on_setupbutton_clicked()
      {
          setup = new Setup(this);
          setup->show();
          connect(this, &MainWindow::newSerialData, setup, &Setup::onNewSerialData);
      }
      

      but, every time you click on the setupButton you will create a new instance of that class, you're leaking memory


      Edit changed for error free connect, thanks to @JonB for mentioning it


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JonBJ S 2 Replies Last reply
      3
      • J.HilkJ J.Hilk

        @Shivam-Sharma
        I said where you create Setup, you put the connect into the constructor of Setup

        here:

        void MainWindow::on_setupbutton_clicked()
        {
            setup = new Setup(this);
            setup->show();
            connect(this, &MainWindow::newSerialData, setup, &Setup::onNewSerialData);
        }
        

        but, every time you click on the setupButton you will create a new instance of that class, you're leaking memory


        Edit changed for error free connect, thanks to @JonB for mentioning it

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #10

        @J-Hilk
        As per my previous, to help the user in this case you should change your 3rd param from Setup to setup anyway. Unless you want him to figure for himself (which would not be a bad idea, bit late now that I have spoken!) :)

        1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @Shivam-Sharma
          I said where you create Setup, you put the connect into the constructor of Setup

          here:

          void MainWindow::on_setupbutton_clicked()
          {
              setup = new Setup(this);
              setup->show();
              connect(this, &MainWindow::newSerialData, setup, &Setup::onNewSerialData);
          }
          

          but, every time you click on the setupButton you will create a new instance of that class, you're leaking memory


          Edit changed for error free connect, thanks to @JonB for mentioning it

          S Offline
          S Offline
          Shivam Sharma
          wrote on last edited by
          #11

          @J-Hilk everthing is error free except one, which I ain't undertsanding:

          'Setup' does not name a type.

          J.HilkJ JonBJ 2 Replies Last reply
          0
          • S Shivam Sharma

            @J-Hilk everthing is error free except one, which I ain't undertsanding:

            'Setup' does not name a type.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #12

            @Shivam-Sharma
            where ?
            It should point you to a line


            actually, remove the mainwindow.h include from setup
            here

            #include "setup.h"
            #include "ui_setup.h"
            #include <QSerialPort>
            #include <QDebug>
            #include <mainwindow.h>
            #include <QSerialPortInfo>
            #include <QFile>
            

            and here

            #ifndef SETUP_H
            #define SETUP_H
            #include <mainwindow.h>
            #include <QDialog>
            

            thats a circular include with your mainwindow.h were you include setup.h


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3
            • S Shivam Sharma

              @J-Hilk everthing is error free except one, which I ain't undertsanding:

              'Setup' does not name a type.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #13

              @Shivam-Sharma
              As @J-Hilk has said. And further to that: (unless a Qt expert contradicts me) you should never be including mainwindow.h into anything other than mainwindow.cpp and your main.cpp, not into any other UI stuff you define. Keeps it clean --- they should never need to know about your MainWindow class. This is true all over the place: a widget which accesses/opens a "child" widget should include that child's .h file, but the "child" widgets should never need to know about/include the .h file of other ("parent") widgets where they might be called from. At least usually.

              1 Reply Last reply
              2
              • S Offline
                S Offline
                Shivam Sharma
                wrote on last edited by
                #14
                This post is deleted!
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Shivam Sharma
                  wrote on last edited by
                  #15

                  @J-Hilk @JonB

                  Thank you so much guys. This really helped me. All the solutions that you'll constantly engaged me with really means a lot. Appreciate this.

                  1 Reply Last reply
                  2
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    Hi,

                    One small addition, when passing QString around like that (but it is also valid for other classes), it's a good habit to use const references. This allows to avoid creating unneeded copies.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • S Offline
                      S Offline
                      Shivam Sharma
                      wrote on last edited by
                      #17

                      @J-Hilk @JonB Hi guys. Need one more help. If i want to add a push button on the Setup form that connects and disconnects the serial data incoming on both the forms how could I do that.

                      This is the code for the push button, i can do it on the mainwindow form but unable to do it on the Setup form since it is not being able to access *serial.

                      Code:

                      void MainWindow::on_start_toggled(bool checked)
                      {
                          ui->start->setText(ui->start->isChecked() ? "Connect" : "Disconnect");
                          if (checked)
                          {
                              serial->close();
                              ui->start->setStyleSheet(QString("QPushButton {background-color: rgb(115, 210, 22);}"));
                          }
                          else
                          {
                              ui->start->setStyleSheet(QString("QPushButton {background-color: red;}"));
                      
                      
                                 serial = new QSerialPort(this);
                                 serial = new QSerialPort(this);
                                 serial->setPortName("ttyUSB0");
                                 serial->setBaudRate(QSerialPort::Baud9600);
                                 serial->setDataBits(QSerialPort::Data8);
                                 serial->setParity(QSerialPort::NoParity);
                                 serial->setStopBits(QSerialPort::OneStop);
                                 serial->setFlowControl(QSerialPort::NoFlowControl);
                      
                                 if( serial->open(QIODevice::ReadWrite))
                                  {
                                      qDebug()<<"Port Open OK";
                                      connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                                  }
                                else
                                  {
                                      qDebug()<<"Can't Open Port : " << serial->error();
                                  }
                                 serialReceived();
                          }
                      }
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • S Shivam Sharma

                        @J-Hilk @JonB Hi guys. Need one more help. If i want to add a push button on the Setup form that connects and disconnects the serial data incoming on both the forms how could I do that.

                        This is the code for the push button, i can do it on the mainwindow form but unable to do it on the Setup form since it is not being able to access *serial.

                        Code:

                        void MainWindow::on_start_toggled(bool checked)
                        {
                            ui->start->setText(ui->start->isChecked() ? "Connect" : "Disconnect");
                            if (checked)
                            {
                                serial->close();
                                ui->start->setStyleSheet(QString("QPushButton {background-color: rgb(115, 210, 22);}"));
                            }
                            else
                            {
                                ui->start->setStyleSheet(QString("QPushButton {background-color: red;}"));
                        
                        
                                   serial = new QSerialPort(this);
                                   serial = new QSerialPort(this);
                                   serial->setPortName("ttyUSB0");
                                   serial->setBaudRate(QSerialPort::Baud9600);
                                   serial->setDataBits(QSerialPort::Data8);
                                   serial->setParity(QSerialPort::NoParity);
                                   serial->setStopBits(QSerialPort::OneStop);
                                   serial->setFlowControl(QSerialPort::NoFlowControl);
                        
                                   if( serial->open(QIODevice::ReadWrite))
                                    {
                                        qDebug()<<"Port Open OK";
                                        connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                                    }
                                  else
                                    {
                                        qDebug()<<"Can't Open Port : " << serial->error();
                                    }
                                   serialReceived();
                            }
                        }
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #18

                        @Shivam-Sharma Create a signal in Setup, connect that signal to clicked() signal of the button in Setup and then connect the signal you created in Setup (the first one I mentioned) to a slot in MainWindow where you can enable/disable the serial port.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • S Offline
                          S Offline
                          Shivam Sharma
                          wrote on last edited by
                          #19

                          I am sorry, but unable to understand. How do i connect the pushbutton to mainwindow from Setup form, since i cannot inherit the parent class, neither can i create an instance of it. Could you give a demo?

                          jsulmJ 1 Reply Last reply
                          0
                          • S Shivam Sharma

                            I am sorry, but unable to understand. How do i connect the pushbutton to mainwindow from Setup form, since i cannot inherit the parent class, neither can i create an instance of it. Could you give a demo?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #20

                            @Shivam-Sharma said in Is data received from serial port possible to view on two forms?:

                            How do i connect the pushbutton to mainwindow from Setup form

                            You don't, please read more carefully.

                            class Setup
                            {
                            signals:
                                void onOffSerialPort();
                            };
                            
                            void Setup::Setup()
                            {
                                connect(ui->button, &QPushButton::clicked, this, &Setup::onOffSerialButton);
                            }
                            
                            // MainWindow
                            connect(setup, &Setup::onOffSerialButton, this, &MainWindow::onOffSerialButton);
                            

                            As you can see Setup does not know anything about MainWindow.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            2
                            • S Offline
                              S Offline
                              Shivam Sharma
                              wrote on last edited by
                              #21
                              // MainWindow
                              connect(setup, &Setup::onOffSerialButton, this, &MainWindow::onOffSerialButton);
                              
                              

                              shouldn't all of this be

                              onOffSerialPort
                              

                              and the last part be :

                              &MainWindow::close);
                              
                              void Mainwindow::close()
                              {
                              serial->close();
                              }
                              jsulmJ 1 Reply Last reply
                              0
                              • S Shivam Sharma
                                // MainWindow
                                connect(setup, &Setup::onOffSerialButton, this, &MainWindow::onOffSerialButton);
                                
                                

                                shouldn't all of this be

                                onOffSerialPort
                                

                                and the last part be :

                                &MainWindow::close);
                                
                                void Mainwindow::close()
                                {
                                serial->close();
                                }
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #22

                                @Shivam-Sharma Sorry, I don't understand what you mean. The code I provided is just to show the idea, you need to adapt it to your needs.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #23

                                  let me expand on @jsulm example

                                  first of in your mainwindow, create a new function, defined as a slot

                                  and copy the content of your on button clicked in there

                                  public slots:
                                       void toggleSerial(bool checked);
                                  
                                  //mainwindow.cpp
                                  void MainWindow:: toggleSerial(bool checked)
                                  {
                                        ui->start->setText(ui->start->isChecked() ? "Connect" : "Disconnect");
                                            if (checked)
                                            {
                                                serial->close();
                                                ui->start->setStyleSheet(QString("QPushButton {background-color: rgb(115, 210, 22);}"));
                                            }
                                            else
                                            {
                                                ui->start->setStyleSheet(QString("QPushButton {background-color: red;}"));
                                        
                                        
                                                   serial = new QSerialPort(this);
                                                   serial = new QSerialPort(this);
                                                   serial->setPortName("ttyUSB0");
                                                   serial->setBaudRate(QSerialPort::Baud9600);
                                                   serial->setDataBits(QSerialPort::Data8);
                                                   serial->setParity(QSerialPort::NoParity);
                                                   serial->setStopBits(QSerialPort::OneStop);
                                                   serial->setFlowControl(QSerialPort::NoFlowControl);
                                        
                                                   if( serial->open(QIODevice::ReadWrite))
                                                    {
                                                        qDebug()<<"Port Open OK";
                                                        connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                                                    }
                                                  else
                                                    {
                                                        qDebug()<<"Can't Open Port : " << serial->error();
                                                    }
                                                   serialReceived();
                                            }
                                  }
                                  void MainWindow::on_start_toggled(bool checked)
                                  {
                                      toggleSerial(checked);
                                  }
                                  

                                  inside Setup add the signal like @jsulm suggested

                                  signals:
                                      void onOffSerialPort(bool checked);
                                  

                                  connect your button toggle signal (in Setup) to this signal (constructor of Setup

                                  connect(ui->myButton, &QPushButton:: toggled, this, &Setup:: onOffSerialPort);
                                  

                                  and finally
                                  connect that onOffSerialPort signal to your toggleSerial slot

                                  connect(setup, &Setup:: onOffSerialPort, this, &MainWindow:: toggleSerial);
                                  

                                  one thing to keep in mind, your 2 buttons will now not necessarily show the correct checked status.

                                  I'll let you figure that one out.


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  2
                                  • S Offline
                                    S Offline
                                    Shivam Sharma
                                    wrote on last edited by
                                    #24

                                    Error: no member named start in ui::mainwindow,

                                    that is because it isn't able to access the pushbutton from setup window. Help please.

                                    Also:
                                    void MainWindow::on_start_toggled(bool checked)
                                    {
                                    toggleSerial(checked);
                                    }
                                    this also has errors saying: on_start_toggled doesn't match any declaration.

                                    J.HilkJ jsulmJ JonBJ 3 Replies Last reply
                                    0
                                    • S Shivam Sharma

                                      Error: no member named start in ui::mainwindow,

                                      that is because it isn't able to access the pushbutton from setup window. Help please.

                                      Also:
                                      void MainWindow::on_start_toggled(bool checked)
                                      {
                                      toggleSerial(checked);
                                      }
                                      this also has errors saying: on_start_toggled doesn't match any declaration.

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #25

                                      @Shivam-Sharma
                                      where did you put this ?

                                      public slots:
                                           void toggleSerial(bool checked);
                                      

                                      in mainwindow.h or on setup.h ?


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        Shivam Sharma
                                        wrote on last edited by
                                        #26

                                        in mainwindow.h

                                        1 Reply Last reply
                                        0
                                        • S Shivam Sharma

                                          Error: no member named start in ui::mainwindow,

                                          that is because it isn't able to access the pushbutton from setup window. Help please.

                                          Also:
                                          void MainWindow::on_start_toggled(bool checked)
                                          {
                                          toggleSerial(checked);
                                          }
                                          this also has errors saying: on_start_toggled doesn't match any declaration.

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #27

                                          @Shivam-Sharma said in Is data received from serial port possible to view on two forms?:

                                          that is because it isn't able to access the pushbutton from setup window

                                          I repeat it once more: YOU DO NOT HAVE TO ACCESS THIS BUTTON FROM MAIN WINDOW!
                                          Please show your current code.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved