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. Can't use Q_PROPERTY in QGraphicsItem?
Forum Updated to NodeBB v4.3 + New Features

Can't use Q_PROPERTY in QGraphicsItem?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 444 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    Kite R
    wrote on last edited by
    #1

    Hi, my program uses external stylesheet file and until now I've had no problems using q_properties, but with this class I can't get the color values from the stylesheet. What's up? I'm still learning Qt, greatly appreciate any help.

    CPP:

    void Name::updatePath(const QPointF &startPos, const QPointF &endPos) {
      ...
      QColor color = getMyColor();
      qDebug << color; // output: "Invalid Color"
      ...
    }
    

    H:

    class Name : public QObject, public QGraphicsItem {
      Q_OBJECT
    
    public:
      Name(QGraphicsItem *parent, QGraphicsScene *scene);
      ~Name();
    
      void setMyColor(const QColor &color) { m_myColor = color; }
      QColor getMyColor() const { return m_myColor; }
    
    private:
      QColor m_myColor;
      Q_PROPERTY(QColor MyColor READ getMyColor WRITE setMyColor)
    

    STYLESHEET:

    Name {
    qproperty-MyColor: red;
    }
    

    In the output, I get this error:

    warning : Class Name implements the interface QGraphicsItem but does not list it in Q_INTERFACES. qobject_cast to QGraphicsItem will not work!
    

    Adding Q_INTERFACES(QGraphicsItem) under Q_OBJECT removes the error, but doesn't solve my problem not being able to get the q_property value from the stylesheet.

    JonBJ 1 Reply Last reply
    0
    • K Kite R

      Hi, my program uses external stylesheet file and until now I've had no problems using q_properties, but with this class I can't get the color values from the stylesheet. What's up? I'm still learning Qt, greatly appreciate any help.

      CPP:

      void Name::updatePath(const QPointF &startPos, const QPointF &endPos) {
        ...
        QColor color = getMyColor();
        qDebug << color; // output: "Invalid Color"
        ...
      }
      

      H:

      class Name : public QObject, public QGraphicsItem {
        Q_OBJECT
      
      public:
        Name(QGraphicsItem *parent, QGraphicsScene *scene);
        ~Name();
      
        void setMyColor(const QColor &color) { m_myColor = color; }
        QColor getMyColor() const { return m_myColor; }
      
      private:
        QColor m_myColor;
        Q_PROPERTY(QColor MyColor READ getMyColor WRITE setMyColor)
      

      STYLESHEET:

      Name {
      qproperty-MyColor: red;
      }
      

      In the output, I get this error:

      warning : Class Name implements the interface QGraphicsItem but does not list it in Q_INTERFACES. qobject_cast to QGraphicsItem will not work!
      

      Adding Q_INTERFACES(QGraphicsItem) under Q_OBJECT removes the error, but doesn't solve my problem not being able to get the q_property value from the stylesheet.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Kite-R said in Can't use Q_PROPERTY in QGraphicsItem?:

      class Name : public QObject, public QGraphicsItem

      I don't know whether it addresses your issue (perhaps not), but would it help/be better if you derived from GraphicsObject?

      K 1 Reply Last reply
      1
      • JonBJ JonB

        @Kite-R said in Can't use Q_PROPERTY in QGraphicsItem?:

        class Name : public QObject, public QGraphicsItem

        I don't know whether it addresses your issue (perhaps not), but would it help/be better if you derived from GraphicsObject?

        K Offline
        K Offline
        Kite R
        wrote on last edited by
        #3

        @JonB I re-implement QGraphicsItem::boundingRect() and ::shape() so I won't be able to do that. :-/

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          I have run into a lot of problems with multiple inheritance and QObject. It would be better to inherit from just QObject for your main object. Then create another class that inherits from QGraphicsItem. Make your first object a friend of the second object. That way it can treat the second object pretty much how it wants. I will often put the second object inside the first to indicate this is to be used with the first object only (if that is a thing you need to enforce). This sidesteps everything and allows you to inherit from QObject without it having issues with other objects. I don't know if this is ideal, but it seems to be working for me.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0

          • Login

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