Skip to content
  • 0 Votes
    5 Posts
    280 Views
    Pl45m4P

    @StudentScripter

    This example shows how a use case could look like

    https://doc.qt.io/qt-6/qtwidgets-graphicsview-basicgraphicslayouts-example.html

    But as you can see, the custom items inherit from QGraphicsLayoutItem and QGraphicsItem.
    So to properly use a QGraphicsItem in a layout, you have to inherit QGraphicsLayoutItem or use QGraphicsWidget directly... however I don't know how it fits in your existing logic/structure. It might break something... using layouts and "moving free / free resizable by user" is kinda counterproductive.

  • 0 Votes
    2 Posts
    253 Views
    Pl45m4P

    @StudentScripter

    Have you checked the QGraphicsLayout example already?

    https://doc.qt.io/qt-6/qtwidgets-graphicsview-basicgraphicslayouts-example.html
  • 0 Votes
    7 Posts
    1k Views
    C

    @SGaist I have tried many things so there are a few comments

    Here the minimal code below:

    main.cpp: #include "QApplication" #include "graphicsitemgroup.h" #include "qgraphicslinearlayout.h" #include "qgraphicsscene.h" #include "qgraphicsview.h" #include "graphicswidget.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); auto widget = new QGraphicsWidget(nullptr, Qt::Window); auto item_0 = new GraphicsItemGroup(widget); auto item_1 = new GraphicsItemGroup(widget); auto linearLayout = new QGraphicsLinearLayout(); linearLayout->addItem(item_0); linearLayout->addItem(item_1); widget->setLayout(linearLayout); QGraphicsScene scene; scene.addItem(widget); QGraphicsView view(&scene); view.setResizeAnchor(QGraphicsView::AnchorViewCenter); view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view.show(); return app.exec(); } graphicsitemgroup.h: #ifndef GRAPHICSITEMGROUP_H #define GRAPHICSITEMGROUP_H #include "qgraphicslayoutitem.h" #include "QGraphicsItemGroup" #include "qsize.h" class GraphicsItem; /** * @brief Placeholder for graphics items */ class GraphicsItemGroup : public QGraphicsLayoutItem, public QGraphicsItemGroup { public: GraphicsItemGroup(QGraphicsItem* parent = nullptr); virtual ~GraphicsItemGroup(); void setGeometry(const QRectF& geometry) override; QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const override; // QRectF boundingRect() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; private: QSizeF m_size; GraphicsItem* m_item; }; #endif // GRAPHICSITEMGROUP_H graphicsitemgroup.cpp: #include "graphicsitemgroup.h" #include "graphicsitem.h" #include "qpainter.h" #include "qdebug.h" GraphicsItemGroup::GraphicsItemGroup(QGraphicsItem* parent) : QGraphicsItemGroup(parent) , m_size(QSize(100, 100)) , m_item(new GraphicsItem(QSizeF(50, 50))) { addToGroup(m_item); setGraphicsItem(this); } GraphicsItemGroup::~GraphicsItemGroup() = default; void GraphicsItemGroup::setGeometry(const QRectF& geometry) { prepareGeometryChange(); QGraphicsLayoutItem::setGeometry(geometry); setPos(geometry.topLeft()); qDebug() << __PRETTY_FUNCTION__; } QSizeF GraphicsItemGroup::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const { switch (which) { case Qt::MinimumSize: case Qt::PreferredSize: return m_size; case Qt::MaximumSize: default: break; } return constraint; } //QRectF GraphicsItemGroup::boundingRect() const //{ // return QRectF(QPointF(0, 0), geometry().size()); //} void GraphicsItemGroup::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { painter->drawRect(QRectF(QPointF(0, 0), geometry().size())); } graphicsitem.h: #ifndef GRAPHICSITEM_H #define GRAPHICSITEM_H #include "qgraphicslayoutitem.h" #include "qgraphicsitem.h" #include "qsize.h" class QRectF; class QPainter; class QStyleOptionGraphicsItem; class GraphicsItem : /*public QGraphicsLayoutItem,*/ public QGraphicsItem { public: GraphicsItem(QSizeF size, QGraphicsItem* parent = nullptr); virtual ~GraphicsItem(); // void setGeometry(const QRectF& geometry) override; // QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const override; QRectF boundingRect() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; private: QSizeF m_size; }; #endif graphicsitem.cpp: #include "graphicsitem.h" #include "qpainter.h" #include "qrect.h" #include "qdebug.h" GraphicsItem::GraphicsItem(QSizeF size, QGraphicsItem* parent) : QGraphicsItem(parent) , m_size(size) { // setGraphicsItem(this); } GraphicsItem::~GraphicsItem() = default; //void GraphicsItem::setGeometry(const QRectF& geometry) //{ // prepareGeometryChange(); // QGraphicsLayoutItem::setGeometry(geometry); // setPos(geometry.topLeft()); // // qDebug() << __PRETTY_FUNCTION__; //} // //QSizeF GraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const //{ // switch (which) // { // case Qt::MinimumSize: // case Qt::PreferredSize: // return m_size; // case Qt::MaximumSize: // default: // break; // } // // return constraint; //} QRectF GraphicsItem::boundingRect() const { return QRectF(QPointF(20, 20), m_size); } void GraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { painter->drawRect(boundingRect()); } CMakeLists.txt: cmake_minimum_required(VERSION 3.0) project(GraphicsLayoutExample) find_package(Qt5 COMPONENTS Core REQUIRED Widgets REQUIRED Gui REQUIRED ) add_executable(${CMAKE_PROJECT_NAME} main.cpp graphicsitemgroup.cpp graphicsitem.cpp ) target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC .) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt5::Widgets Qt5::Core Qt5::Gui)

    I would like to have the graphics item (here a little rectangle) to be resized depending on the a graphics item group (the latter paints another rectangle just to have a visual on what is going on). In the future, I have planned to put two graphics items (one over the other) in the group. Thanks for your help.

  • 0 Votes
    2 Posts
    580 Views
    SGaistS

    Hi,

    What you want to achieve is not really clear. Do you want to obtain with your classes the same thing as a Widget with a QGridLayout and other widgets in that layout ?