Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to be notified when a property changes in a 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.

like image 430
darkadept Avatar asked Mar 08 '10 06:03

darkadept


2 Answers

For dynamic properties, you can use QDynamicPropertyChangeEvent.

Hope it helps !

like image 62
Matthieu Avatar answered Oct 16 '22 09:10

Matthieu


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);

//...
like image 5
TimW Avatar answered Oct 16 '22 09:10

TimW