Skip to content
QtWS25 Call for Papers
  • 0 Votes
    2 Posts
    538 Views
    eyllanescE

    boundingRect does not depend on the position of the item with respect to the scene since it is in coordinates with respect to the item. I recommend you review https://doc.qt.io/qt-5/graphicsview.html so that you understand the different coordinate systems that are handled.

  • 0 Votes
    2 Posts
    599 Views
    jsulmJ

    @incam https://doc.qt.io/qt-5/qpropertyanimation.html

  • 0 Votes
    3 Posts
    341 Views
    L

    @mrjj Thanks, so I hit the limit. I'll try not to go over that.

  • 0 Votes
    1 Posts
    403 Views
    No one has replied
  • 0 Votes
    2 Posts
    465 Views
    B

    Since QGraphicsItem::pos()

    Returns the position of the item in parent coordinates.

    So the item and its parent item are not in the same coordinate system.
    I think you should calculate the offset between pos() and value, then calculate the new pos of mParentItem by adding offset to mParentItem->pos().

  • 0 Votes
    1 Posts
    358 Views
    No one has replied
  • 0 Votes
    1 Posts
    252 Views
    No one has replied
  • 0 Votes
    7 Posts
    907 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
    6 Posts
    744 Views
    J

    @amansahil
    Check after setting the position if polygon->scenePos() - clickPoint == (0, 0). If it is, there's a problem with the event->scenePos(), and if not, there's a problem with how the polygon position is being set.

    The right conversion would be

    polygon->setPos( clickPoint - polygonParent->scenePos() )

    mapFromScene() is better than mapToScene() since you have the coordinates in the scene frame and you want them in local coordinates. But it still isn't quite right! You specify position in the local parent coordinates, not the local item frame. So that approach to use is

    polygon->setPos( polygonParent->mapFromScene(clickPoint) )
  • 0 Votes
    5 Posts
    631 Views
    F

    @mrjj Yes, it worked. Thank you!

  • 0 Votes
    1 Posts
    189 Views
    No one has replied
  • 0 Votes
    3 Posts
    521 Views
    W

    For sure it can be measured, and I do agree with the approach "if it works fine, just use it".

    But I am still curious, why clipping is expensive in the first place, so that the warning exists in the docs? Has anyone experienced performance impact while actively using clipping for real-time chart?

  • 0 Votes
    3 Posts
    389 Views
    D

    It appears that QGraphicsScene checks if position of item is visible in view and then draws/dont draws it... returning qgraphicsScene->sceneRect() as boundingRect on my curve "solved" the issues but its a problem for selectable items... hmmm

  • 0 Votes
    2 Posts
    463 Views
    mrjjM

    Hi
    I think you just broke GraphicsView normal handling.
    Try also calling base class mousePressEvent as it might use for the actual item selection

    void GraphicsView::mousePressEvent(QMouseEvent *event) { if(event->buttons().testFlag(Qt::LeftButton)) { int x,y; x = event->pos().x(); y = event->pos().y(); qDebug() << x << ", " << y; QPointF point = mapToScene(x, y); QGraphicsEllipseItem *ellipse; ellipse = scene->addEllipse(point.x(),point.y(),5,5,QPen(Qt::red), QBrush(Qt::red)); ellipse->setFlag(QGraphicsEllipseItem::ItemIsMovable); } // call base class QGraphicsView::mousePressEvent(event); }
  • 0 Votes
    2 Posts
    418 Views
    SGaistS

    Hi,

    Can you provide a minimal compilable code that shows that behaviour ?

  • 0 Votes
    1 Posts
    219 Views
    No one has replied
  • 0 Votes
    5 Posts
    455 Views
    D

    @SGaist Possibly yes... I'm back to this & lost again... I think I figured out the last problem without needing it, but now I need it again...
    Scenario = bezier curve, 4 points, first start, control left, control right, endpoints.

    Start and end can never cross each other on Z-axis.

    Control left can not pass end on the Z-axis and control Right can not pass start on z-axis either.
    Control points are children of start/endpoints.

    (Hope this make sense...)

    So what I need to find out is... location of End point while being inside Control Left event. I need to somehow map scenePos() of End point to my Control Left local space....

    here is some debuging I was trying to do...

    (test case, moving right control point towards left Start item)

    targetPos : QPointF(348,-478) // scenePos() of Start item myPos : QPointF(336,-617) // scenePos() of rightControl scenePos() - already invalid position because we passed the Z location of targetPos item. myLocPos : QPointF(-237,2) This should match other item loc at some point... localPos of rightControl mapFromScene: QPointF(12,139) // mappings mapToItem : QPointF(113,-477) // mappings mapFromItem : QPointF(583,-479) // mappings

    one of mappings should return a number close to myLocPos but none of them do :- (

    Debug code :

    qDebug() << "oh snap!" << "\n targetPos : " << itemPos << "\n myPos : " << myPos << "\n myLocPos : " << newPos << "local pos of rightControl item" << "\n mapFromScene: " << mapFromScene(itemPos) << "\n mapToItem : " << mapToItem(items[x], itemPos) << "\n mapFromItem : " << mapFromItem(items[x], itemPos);

    Esentially this :
    699607e4-2d41-4882-93a8-4b62531390a6-image.png
    Top left item, is the myPos/newPos variable newPos being local and myPos being scenePos(), item below is the "Start"

  • 0 Votes
    2 Posts
    293 Views
    SGaistS

    Hi,

    Without more details form you side, it looks like that's the function you are looking for.

  • 0 Votes
    3 Posts
    2k Views
    A

    Call fitInView on the GraphicsView with the Image whenever the size of the GraphicsView changes. The rest should work automatically.