将按钮连接到任意功能 [英] Connecting button to an arbitrary function

查看:109
本文介绍了将按钮连接到任意功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己试图在Qt中编写一个程序,将函数连接到Qt5中的一个按钮。

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

但我得到的错误如
main.cc:23: 39:错误:在此上下文中
main.cc:23:55:错误:无效使用void表达式
make: * [main.o]错误1

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

请帮助...

推荐答案

在Qt 5中, qt信号和插槽系统。连接将如下所示:

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.

它已经说明,但你需要调用 app.exec ); 开始事件循环处理。

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.

此外,如果您处于发布模式,那么您可能看不到 qDebug()

Furthermore, if you are on release mode then you may not see the output of qDebug()

这篇关于将按钮连接到任意功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆