0

I tried a very simple program in QT. I created a dialog in QT designer with one push button. I want to have program, when I click on push button, I will get message box. I debugged the program. But, signal will not come to function OnClickedPushButton(bool) after clicking on push button. Where is a mistake? My code is looking like:

#include "qttest.h"
#include <QtGui>
#include <QMessageBox>

QTTest::QTTest(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(OnClickedPushButton()));

    QPushButton *button = new QPushButton(this);
    button->setText("ClcikMe");
    button->setGeometry(QRect(QPoint(150, 50), QSize(85, 25)));
    connect(button, SIGNAL(clicked()), this, SLOT(OnClickedPushButton()));
}

QTTest::~QTTest()
{

}

void QTTest::OnClickedPushButton()
{
    QMessageBox::about(this, "Message", "You pushed button.");
}

I can build it, and I can launch it. But, debugger writes me in the constructor following messages:

QObject::connect: No such slot QTTest::OnClickedPushButton() in qttest.cpp:9
QObject::connect:  (sender name:   'pushButton')
QObject::connect:  (receiver name: 'QTTestClass')
QObject::connect: No such slot QTTest::OnClickedPushButton() in qttest.cpp:14
QObject::connect:  (receiver name: 'QTTestClass')

I can see both buttons at the window. pushButton and button. But, if I will click on button the message will not come due to wrong slot mapping. Do you have any idea how to do it correctly?

2
  • In your header file OnClickedPushButton is declared within public: section. Declare public slots: before OnClickedPushButton declaration Commented Apr 23, 2017 at 8:01
  • That was the problem! Thanks a lot. Commented Apr 23, 2017 at 9:18

4 Answers 4

2

There are two possibilities here: You either declare OnClickedPushButton as a slot, as mentioned by Avi, or you use the new signal/slot connection syntax. With the new syntax you'd connect the signal to an arbitrary function like this:

connect(pushButton, &QPushButton::clicked, this, &QTTest::OnClickedPushButton);

The major advantage this has over the old syntax is that it is checked at compile time and you can't end up with runtime errors like the ones you currently got.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to declare the 'OnClickedPushButton' as SLOT in your header file. Compiler cannot find the specified function in slots list in your code.

Comments

0

If you have already a mainwindow created while project creation then you do not have to create the pushbutton manually. You can open the MainWindow using the designer, drag and drop the pushbutton and then right click on the button, select "Go to slot" option, this will open the source code. The sample source is here,

#include "qttest.h"
#include "ui_qttest.h"
#include <QMessageBox>

QTTest::QTTest(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::QTTest)
{
    ui->setupUi(this);
}

QTTest::~QTTest()
{
    delete ui;
}

void QTTest::on_pushButton_clicked()
{
    QMessageBox::about(this, "Message", "You pushed button.");
}

Also please paste your header code.

Comments

-2

try this connect(ui.pushButton, SIGNAL(release()), this, SLOT(OnClickedPushButton(bool))); or you can use SIGNAL(clicked()) without bool. I didn't test it yet but seem work for you.

4 Comments

I tried already. The signals do not come to OnClickedPushButton() function for some reasons. I do not know why.
could you try this first QPushButton *button = new QPushButton(this); button->setText("ClcikMe"); button->setGeometry(QRect(QPoint(150, 50), QSize(85, 25))); connect(button, SIGNAL(clicked()), this, SLOT(OnClickedPushButton(bool))); i think is from ui.pushButton.
By debugger I have got this message:QObject::connect: No such slot QTTest::OnClickedPushButton() in qttest.cpp:10 QObject::connect: (sender name: 'pushButton') QObject::connect: (receiver name: 'QTTestClass')
Oh so try to use void QTTest::OnClickedPushButton() without bool bClicked and remove bool from SLOT(OnClickedPushButton(bool)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.