passing values in a connect statement



  • 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.


  • Qt Champions 2016

    @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



  • @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.


  • Qt Champions 2016

    @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 :)



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

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

Log in to reply
 

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