Want to implement rectangle movements similar to in microsoft power point in my QGraphicsView
-
Hi All
I have a QgraphicsView and I have rectangles drawn inside the sameThere is functionality in microsoft power point where When I press up key or down key , the right key or letf key the rectangular box moves
UP : vertically up on the Y axis
Down : vertically down on the Y axis
Right : right movement on right key press
Left : left movement on left key pressI want to implement the same functionality in my QGraphicsView any suggestions would help
-
Hi All
I have a QgraphicsView and I have rectangles drawn inside the sameThere is functionality in microsoft power point where When I press up key or down key , the right key or letf key the rectangular box moves
UP : vertically up on the Y axis
Down : vertically down on the Y axis
Right : right movement on right key press
Left : left movement on left key pressI want to implement the same functionality in my QGraphicsView any suggestions would help
@Qt-Enthusiast You can use http://doc.qt.io/qt-5/qgraphicsview.html#mousePressEvent to select a rectangle.
Then use http://doc.qt.io/qt-5/qgraphicsview.html#keyPressEvent to move the selected rectangle. -
First I select the rectangle and then the rectangle should move up when I use up arrow key
-
First I select the rectangle and then the rectangle should move up when I use up arrow key
@Qt-Enthusiast Why do you want to move mouse up? You want to move rectangle, not mouse cursor, right?
You use mousePressEvent to select a rectangle, not to move it. I mean, how do you decide which rectangle to move? I guess your user has to click on the rectangle to select it and then move? -
First I select the rectangle and then the rectangle should move up when I use up arrow key
@Qt-Enthusiast What is "mouse up arrow key"?
-
@Qt-Enthusiast What is "mouse up arrow key"?
@jsulm I hav e edited the text
-
@jsulm I hav e edited the text
-
could you guide a little bit more .on Keypress event what should I do so that rectangle moves up on pressing SHift+ arrow key
-
could you guide a little bit more .on Keypress event what should I do so that rectangle moves up on pressing SHift+ arrow key
you overwrite the function, in your QGraphicsView header
//xxx.h protected: void keyPressEvent(QKeyEvent *event);
than you handle the event in your cpp file
keyPressEvent(QKeyEvent *event){ if( event->key == Qt::Key_Left){ do stuff... } }
-
could you guide a little bit more .on Keypress event what should I do so that rectangle moves up on pressing SHift+ arrow key
If you want another way, do the following:
[...] #include <QGraphicsRectItem> class MyRectItem : public QGraphicsRectItem { public: explicit MyRectItem(QGraphicsItem *parent = 0); // QGraphicsItem interface protected: virtual void keyPressEvent(QKeyEvent *event) override; }; [...] void MyRectItem::keyPressEvent(QKeyEvent *event) { if (!flags().testFlag(QGraphicsItem::ItemIsMovable)) return; if (!isSelected()) return; switch (event->key()) { case Qt::Key_Left: moveBy(-1, 0); break; case Qt::Key_Right: moveBy(1, 0); break; case Qt::Key_Up: moveBy(0, -1); break; case Qt::Key_Down: moveBy(0, 1); break; } }
[...] auto item = new MyRectItem; item->setRect(QRectF(-100, -100, 100, 100)); item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable); graphicsView->scene()->addItem(item); [...]
-
In this I am not sure how to edit the locaation of existing rectangle. are you saying you are removing the previous rectangle
-
If you want another way, do the following:
[...] #include <QGraphicsRectItem> class MyRectItem : public QGraphicsRectItem { public: explicit MyRectItem(QGraphicsItem *parent = 0); // QGraphicsItem interface protected: virtual void keyPressEvent(QKeyEvent *event) override; }; [...] void MyRectItem::keyPressEvent(QKeyEvent *event) { if (!flags().testFlag(QGraphicsItem::ItemIsMovable)) return; if (!isSelected()) return; switch (event->key()) { case Qt::Key_Left: moveBy(-1, 0); break; case Qt::Key_Right: moveBy(1, 0); break; case Qt::Key_Up: moveBy(0, -1); break; case Qt::Key_Down: moveBy(0, 1); break; } }
[...] auto item = new MyRectItem; item->setRect(QRectF(-100, -100, 100, 100)); item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable); graphicsView->scene()->addItem(item); [...]
@Devopia53 In this I am not sure how to edit the locaation of existing rectangle. are you saying you are removing the previous rectangle
reply
-
@Devopia53 In this I am not sure how to edit the locaation of existing rectangle. are you saying you are removing the previous rectangle
reply
@Qt-Enthusiast
@Devopia53 pretty much wrote it out for you. Instead of the defaultQGraphicsItem
you use the custom classMyRectItem
he defined for you.the last section between the 2
[...]
even show you how to add aMyRectItem
to yourgraphicsView
.Not much else can be done here
-
One more question If I have pressed Shift + Up key what should be value of event->key()
-
One more question If I have pressed Shift + Up key what should be value of event->key()
Key Press event don't come simultaneously, so reimplement
KeyReleaseEvent
as well, to check if a shift key was pressed and relesed or is still pressed and adjust your KeyPress event accordingly -
A small code will be helpful again
-
A small code will be helpful again
//MyRectItem .h private: bool bShiftPressed = false; protected: virtual void keyReleaseEvent(QKeyEvent *event) override;
//MyRectItem .cpp void MyRectItem::keyPressEvent(QKeyEvent *event) { if (!flags().testFlag(QGraphicsItem::ItemIsMovable)) return; if (!isSelected()) return; if(!bShiftPressed) switch (event->key()) { case Qt::Key_Left: moveBy(-1, 0); break; case Qt::Key_Right: moveBy(1, 0); break; case Qt::Key_Up: moveBy(0, -1); break; case Qt::Key_Down: moveBy(0, 1); break; case Qt::Key_Shift: bShiftPressed = true; } else switch (event->key()) { case Qt::Key_Left: moveBy(-10, 0); break; case Qt::Key_Right: moveBy(10, 0); break; case Qt::Key_Up: moveBy(0, -10); break; case Qt::Key_Down: moveBy(0, 10); break; } } void MyRectItem::keyReleaseEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Shift) bShiftPressed =false; }
-
I this case up and down arrow key are associated with scrollbar movment . If there is scrollbar movement , then these macros does =move but scrollbar is still moveBy works
Any suggestions on how to avoid this issue
-
I this case up and down arrow key are associated with scrollbar movment . If there is scrollbar movement , then these macros does =move but scrollbar is still moveBy works
Any suggestions on how to avoid this issue
@Qt-Enthusiast said in Want to implement rectangle movements similar to in microsoft power point in my QGraphicsView:
I this case up and down arrow key are associated with scrollbar movment . If there is scrollbar movement , then these macros does =move but scrollbar is still moveBy works
Could you please rephrase this? I really cannot understand what you mean.
-
In my case the UP key moves the scrollbar up ,
but with Shift+Up key with the code you suggested the Shift+key moves the scroll bar up not the object
in this action on Up takes precedence on action on other key . COuld you let me know what should be done