Qt5函数对象作为回调复制

时间:2016-07-12 19:19:52

标签: c++ qt callback qt5 function-object

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

然而,它并没有像预期的那样表现。它从复制回调对象开始(我理解:因此它不会超出与其关联的对象之前的范围)。但是,在每次触发事件之前复制对象,因此我的计数器保持为零。为什么它保持对象不变?

1 个答案:

答案 0 :(得分:-1)

这似乎是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

更期待哪个