简体   繁体   English

QT5连接信号功能

[英]QT5 connect signal to function

So, I'm looking to make a button that becomes flat when pressed in QT5. 因此,我想在QT5中按下按钮使其变平。 I've read this 我读过这个
https://woboq.com/blog/new-signals-slots-syntax-in-qt5.html https://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
and it seems that I should be able to do this without making my own button class. 似乎我应该能够在不创建自己的按钮类的情况下完成此操作。 So, I've got 所以,我有

QPushButton* button = new QPushButton("text", parent); QObject::connect(button, &QPushButton::clicked, button, &QPushButton::isFlat(true));
and I'm getting 而且我正在
error: call to non-static member function without an object argument
My questions are; 我的问题是; am I reading this new syntax wrong? 我读这个新语法错了吗? Can I only connect to static functions? 我可以只连接静态功能吗?

You are trying to connect a method of no instance, use a lambda for example for capture the button instance: 您正在尝试连接无实例的方法,例如使用lambda来捕获按钮实例:

QObject::connect(button, &QPushButton::clicked, button, 
                 [&button]() {button->setFlat(true)});

Not tested. 没有测试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM