Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Want to implement rectangle movements similar to in microsoft power point in my QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

Want to implement rectangle movements similar to in microsoft power point in my QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 5.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Devopia53

    @Qt-Enthusiast

    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);
    [...]
    
    
    Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #12

    @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

    J.HilkJ 1 Reply Last reply
    0
    • Q Qt Enthusiast

      @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

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #13

      @Qt-Enthusiast
      @Devopia53 pretty much wrote it out for you. Instead of the default QGraphicsItem you use the custom class MyRectItem he defined for you.

      the last section between the 2 [...] even show you how to add a MyRectItem to your graphicsView.

      Not much else can be done here


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #14

        One more question If I have pressed Shift + Up key what should be value of event->key()

        J.HilkJ 1 Reply Last reply
        0
        • Q Qt Enthusiast

          One more question If I have pressed Shift + Up key what should be value of event->key()

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #15

          @Qt-Enthusiast

          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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #16

            A small code will be helpful again

            J.HilkJ 1 Reply Last reply
            0
            • Q Qt Enthusiast

              A small code will be helpful again

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #17

              @Qt-Enthusiast

              //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;
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #18

                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

                jsulmJ 1 Reply Last reply
                0
                • Q Qt Enthusiast

                  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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #19

                  @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.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #20

                    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

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by Qt Enthusiast
                      #21

                      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

                      https://paste.ofcode.org/9kgHwu2FBRiWvQsQpryu3f

                      J.HilkJ 1 Reply Last reply
                      0
                      • Q Qt Enthusiast

                        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

                        https://paste.ofcode.org/9kgHwu2FBRiWvQsQpryu3f

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #22

                        @Qt-Enthusiast

                        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 :)


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved