Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to send variable to another form
Qt 6.12 LTS is out! See what's new in the release blog

Unable to send variable to another form

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 865 Views
  • 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.
  • V Offline
    V Offline
    VJ01
    wrote on last edited by VJ01
    #1

    Hi

    I’m still new to QT and have just started using slots and signals. I’m trying to send a variable from one form to another, and then the other way back, however I wanted to attempt to do it one way first, however I’m getting an error which no matter what I try it won’t go away. I have two forms MainWindow and Settings my code is below

    Mainwindow.h

    signals:
        void SendData(int speed_check_mainform);
    

    Settings.h

    private slots:
        void getData(int speed_check_mainform);
    

    Settings.cpp

    void Settings::getData(int speed_check_mainform){
        qDebug() << speed_check_mainform;
    }
    

    MainWindow.cpp

    connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));
    

    in the MainWindow.cpp is where i am getiing the error

    expected primary-expression before ',' token
    

    I’m not sure if it is something simple or not, I have looked at many examples online and I always get the same error from the connect, I have put it in the main() however still nothing. I of course know that I need to emit when using the signal but I just can’t seem to get passed this error. if anyone could help that would be great

    Thank you

    JonBJ 1 Reply Last reply
    0
    • V VJ01

      Hi

      I’m still new to QT and have just started using slots and signals. I’m trying to send a variable from one form to another, and then the other way back, however I wanted to attempt to do it one way first, however I’m getting an error which no matter what I try it won’t go away. I have two forms MainWindow and Settings my code is below

      Mainwindow.h

      signals:
          void SendData(int speed_check_mainform);
      

      Settings.h

      private slots:
          void getData(int speed_check_mainform);
      

      Settings.cpp

      void Settings::getData(int speed_check_mainform){
          qDebug() << speed_check_mainform;
      }
      

      MainWindow.cpp

      connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));
      

      in the MainWindow.cpp is where i am getiing the error

      expected primary-expression before ',' token
      

      I’m not sure if it is something simple or not, I have looked at many examples online and I always get the same error from the connect, I have put it in the main() however still nothing. I of course know that I need to emit when using the signal but I just can’t seem to get passed this error. if anyone could help that would be great

      Thank you

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

      @VJ01 said in Unable to send variable to another form:

      connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));

      At a guess, from your Settings.cpp extract void Settings::getData() it looks like Settings is a class name. For signal/slot you must connect instances (presumably MainWindow is one?), what is your Settings instance's variable?

      Next, given you have declared the getData() slot as private in Settings.h obviously you are not going to be able to access it on a Settings instance anyway, only from within Settings class. If you want to connect() to it from the outside world/on an instance don't make it private, that is what private is there to prevent!

      You should not be using SIGNAL/SLOT() macros for connect(), they are old-style from many years ago. You should be using new style, the error messages may be more helpful.

      V 1 Reply Last reply
      2
      • JonBJ JonB

        @VJ01 said in Unable to send variable to another form:

        connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));

        At a guess, from your Settings.cpp extract void Settings::getData() it looks like Settings is a class name. For signal/slot you must connect instances (presumably MainWindow is one?), what is your Settings instance's variable?

        Next, given you have declared the getData() slot as private in Settings.h obviously you are not going to be able to access it on a Settings instance anyway, only from within Settings class. If you want to connect() to it from the outside world/on an instance don't make it private, that is what private is there to prevent!

        You should not be using SIGNAL/SLOT() macros for connect(), they are old-style from many years ago. You should be using new style, the error messages may be more helpful.

        V Offline
        V Offline
        VJ01
        wrote on last edited by
        #3

        @JonB hi thank you for the reply i tried to implement it a different way

        MainWindow.cpp

        void MainWindow::on_actionSettings_triggered()
        {
         qDebug() << "Settings";
         Settings *dlg = new Settings();
             connect(this, SIGNAL(sendData(int)), dlg, SLOT(setData(int)));
             emit sendData(35);
         settings.show();
        }
        

        Settings.cpp

        void Settings::setData(int data)
        {
         printf("BLAH BLAH BLAH %d\n", data);
        ui->lineEdit->setText(data);
        }
        

        my printf function does show that data is 35 however i cant seem to get the line Edit to work

        Thank you

        jsulmJ 1 Reply Last reply
        0
        • V VJ01

          @JonB hi thank you for the reply i tried to implement it a different way

          MainWindow.cpp

          void MainWindow::on_actionSettings_triggered()
          {
           qDebug() << "Settings";
           Settings *dlg = new Settings();
               connect(this, SIGNAL(sendData(int)), dlg, SLOT(setData(int)));
               emit sendData(35);
           settings.show();
          }
          

          Settings.cpp

          void Settings::setData(int data)
          {
           printf("BLAH BLAH BLAH %d\n", data);
          ui->lineEdit->setText(data);
          }
          

          my printf function does show that data is 35 however i cant seem to get the line Edit to work

          Thank you

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

          @VJ01 said in Unable to send variable to another form:

          however i cant seem to get the line Edit to work

          Because setText expects a QString as you can clearly see in the documentation: https://doc.qt.io/qt-6/qlineedit.html#text-prop
          So, convert the int to QString: https://doc.qt.io/qt-6/qstring.html#number-1

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

          V 1 Reply Last reply
          0
          • jsulmJ jsulm

            @VJ01 said in Unable to send variable to another form:

            however i cant seem to get the line Edit to work

            Because setText expects a QString as you can clearly see in the documentation: https://doc.qt.io/qt-6/qlineedit.html#text-prop
            So, convert the int to QString: https://doc.qt.io/qt-6/qstring.html#number-1

            V Offline
            V Offline
            VJ01
            wrote on last edited by
            #5

            @jsulm

            sorry about that i pasted the wrong code

            void Settings::setData(int data)
            {
             printf("BLAH BLAH BLAH %d\n", data);
             ui->lineEdit->setText(QString::number(data));
            }
            

            this is the code im using but has the same effect and nothing happens
            never mind i was just being really thick i should have done this in my MainWindow.cpp

             dlg->show();
            
            1 Reply Last reply
            1

            • Login

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