Qt5函数对象作为回调复制

Qt5 Function object as callback copying

我尝试了使用可调用对象作为回调的新 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

然而,它并没有真正按预期运行。它从复制回调对象开始(据我所知:因此它不会在与其关联的对象之前超出范围)。但是,在每次触发事件之前都会复制该对象,因此我的计数器保持为零。为什么它保持对象不变?

这似乎是 Qt 5.5.1 中的错误。针对 Qt 5.7 编译给出以下输出:

CTOR 0x7ffd7a47e370
Copy from 0x7ffd7a47e370 -> 0x7ffd7a47e380
Copy from 0x7ffd7a47e380 -> 0x7ffd7a47e310
Copy from 0x7ffd7a47e310 -> 0xb65970
0xb65970  counter:0
0xb65970  counter:1
0xb65970  counter:2

哪个更值得期待