0

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

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

4

1 回答 1

-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

哪个更值得期待

于 2016-07-13T08:04:11.497 回答