QAbstractListModel with QObject::event()
-
Hi,
I have a QAbstractListModel.
void SplashScreenListModel::append(const QString& name, QObject* item) { s_instance->beginInsertRows(QModelIndex(), s_instance->m_data.length(), s_instance->m_data.length()); s_instance->m_data.append(SPDataItem(name, item, false)); s_instance->endInsertRows(); // Set name for identification in slot item->setObjectName(name); connect(item, &QObject::event, s_instance.get(), &SplashScreenListModel::recEvent); <----- HOW? } void SplashScreenListModel::recEvent(QEvent* e) { if (e->type() != QEvent::DynamicPropertyChange) return; QDynamicPropertyChangeEvent* pc = static_cast<QDynamicPropertyChangeEvent*>(e); // Not the correct dynamic property if (pc->propertyName() != "initialized") return; // Value is false if (!sender()->property(pc->propertyName()).value<bool>()) return; QModelIndex idx = indexByName(sender()->objectName()); setData(idx, true, SplashScreenListModel::InitializationRole); }
QObjects have a function
[virtual] bool QObject::event(QEvent *e)
which can detectQDynamicPropertyChangeEvent
.
Since my SplashScreen dependencies can change I dont want to adjust each dependencie. I want to bind to a signal for each QObject so I can filter forQDynamicPropertyChangeEvent
in my model. How can I implement thatconnect()
in theappend(const QString& name, QObject* item)
? -
The correct solution in my scenario is to install an event filter.
void SplashScreenListModel::append(const QString& name, QObject* item) { // Get dynamic property initialized value // If the returned variant is invalid, the property is not set yet QVariant v = item->property("initialized"); bool initStatus = false; if (v.isValid() && v.value<bool>()) initStatus = true; s_instance->beginInsertRows(QModelIndex(), s_instance->m_data.length(), s_instance->m_data.length()); s_instance->m_data.append(SPDataItem(name, item, initStatus)); s_instance->endInsertRows(); // Set name for identification in slot item->setObjectName(name); // Install eventfilter item->installEventFilter(s_instance.get()); }
bool SplashScreenListModel::eventFilter(QObject* obj, QEvent* event) { if (event->type() != QEvent::DynamicPropertyChange) return QObject::eventFilter(obj, event); // Default processing // We have a QDynamicPropertyChangeEvent at hand QDynamicPropertyChangeEvent* pc = static_cast<QDynamicPropertyChangeEvent*>(event); // Not the correct dynamic property if (pc->propertyName() != "initialized") return QObject::eventFilter(obj, event); // Default processing // Value is false if (!obj->property(pc->propertyName()).value<bool>()) return true; QModelIndex idx = indexByName(obj->objectName()); setData(idx, true, SplashScreenListModel::InitializationRole); return true; }
-
Hi,
I have a QAbstractListModel.
void SplashScreenListModel::append(const QString& name, QObject* item) { s_instance->beginInsertRows(QModelIndex(), s_instance->m_data.length(), s_instance->m_data.length()); s_instance->m_data.append(SPDataItem(name, item, false)); s_instance->endInsertRows(); // Set name for identification in slot item->setObjectName(name); connect(item, &QObject::event, s_instance.get(), &SplashScreenListModel::recEvent); <----- HOW? } void SplashScreenListModel::recEvent(QEvent* e) { if (e->type() != QEvent::DynamicPropertyChange) return; QDynamicPropertyChangeEvent* pc = static_cast<QDynamicPropertyChangeEvent*>(e); // Not the correct dynamic property if (pc->propertyName() != "initialized") return; // Value is false if (!sender()->property(pc->propertyName()).value<bool>()) return; QModelIndex idx = indexByName(sender()->objectName()); setData(idx, true, SplashScreenListModel::InitializationRole); }
QObjects have a function
[virtual] bool QObject::event(QEvent *e)
which can detectQDynamicPropertyChangeEvent
.
Since my SplashScreen dependencies can change I dont want to adjust each dependencie. I want to bind to a signal for each QObject so I can filter forQDynamicPropertyChangeEvent
in my model. How can I implement thatconnect()
in theappend(const QString& name, QObject* item)
?@Redman said in QAbstractListModel with QObject::event():
QObject::event
This is an event handler, not a signal. You cannot connect to event handlers, you can only override event handlers in derived classes.
You could have a base class for all your item where you override QObject::event. -
The correct solution in my scenario is to install an event filter.
void SplashScreenListModel::append(const QString& name, QObject* item) { // Get dynamic property initialized value // If the returned variant is invalid, the property is not set yet QVariant v = item->property("initialized"); bool initStatus = false; if (v.isValid() && v.value<bool>()) initStatus = true; s_instance->beginInsertRows(QModelIndex(), s_instance->m_data.length(), s_instance->m_data.length()); s_instance->m_data.append(SPDataItem(name, item, initStatus)); s_instance->endInsertRows(); // Set name for identification in slot item->setObjectName(name); // Install eventfilter item->installEventFilter(s_instance.get()); }
bool SplashScreenListModel::eventFilter(QObject* obj, QEvent* event) { if (event->type() != QEvent::DynamicPropertyChange) return QObject::eventFilter(obj, event); // Default processing // We have a QDynamicPropertyChangeEvent at hand QDynamicPropertyChangeEvent* pc = static_cast<QDynamicPropertyChangeEvent*>(event); // Not the correct dynamic property if (pc->propertyName() != "initialized") return QObject::eventFilter(obj, event); // Default processing // Value is false if (!obj->property(pc->propertyName()).value<bool>()) return true; QModelIndex idx = indexByName(obj->objectName()); setData(idx, true, SplashScreenListModel::InitializationRole); return true; }
-