Myself trying to write a program in Qt connecting a function to a button in Qt5.
#include <QApplication>
#include <QtGui>
#include <QPushButton>
static void insert()
{
qDebug() << “pressed”;
}
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QPushButton *button=new QPushButton(“button”);
button->setGeometry(50,100,150,80);
QObject::connect(button,&QPushButton::clicked,insert());
button->show();
}
But I am getting errors like main.cc:23:39: error: within this context main.cc:23:55: error: invalid use of void expression make: * [main.o] Error 1
Please help…
In Qt 5, you need to use the new qt signal and slots system. The connection will look like :
QObject::connect(button,&QPushButton::clicked,insert); <-- no parentheses.
It has already been stated, but you need to call app.exec();
to start the event loop processing. Otherwise the connection will never be triggered.
Furthermore, if you are on release mode then you may not see the output of qDebug()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With