我尝试了使用可调用对象作为回调的新 Qt5 功能。
#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QBoxLayout>
#include <cstdio>
class ButtonEventHandler
{
public:
ButtonEventHandler(const ButtonEventHandler& obj):
counter(obj.counter),r_button(obj.r_button)
{
printf("Copy from %p -> %p\n",&obj,this);
}
ButtonEventHandler& operator=(const ButtonEventHandler& obj)
{
r_button=obj.r_button;
counter=obj.counter;
printf("Assign from %p -> %p\n",&obj,this);
return *this;
}
explicit ButtonEventHandler(QPushButton& button):r_button(&button)
{
printf("CTOR %p\n",this);
counter=0;
}
void operator()(bool checked) noexcept
{
char buffer[32];
printf("%p counter:%zu\n",this,counter);
sprintf(buffer,"%zu",counter);
r_button->setText(buffer);
++counter;
}
private:
size_t counter;
QPushButton* r_button;
};
int main(int argc,char** argv)
{
QApplication app(argc,argv);
QWidget window;
QHBoxLayout box;
window.setLayout(&box);
window.setFixedSize(400,300);
QPushButton button1("Click me");
ButtonEventHandler eh1(button1);
QObject::connect(&button1,&QPushButton::clicked,eh1);
box.addWidget(&button1);
window.show();
return app.exec();
}
样本输出:
CTOR 0x7ffdd35f4ed0
Copy from 0x7ffdd35f4ed0 -> 0x7ffdd35f4ee0
Copy from 0x7ffdd35f4ee0 -> 0x7ffdd35f4e70
Copy from 0x7ffdd35f4e70 -> 0x21b2c10
Copy from 0x21b2c10 -> 0x7ffdd35f4050
0x7ffdd35f4050 counter:0
Copy from 0x21b2c10 -> 0x7ffdd35f4050
0x7ffdd35f4050 counter:0
Copy from 0x21b2c10 -> 0x7ffdd35f4050
0x7ffdd35f4050 counter:0
Copy from 0x21b2c10 -> 0x7ffdd35f4050
0x7ffdd35f4050 counter:0
但是,它并没有真正按预期运行。它从复制回调对象开始(我理解:因此它不会在与其关联的对象之前超出范围)。但是,该对象在每个触发事件之前被复制,所以我的计数器保持为零。为什么它保持对象不变?