简体   繁体   English

如何在 QObject 中捕获 QEvent::LanguageChange

[英]How to catch QEvent::LanguageChange in QObject

My goal is to retranslate a QObject subclass on the fly.我的目标是即时重新翻译QObject子类。

In QWidget it's really simple to catch QEvent::LanguageChange : we just override changeEvent .QWidget ,捕获QEvent::LanguageChange非常简单:我们只需覆盖changeEvent However, there is not such method in QObject and this is where I'm stuck.但是, QObject没有这样的方法,这就是我被卡住的地方。

How to catch QEvent::LanguageChange in QObject ?如何在QObject捕获QEvent::LanguageChange

You can simply override the QObject::event method...您可以简单地覆盖QObject::event方法...

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.或者,您可以将相同的逻辑放在事件过滤器中,并将其附加到感兴趣的QObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM