当 QObject 中的属性发生更改时,有没有办法收到通知?

发布于 2024-08-23 19:17:20 字数 249 浏览 13 评论 0原文

首先,我使用 Qt 4 库和 C++。

QObject上的属性(动态或其他)发生变化时,有没有办法得到通知(信号、事件?)?

我无法修改 QObject 类,因为它是 Qt4 库的一部分。有关 QObject 的更多信息此处

First off, I'm using the Qt 4 libraries and C++.

Is there a way to be notified (signal, event, ?) when a property (dynamic or otherwise) changes on a QObject?

I can't modify the QObject class as it is part of the Qt4 library. More info about QObject here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-08-30 19:17:20

对于动态属性,您可以使用QDynamicPropertyChangeEvent

希望有帮助!

For dynamic properties, you can use QDynamicPropertyChangeEvent.

Hope it helps !

夏九 2024-08-30 19:17:20

您可以在 QObject 实例上安装事件过滤器
因此,如果您想收到有关 windowsTitle 更改的通知,您可以安装一个捕获 QEvent::WindowTitleChange 事件的事件过滤器。
例如:

class WindowsTitleWatcher : public QObject
{
    Q_OBJECT
public:
    WindowsTitleWatcher(QObject *parent) : QObject(parent) {
    }

signals:
    void titleChanged(const QString& title);

protected:
    bool eventFilter(QObject *obj, QEvent *event){ 
        if(event->type()==QEvent::WindowTitleChange) {
            QWidget *const window = qobject_cast<QWidget *>(obj);
            if(window)
                emit titleChanged(window->windowTitle());
        } 
        return QObject::eventFilter(obj, event);
    }
};

//...
QDialog *const dialogToWatch = ...;
QObject *const whoWantToBeNotified = ...;
QObject *const titleWatcher = new WindowsTitleWatcher(dialogToWatch);
whoWantToBeNotified->connect(
    titleWatcher, 
    SIGNAL(titleChanged(QString)), 
    SLOT(onTitleChanged(QString)));
dialogToWatch->installEventFilter(titleWatcher);

//...

You can install an event filter on QObject instances.
So if you want to be notified for windowsTitle changes you can install an eventfilter that captures QEvent::WindowTitleChange events.
For example:

class WindowsTitleWatcher : public QObject
{
    Q_OBJECT
public:
    WindowsTitleWatcher(QObject *parent) : QObject(parent) {
    }

signals:
    void titleChanged(const QString& title);

protected:
    bool eventFilter(QObject *obj, QEvent *event){ 
        if(event->type()==QEvent::WindowTitleChange) {
            QWidget *const window = qobject_cast<QWidget *>(obj);
            if(window)
                emit titleChanged(window->windowTitle());
        } 
        return QObject::eventFilter(obj, event);
    }
};

//...
QDialog *const dialogToWatch = ...;
QObject *const whoWantToBeNotified = ...;
QObject *const titleWatcher = new WindowsTitleWatcher(dialogToWatch);
whoWantToBeNotified->connect(
    titleWatcher, 
    SIGNAL(titleChanged(QString)), 
    SLOT(onTitleChanged(QString)));
dialogToWatch->installEventFilter(titleWatcher);

//...
桃扇骨 2024-08-30 19:17:20

我不熟悉“语言”,但总的来说,您想要做的事情遵循观察者设计模式。您会看到,在这种模式中,您所做的是将观察者注册到可观察对象,即 QObject。在 Observable 对象内部,您将跟踪其观察者的列表。当 QObject 的状态发生变化时,您可以使用它拥有的观察者列表通知所有观察者...本质上,您创建了一个观察者可以实现的接口...该接口将成为您通知对象的方式可观察对象的不同观察者。只是一个想法!

I'm not familiar with the "language" but in general, what you want to do follows the Observer design pattern. You see, in this pattern, what you do is to register Observers to the Observable Objects i.e. QObjects. Inside the Observable object, you will keep track of a list of its observers. When a change in the QObjects' state occured, you could notify all of the observers using the observer list it has.... In essence, you create an Interface which Observers can implement... This Interface will become you way of notifying the different Observers of the Observable Object. just a thought!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文