Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Error when making "QObject::connect"



  • Hi,

    I want to create a simple class inheriting from QList but while creating the line, from connect returns an error:

    Error:

    C:\Qt\Qt5.12.5\5.12.5\mingw73_64\include\QtCore\qobject.h:250: error: no matching function for call to 'QObject::connectImpl(const Object*&, void**, const Object*&, void**, QtPrivate::QSlotObject<void (AssetsList::*)(), QtPrivate::List<>, void>*, Qt::ConnectionType&, const int*&, const QMetaObject*)'
             return connectImpl(sender, reinterpret_cast<void **>(&signal),
                    ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                receiver, reinterpret_cast<void **>(&slot),
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                new QtPrivate::QSlotObject<Func2, typename QtPrivate::List_Left<typename SignalType::Arguments, SlotType::ArgumentCount>::Value,
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                typename SignalType::ReturnType>(slot),
                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                 type, types, &SignalType::Object::staticMetaObject);
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    C:\Qt\Qt5.12.5\5.12.5\mingw73_64\include\QtCore\qobjectdefs_impl.h:414: error: invalid static_cast from type 'QObject*' to type 'QtPrivate::FunctionPointer<void (AssetsList::*)()>::Object* {aka AssetsList*}'
                     FuncType::template call<Args, R>(static_cast<QSlotObject*>(this_)->function, static_cast<typename FuncType::Object *>(r), a);
                                                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    My code:

    class AssetsList: public QList<Asset>{
        Q_OBJECT
    public:
        AssetsList(): actionsValue(0), bondsValue(0), rawMaterialsValue(0){
            QObject::connect(this, &AssetsList::test_signal, this, &AssetsList::test_slot);
        }
        virtual ~AssetsList(){}
    
    signals:
        void test_signal();
    
    public slots:
        void test_slot(){
            /* do something*/
        }
    
    private:
        float actionsValue;
        float bondsValue;
        float rawMaterialsValue;
    };
    

    How could i fix that?


  • Lifetime Qt Champion

    @Creatorczyk https://doc.qt.io/qt-5/qobject.html#Q_OBJECT
    "Note: This macro requires the class to be a subclass of QObject. Use Q_GADGET instead of Q_OBJECT to enable the meta object system's support for enums in a class that is not a QObject subclass."
    Try to change Q_OBJECT to Q_GADGET because QList is not derived from QObject.



  • @jsulm I changed like below:

    class AssetsList: public QList<Asset>{
        Q_GADGET
    public:
        AssetsList(): actionsValue(0), bondsValue(0), rawMaterialsValue(0){
            QObject::connect(this, &AssetsList::test_signal, this, &AssetsList::test_slot);
        }
        virtual ~AssetsList(){}
    
    signals:
        void test_signal();
    
    public slots:
        void test_slot(){
    
        }
    
    private:
        float actionsValue;
        float bondsValue;
        float rawMaterialsValue;
    };
    

    But there are still errors:

    error: 'qt_metacall' is not a member of 'AssetsList'
             enum { Value =  sizeof(test(&Object::qt_metacall)) == sizeof(int) };
                                         ^~~~~~~
    
    error: static assertion failed: No Q_OBJECT in the class with the signal
     #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
    
    error: static assertion failed: No Q_OBJECT in the class with the signal
     #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                     ^
    

  • Lifetime Qt Champion

    @Creatorczyk My bad: "Q_GADGETs can have Q_ENUM, Q_PROPERTY and Q_INVOKABLE, but they cannot have signals or slots."
    https://doc.qt.io/qt-5/qobject.html#Q_GADGET
    So, my suggestion is not going to work as you want to have signals/slots.
    Is there a reason why you want to subclass QList?



  • @jsulm I would like to inherit from QList to add my methods and to be able to pass a list to a model in a ListView in qml



  • @Creatorczyk Isn't it easier if you derive from QAbstractListModel?


Log in to reply