Issues When Add the Q_OBJECT to My SubClass of QGraphicsRectItem



  • like the head file, i had the subclass of QGraphicsRectItem.

    #ifndef GRAPHICSRECTITEM_H
    #define GRAPHICSRECTITEM_H
    
    #include <QGraphicsRectItem>
    
    class GraphicsRectItem : public QGraphicsRectItem
    {
    public:
        explicit GraphicsRectItem(const QRect &rect,QGraphicsItem *parent = Q_NULLPTR);
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    
    protected:
        /**...*/
    
    private:
    
        /**...*/
    private:
    
        /**...*/
    };
    
    #endif // GRAPHICSRECTITEM_H
    

    Every thing was ok. Where was no Q_OBJECT

    But, i want to use the signal and slot. so, i add the Q_OBJECT. like below.

    class GraphicsRectItem : public QGraphicsRectItem
    {
        Q_OBJECT
    public:
        explicit GraphicsRectItem(const QRect &rect,QGraphicsItem *parent = Q_NULLPTR);
        /**...*/
       
    

    Then, Has the some issues.

    1. ...error: 'staticMetaObject' is not a member of 'QGraphicsRectItem'...
    2. ...&QGraphicsRectItem::staticMetaObject, qt_meta_stringdata_GraphicsRectItem.data,...
    3. ...error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected QScopedPointer<QObjectData> d_ptr;
    4. ...error: within this context return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
      ...

    I just want to use the signal and slot. so Add the Q_OBJECT in my class. But There has some wrong!

    I didn't understand Q_OBJECT well.

    Thank u.



  • @joeQ said in Issues When Add the Q_OBJECT to My SubClass:

    Q_OBJECT

    Hi, I would recomment you taking some time and reading throught the docu.

    I'm not sure if QGraphicsRectItem actually subclasses of QObject but adding a #include <QObject> might help :)



  • QGraphicsRectItem is not from QObject. You can't do this. Why do you want add Q_OBJECT ?



  • @J.Hilk

    I add the #include<QObject>. it didn't work.

    In the doc waht you said. I searched the QGraphicsRectItem or QGraphicsItem . and want to know that did them Inherited the QObject.
    They didn't inherited QObject.



  • @dheerendra

    I want to use the signal and slot. I want to emit signal to scene(or to another rect item) when rect item size changed.



  • Then do something like this. It should work. Inherit from QObject.

    class MyItem : public QObject,public QGraphicsRectItem
    {
    Q_OBJECT
    public:
    MyItem();
    };



  • @dheerendra

    Yes, (0o0), it's ok! Thank u. Very much. Excellent.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.