QWidget and addanchor?
-
Is there something wrong with the information on this page?
https://doc.qt.io/qt-6/qtwidgets-graphicsview-simpleanchorlayout-example.htmlBecause I am trying to add a widget and I get the error message:
error: Cannot initialize a parameter of type 'QGraphicsLayoutItem *' with an lvalue of type 'QWidget *'
However looking again, its not a QWidget at all, its defined as Widget.
What I am trying to do is add anchor functionality to my widgets, is this possible ?
-
@SPlatten
Look at the source code here:
https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/graphicsview/simpleanchorlayout/main.cpp?h=6.6class Widget : public QGraphicsWidget { public: Widget(const QColor &color, const QColor &textColor, const QString &caption, QGraphicsItem *parent = nullptr) : QGraphicsWidget(parent) , caption(caption) , color(color) , textColor(textColor) { } void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override { QFont font; font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height())); painter->fillRect(boundingRect(), color); painter->save(); painter->setFont(font); painter->setPen(textColor); painter->drawText(boundingRect(), Qt::AlignCenter, caption); painter->restore(); } private: QString caption; QColor color; QColor textColor; };
-
@JoeCFD said in QWidget and addanchor?:
Thank you, however it doesn't look it supports regular QWidgets, If I want to add a QPushButton with anchors can that be done with this code?
-
@JoeCFD said in QWidget and addanchor?:
What I am trying to do is add the capability for any widgets to be added to any layout with any anchors.
I believe I can write my own implementation.
-
@SPlatten hi,
You can add a widget to a QGraphicsScene and you get a QGraphicsProxyWidget that you can then add to your layout.