Skip to content
QtWS25 Last Chance
  • 0 Votes
    8 Posts
    2k Views
    JonBJ
    @MegAmaNeo1 In @jsulm's outline code you need a signal named pressed in CustomItem for connect(&item, CustomItem::pressed ..., and you also need an array/QVector of bool named pressed for pressed[i]. You'll presumably need to rename one of them. And where he writes void QGraphicsItem::mousePressEvent he means your void CustomItem::mousePressEvent override.
  • How to enable mouse tracking for QGraphicsItem?

    Unsolved General and Desktop qgraphicsitem
    3
    0 Votes
    3 Posts
    945 Views
    Alex SUNPPA
    @SGaist yes, hovering will be okay too. I just didn't think that constant mouse tracking is such a problem.
  • 0 Votes
    2 Posts
    745 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
    778 Views
    jsulmJ
    @incam https://doc.qt.io/qt-5/qpropertyanimation.html
  • 0 Votes
    3 Posts
    467 Views
    L
    @mrjj Thanks, so I hit the limit. I'll try not to go over that.
  • 0 Votes
    1 Posts
    517 Views
    No one has replied
  • QGraphicsItem move parent instead of child...

    Unsolved General and Desktop qgraphicsitem
    2
    0 Votes
    2 Posts
    567 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
    415 Views
    No one has replied
  • 0 Votes
    1 Posts
    334 Views
    No one has replied
  • 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
    6 Posts
    879 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
    870 Views
    F
    @mrjj Yes, it worked. Thank you!
  • 0 Votes
    1 Posts
    236 Views
    No one has replied
  • Clipping QGraphicsItems in a QChart

    Unsolved General and Desktop qchart qgraphicsitem clipping
    3
    0 Votes
    3 Posts
    690 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
    475 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
    537 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
    579 Views
    SGaistS
    Hi, Can you provide a minimal compilable code that shows that behaviour ?
  • QGraphicsItem - itemChanged & child items issue

    Unsolved General and Desktop qgraphicsitem
    1
    0 Votes
    1 Posts
    261 Views
    No one has replied
  • 0 Votes
    5 Posts
    568 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 : [image: 1e3b77d8-9ab6-40cb-a748-1377ac582d47.png] Top left item, is the myPos/newPos variable newPos being local and myPos being scenePos(), item below is the "Start"