Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Connecting a QPushButton and a function



  • I'm creating a QPushButton, putting it on the form and then connecting it so that when it gets clicked, a function is called. This is what I have in the .h file:
    @
    private:
    QPushButton *manAutoButton;

    private slots:
    void on_manAutoButton_clicked();
    @
    And this is what's in the .cpp file:
    @
    manAutoButton = new QPushButton(this);
    manAutoButton->resize(84, 84);
    manAutoButton->move(629, 270);
    connect(manAutoButton, SIGNAL(clicked()), this, SLOT(on_manAutoButton_clicked()));
    @
    When the app runs, everything is fine. The function gets called whenever I click the manAutoButton. The only thing is that I get the following message in the run window: "QMetaObject::connectSlotsByName: No matching signal for on_manAutoButton_clicked()". What am I doing wrong?

    EDIT: moved to General, peppe



  • You have to create the on_manAutoButton_clicked() function in your .cpp file.



  • Yes, it was already in there. For completeness, this is it:
    @
    void MonitorWindow::on_manAutoButton_clicked()
    {
    if(mode == Auto)
    {
    mode = Manual;
    ui->label_28->setText("MAN");
    manAutoButton->setIcon(QIcon(QPixmap(":/MonitorWindowPix/Automatic1.png")));
    }
    }
    @



  • Try to remove on_ from slot name.

    @private slots:
    void manAutoButton_clicked();

    ...

    connect(manAutoButton, SIGNAL(clicked()), this, SLOT(manAutoButton_clicked()));

    ...

    void MonitorWindow::manAutoButton_clicked()
    {@



  • Thanks - that did it. I guess the buttons that are placed on Designer UI forms are automatically connected to the on_buttonname_clicked functions, but creating your own functions in that format creates some confusion.


Log in to reply