How to have a function triggered by an event
I have a class (which inherits from QSortFilterProxyModel, so it is a QObject).
I would like the class to act on a @QEvent::LanguageChange@
All my QWidget derived classes have a very helpful QWidget::changeEvent function that seems to get called automatically when the event happens.
How can I have a specific function in the QSortFilterProxyModel derived object be called as well? I was hoping to find some kind of "register to receive THIS kind of event, by having THIS class function called" mechanism.
In your parent class add the connection to it?
All QObject derived classes have the build in LanguageChange event.
Read this: "QEvent":http://qt-project.org/doc/qt-5/QEvent.html#details
But do you really want the proxy to handle this? Or should the base data in your model itself do it? IMHO you should do it where the data is hold, so in the model data.
Ah, is it done with deliberately made connections? I had the idea from somewhere that QEvents happened in some kind of global type environment (or at least, something with a wide scope) and that QObjects could effectively ask to be triggered by any event.
I might be misremembering that from a totally different message-handling system I worked with a few years ago. I've got enough frameworks in my head now that some of them blur together :(
QObjects get event automatic from the QApplication or from their local event loop AFAIK.
When somewhere the qApp->setTranslator is called, every widget should get someting like this:
void MainWindow::changeEvent(QEvent* event)
if (event->type() == QEvent::LanguageChange)
// retranslate designer form
// remember to call base class implementation QWidget::changeEvent(event);
Yes, the QWidgets all do; I have other QObjects that also need to know.
Hi, If those QObjects have parents, those parents will continue those events downwards. So, that's why in my example, i added the QWidget::changeEvent(event) code. The same goes for QObjects!
Did you read the QEvent class I mentioned before?