Unsolved Can't use Q_PROPERTY in QGraphicsItem?
-
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.
-
@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
? -
@JonB I re-implement QGraphicsItem::boundingRect() and ::shape() so I won't be able to do that. :-/
-
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.