Want to implement rectangle movements similar to in microsoft power point in my QGraphicsView
-
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
-
To have only one action on Shift+UP key I have put return in Key_UP case in the following code https://paste.ofcode.org/9kgHwu2FBRiWvQsQpryu3f. As I i undesstand the Shift+Up will override on the key press event binding . Is that OK ?
Do you see problem in the following code
-
To have only one action on Shift+UP key I have put return in Key_UP case in the following code https://paste.ofcode.org/9kgHwu2FBRiWvQsQpryu3f. As I i undesstand the Shift+Up will override on the key press event binding . Is that OK ?
Do you see problem in the following code
From my brief overview, it looks good.
Just one thing, try not to use qt's
foreach
makro, it will be deprecated soon in Qt 5.9 or later.Better to adjust now than revisit all your code when you updated :)