John Doe
John Doe

Reputation: 635

How to catch QEvent::LanguageChange in QObject

My goal is to retranslate a QObject subclass on the fly.

In QWidget it's really simple to catch QEvent::LanguageChange: we just override changeEvent. However, there is not such method in QObject and this is where I'm stuck.

How to catch QEvent::LanguageChange in QObject?

Upvotes: 0

Views: 549

Answers (1)

G.M.
G.M.

Reputation: 12929

You can simply override the QObject::event method...

class my_object: public QObject {
    using super = QObject;
protected:
    virtual bool event (QEvent *event) override
    {
        if (event->type() == QEvent::LanguageChange) {

            /*
             * Retranslation code goes here...
             */

            /*
             * Return true to prevent further processing.  This may
             * or may not be what you want depending on your needs.
             */
            return true;
        }

        /*
         * Fall through to the base class implementation.
         */
        return super::event(event);
    }
};

Alternatively, you could put the same logic in an event filter and attach that to the QObject of interest.

Upvotes: 1

Related Questions