Skip to content
  • 0 Votes
    11 Posts
    633 Views
    S

    @Pl45m4 I tried another thing now. Just using the group as way of organizing but setting a qgraphicsrectitem as parent of the qgraphicsitem group. On mouse release i position the qgraphicsrectitem above the groupitem:

    if(DragSelectionInProgress == true){ qDebug() << "SelectionInProgress" << GroupSelectionRect; //Erstelle eine Liste aller selecteten items die nicht das grouprect sind und füge alle items dieser Liste dem grouprect hinzu if(GroupSelectionRect){ QList<QGraphicsItem *> selectedItems = this->selectedItems(); QList<QGraphicsItem *> itemsToAdd; foreach(QGraphicsItem *item, selectedItems) { if(item != GroupSelectionRect && item != SelectionRectPixItem) { itemsToAdd.append(item); } } if(itemsToAdd.count() > 1){ foreach(QGraphicsItem *item, itemsToAdd) { // Check if the item is a pixmap item or a rect item ResizablePixmapItem *rectItem = qgraphicsitem_cast<ResizablePixmapItem *>(item); if(rectItem){ rectItem->setShouldDrawHandles(false); rectItem->drawHandlesIfNecessary(false); //rectItem->setParentItem(SelectionRectPixItem); } GroupSelectionRect->addToGroup(item); } GroupSelectionRect->setParentItem(SelectionRectPixItem); SelectionRectPixItem->setRect(GroupSelectionRect->boundingRect()); //GroupSelectionRect->setSelected(true); } } DragSelectionInProgress = false; }else{ } QGraphicsScene::mouseReleaseEvent(event); }

    I can calculate exactly how many pixel the qgraphicsrectitem is resized, but how do i apply this to the group? I want the graphicsitemgroup to mimic the proportions of the qgraphicsrectitem. It should only resize, but stay anchored in the corner that is not moved.

    The green outline below is the groupitem, the handles are from the qgraphicsrectitem laying above: f41f87da-3b8b-4e26-aee5-dcf1bf1d3da3-image.png cb6797e3-0bd4-4e9e-9f11-90d6cc8547e8-image.png

  • 0 Votes
    5 Posts
    1k Views
    T

    At the end I've researched again the view's transformations and concluded to support both features and warn for the complexity.

    I did not re-implement the add/remove items of the scene together with a group. Thanks for the hint @Asperamanca .

  • 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
    1k Views
    X

    Finally, I found a nice way to visualize a QGraphicsItemGroup in an accurate way. Here is the code:

    if (QGraphicsItemGroup *group = qgraphicsitem_cast<QGraphicsItemGroup *>(item)) { foreach (QGraphicsItem *child, group->childItems()) child->paint(painter, option, widget); } else { item->paint(painter, option, widget); }

    I hope this trick can be useful for somebody else ;)