Reputation:
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?
Upvotes: 0
Views: 6802
Reputation: 5510
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.
Upvotes: 2
Reputation: 2069
You need to declare the 'OnClickedPushButton' as SLOT in your header file. Compiler cannot find the specified function in slots list in your code.
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 80
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.
Upvotes: -2