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. passing values in a connect statement
Forum Updated to NodeBB v4.3 + New Features

passing values in a connect statement

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 996 Views 1 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.
  • M Offline
    M Offline
    mar0029
    wrote on last edited by
    #1

    Hello!

    I had a question concerning passing values by value or reference.

    if my function prototype in my header file looks like this:

    ...
    void loadButton(QString&);
    ...
    

    and my function definition looks like:

    ...
    void MainWindow::loadButton(QString& H1);
    

    and my function call looks like:

    ...
    connect(ui->loadButton, SIGNAL(clicked(bool)), this, SLOT(loadButton(H1);
    ...
    

    Why do I keep getting errors like this:

    ...
    QObject::connect: No such slot MainWindow::loadButton(H1) in ..\where\my\files\are
    QObject::connect:  (sender name:   'loadButton')
    QObject::connect:  (receiver name: 'MainWindow')
    ...
    

    Any help would be appreciated.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @mar0029 said:
      hi
      something odd here.
      The button clicked do not have a bool(normally) and it cannot call a random function with a string ref.

      your loadButton(QString& H1); must be a slot
      which means it should be listed under
      public slots: in .h file as
      void loadButton();

      what did you expect the H1 to be ?

      so the working way would be
      button clicked call your slot function MybuttonClicked
      MybuttonClicked calls the
      loadButton( someQstring)

      maybe read
      http://doc.qt.io/qt-5.5/signalsandslots.html
      http://www.bogotobogo.com/Qt/Qt5_SignalsSlotsGui.php

      M 1 Reply Last reply
      0
      • mrjjM mrjj

        @mar0029 said:
        hi
        something odd here.
        The button clicked do not have a bool(normally) and it cannot call a random function with a string ref.

        your loadButton(QString& H1); must be a slot
        which means it should be listed under
        public slots: in .h file as
        void loadButton();

        what did you expect the H1 to be ?

        so the working way would be
        button clicked call your slot function MybuttonClicked
        MybuttonClicked calls the
        loadButton( someQstring)

        maybe read
        http://doc.qt.io/qt-5.5/signalsandslots.html
        http://www.bogotobogo.com/Qt/Qt5_SignalsSlotsGui.php

        M Offline
        M Offline
        mar0029
        wrote on last edited by
        #3

        @mrjj

        Thank you for your reply!

        I've started to write a response about ten times and then I would get to thinking and then I'd have another problem/question. But I think I have made it to the base question:

        How can I pass variables between Public: functions in Qt?

        In a single file C++ program, I would define the function prototype before main(), and depending on style, I would provide the function definition immediately following the prototype or after main(). Function calls would be made inside main() and any variables that I wanted to share between more than one function would be declared in main() and passed by value or reference between functions.

        How does one pass the variable back and forth if there is no main() to declare it in?

        Sorry for sounding ignorant of Qt's ways but I'm still learning.

        mrjjM 1 Reply Last reply
        0
        • M mar0029

          @mrjj

          Thank you for your reply!

          I've started to write a response about ten times and then I would get to thinking and then I'd have another problem/question. But I think I have made it to the base question:

          How can I pass variables between Public: functions in Qt?

          In a single file C++ program, I would define the function prototype before main(), and depending on style, I would provide the function definition immediately following the prototype or after main(). Function calls would be made inside main() and any variables that I wanted to share between more than one function would be declared in main() and passed by value or reference between functions.

          How does one pass the variable back and forth if there is no main() to declare it in?

          Sorry for sounding ignorant of Qt's ways but I'm still learning.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @mar0029
          Hi
          Ok. It seems your not so used to objects and classes ?

          In Qt the mainwindow is class.
          You could store variables in the .h so they are members of this class.
          All functions in QMainwindow could then use this data and you can also give it to other classes.

          Often one will create own classes for data and use them.
          so variables you normally would put before main, you could put in a class and and put instance of that class to MainWindow;

          So lets take a small example.
          normally a QMainWindow looks like this

          class MainWindow : public QMainWindow
          {
             Q_OBJECT
          public:
             explicit MainWindow(QWidget *parent = 0); // this is special function. a constructor. it creates the class
             ~MainWindow(); // this is called when deleted
          
          private: 
             Ui::MainWindow *ui;
          };
          

          say we want to store a string and and a number we could add it to QMainwindow like this

          class MainWindow : public QMainWindow
          {
             Q_OBJECT
          
          public:
             explicit MainWindow(QWidget *parent = 0);
             ~MainWindow();
          
          private:
             Ui::MainWindow *ui;
          public:
          QString Name;
          int Age;
          };
          

          Then all functions that are in QMainWindow can use them.
          Normally , however, one would make classes so it dont get messy so one might do this instead:

          
          class Person {
          public:
          QString Name;
          int Age;
          };
          
          class MainWindow : public QMainWindow
          {
             Q_OBJECT
          
          public:
             explicit MainWindow(QWidget *parent = 0);
             ~MainWindow();
          
          private:
             Ui::MainWindow *ui;
          public:
          Person OnePerson;
          };
          

          then its also easier to make a list of persons. notice we first declare the type Person and then have an instance of it.
          this is basically the same:
          int Number;
          Person OnePerson;
          but person contains members you can set like
          OnePerson.Name="john";

          So the reason why it feels strange for you ,
          is that its an object oriented programming style and main is no longer
          the center of it. An object will be.
          So you data / variables etc,, best live in a class.
          And if you want to feel more at home with Qt, then you first play around with classes first to see how that works.
          Might read abit here
          http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm

          sorry if not detailed enough :)

          1 Reply Last reply
          1
          • Hamed.MasafiH Offline
            Hamed.MasafiH Offline
            Hamed.Masafi
            wrote on last edited by
            #5

            Thst's impossible. But in c++11 you can write a code like this:

            connect(ui->loadButton, &QAbstractButton::clicked, this, [this] () (bool){
                loadButton(H1);
            });
            

            Remote object sharing (OO RPC)
            http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

            Advanced, Powerful and easy to use ORM for Qt5
            https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

            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