Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractListModel with QObject::event()
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel with QObject::event()

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 232 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Redman
    wrote on last edited by Redman
    #1

    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 detect QDynamicPropertyChangeEvent.
    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 for QDynamicPropertyChangeEvent in my model. How can I implement that connect() in the append(const QString& name, QObject* item)?

    jsulmJ 1 Reply Last reply
    0
    • R Offline
      R Offline
      Redman
      wrote on last edited by
      #3

      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;
      }
      
      1 Reply Last reply
      1
      • R Redman

        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 detect QDynamicPropertyChangeEvent.
        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 for QDynamicPropertyChangeEvent in my model. How can I implement that connect() in the append(const QString& name, QObject* item)?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • R Offline
          R Offline
          Redman
          wrote on last edited by
          #3

          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;
          }
          
          1 Reply Last reply
          1
          • R Redman has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved