Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting button to an arbitrary function

Tags:

c++

qt

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…

like image 642
sarah john Avatar asked May 24 '13 05:05

sarah john


1 Answers

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()

like image 152
UmNyobe Avatar answered Oct 04 '22 04:10

UmNyobe