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. Items in QGraphicsScene are not movable
Forum Updated to NodeBB v4.3 + New Features

Items in QGraphicsScene are not movable

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 9.9k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #11

    Hi,

    Your original problem was that by not calling the base class method implementation, you don't get the behaviour from the base class.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2
    • faiszalkhanF Offline
      faiszalkhanF Offline
      faiszalkhan
      wrote on last edited by
      #12

      Since I am inheriting the QGraphicsItem in StateItem to create custom shape. Am I missing out something for setFlags(QGraphicsItem::ItemIsMovable); in the constructor to not work ?

      Thanks

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #13

        Hi
        You might need
        setFlag(QGraphicsItem::ItemIsMovable);
        setFlag(QGraphicsItem::ItemIsSelectable);

        If we look at
        http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html

        It all it does to flag the items.

        faiszalkhanF 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          You might need
          setFlag(QGraphicsItem::ItemIsMovable);
          setFlag(QGraphicsItem::ItemIsSelectable);

          If we look at
          http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html

          It all it does to flag the items.

          faiszalkhanF Offline
          faiszalkhanF Offline
          faiszalkhan
          wrote on last edited by
          #14

          @mrjj said in Items in QGraphicsScene are not movable:

          setFlag(QGraphicsItem::ItemIsMovable);
          setFlag(QGraphicsItem::ItemIsSelectable);

          Hi @mrjj , tried the same but still not working.

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #15

            Ok. cant tell what is wrong then.
            You might have overridden a virtual and forgot to call base class.

            Have a look at the diagram sample and see if can spot anything you
            did different.

            Or try the StateItem in a default scene/view.

            faiszalkhanF 1 Reply Last reply
            0
            • mrjjM mrjj

              Ok. cant tell what is wrong then.
              You might have overridden a virtual and forgot to call base class.

              Have a look at the diagram sample and see if can spot anything you
              did different.

              Or try the StateItem in a default scene/view.

              faiszalkhanF Offline
              faiszalkhanF Offline
              faiszalkhan
              wrote on last edited by
              #16

              @mrjj I just have the constructor, paint function and bounding rect. The file is as follows,

              StateItem::StateItem(QPointF pos)
              {

              /* Get the position from the caller */
              drawPos = new QPointF(pos.x(), pos.y());
              
              //TODO: Set item as movable
              

              // setFlags(QGraphicsItem::ItemIsFocusable);
              // setFlags(QGraphicsItem::ItemIsSelectable);
              setFlags(QGraphicsItem::ItemIsMovable);

              // QGraphicsItem::QGraphicsItem();
              }

              QRectF StateItem::boundingRect() const
              {
              //TODO: Return a rectangle of calculated size
              qreal penWidth = 1;
              return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
              20 + penWidth, 20 + penWidth);

              }

              /* Explicit call to this function is not required */
              void StateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget widget)
              {
              /
              Set the pen */
              QPen blackpen(Qt::black);
              blackpen.setWidth(2);
              painter->setPen(blackpen);

              /* Set colour of the state */
              QColor statecolor(229,231,233);
              QBrush statebrush(statecolor);
              painter->setBrush(statebrush);
              
              /* Draw state item */
              painter->drawRoundedRect(drawPos->x(),drawPos->y(),100,50,8,8); //x,y,width,height,xradius,yradius
              
              QPoint p(drawPos->x(),(drawPos->y() + 10));
              QPoint q((drawPos->x() + 100), (drawPos->y() + 10));
              QLine stateLine(p,q);
              painter->drawLine(stateLine);
              

              // QGraphicsItem::paint(painter,option,widget);

              }

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #17

                Hi
                Dont it need to be selectable to be moveable ?
                It all seems fine
                try use the DiagramTextItem from the diagram sample (its available directly in Creator)

                If that cant move, it means its your view or scene.

                //! [0]
                DiagramTextItem::DiagramTextItem(QGraphicsItem *parent)
                    : QGraphicsTextItem(parent)
                {
                    setFlag(QGraphicsItem::ItemIsMovable); // BOTH 
                    setFlag(QGraphicsItem::ItemIsSelectable);
                }
                //! [0]
                
                //! [1]
                QVariant DiagramTextItem::itemChange(GraphicsItemChange change,
                                     const QVariant &value)
                {
                    if (change == QGraphicsItem::ItemSelectedHasChanged)
                        emit selectedChange(this);
                    return value;
                }
                //! [1]
                
                //! [2]
                void DiagramTextItem::focusOutEvent(QFocusEvent *event)
                {
                    setTextInteractionFlags(Qt::NoTextInteraction);
                    emit lostFocus(this);
                    QGraphicsTextItem::focusOutEvent(event);
                }
                //! [2]
                
                //! [5]
                void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
                {
                    if (textInteractionFlags() == Qt::NoTextInteraction)
                        setTextInteractionFlags(Qt::TextEditorInteraction);
                    QGraphicsTextItem::mouseDoubleClickEvent(event);
                }
                
                faiszalkhanF 1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  Dont it need to be selectable to be moveable ?
                  It all seems fine
                  try use the DiagramTextItem from the diagram sample (its available directly in Creator)

                  If that cant move, it means its your view or scene.

                  //! [0]
                  DiagramTextItem::DiagramTextItem(QGraphicsItem *parent)
                      : QGraphicsTextItem(parent)
                  {
                      setFlag(QGraphicsItem::ItemIsMovable); // BOTH 
                      setFlag(QGraphicsItem::ItemIsSelectable);
                  }
                  //! [0]
                  
                  //! [1]
                  QVariant DiagramTextItem::itemChange(GraphicsItemChange change,
                                       const QVariant &value)
                  {
                      if (change == QGraphicsItem::ItemSelectedHasChanged)
                          emit selectedChange(this);
                      return value;
                  }
                  //! [1]
                  
                  //! [2]
                  void DiagramTextItem::focusOutEvent(QFocusEvent *event)
                  {
                      setTextInteractionFlags(Qt::NoTextInteraction);
                      emit lostFocus(this);
                      QGraphicsTextItem::focusOutEvent(event);
                  }
                  //! [2]
                  
                  //! [5]
                  void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
                  {
                      if (textInteractionFlags() == Qt::NoTextInteraction)
                          setTextInteractionFlags(Qt::TextEditorInteraction);
                      QGraphicsTextItem::mouseDoubleClickEvent(event);
                  }
                  
                  faiszalkhanF Offline
                  faiszalkhanF Offline
                  faiszalkhan
                  wrote on last edited by
                  #18

                  @mrjj Dont it need to be selectable to be moveable ?

                  No, Only setting it as moveable also works. I have standard QGraphicsRectItem and QGraphicsEllipseItem also in the same scene which are moving. So I don't think its the view or the scene.

                  :'(

                  mrjjM 1 Reply Last reply
                  0
                  • faiszalkhanF faiszalkhan

                    @mrjj Dont it need to be selectable to be moveable ?

                    No, Only setting it as moveable also works. I have standard QGraphicsRectItem and QGraphicsEllipseItem also in the same scene which are moving. So I don't think its the view or the scene.

                    :'(

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #19

                    @faiszalkhan
                    Ok, there is something i dont see then. :)
                    if you provide both StateItem.h and cpp
                    i can try run it and see.

                    faiszalkhanF 1 Reply Last reply
                    0
                    • sanojsubranS Offline
                      sanojsubranS Offline
                      sanojsubran
                      wrote on last edited by
                      #20

                      Hi,

                      Comment your overriden paint method for StateItem and check whether it is working.

                      Also please tell how you are updating the variable "drawPos".

                      Regards,
                      San

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @faiszalkhan
                        Ok, there is something i dont see then. :)
                        if you provide both StateItem.h and cpp
                        i can try run it and see.

                        faiszalkhanF Offline
                        faiszalkhanF Offline
                        faiszalkhan
                        wrote on last edited by faiszalkhan
                        #21
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • faiszalkhanF Offline
                          faiszalkhanF Offline
                          faiszalkhan
                          wrote on last edited by
                          #22
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • faiszalkhanF Offline
                            faiszalkhanF Offline
                            faiszalkhan
                            wrote on last edited by
                            #23

                            I had previously given a static bounding rect value as the previous implementation was static. Now that the value is dynamic I had not changed the contents of the bounding rect as the size has not changed from the previous implementation.

                            Making the changes in the bounding rect solved the problem.

                            Thanks a lot to @mrjj @sanojsubran @SGaist !

                            1 Reply Last reply
                            1

                            • Login

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