Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Move item Line with Line Edit.

    General and Desktop
    3
    9
    1817
    Loading More Posts
    • 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.
    • AlvaroS
      AlvaroS last edited by

      Hi everyone.
      First of all thanks for helping me.

      I have some LineItems in a Scene represented by QGraphicsView.
      I would like to add 4 LineEdit and one button.

      The idea is the next:
      the Line Edits are: right, left, up, down.
      The button is: move lines.

      If the user selects and item and put for example: 3 in right and 0 in the left, up and down. The line selected is moved 3 coordinates in X to the right.

      I thought that I can do that If I can access to coordinates of selected items and then just add the number of user puts in LIneEdit.

      but how can I access to that¿

      I add lines with:

      line = scene->addLine(x_start,y_start,x_end,y_end, red);
      

      Thanks a lot!

      1 Reply Last reply Reply Quote 0
      • A
        Alvaro.sal last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          hi
          "but how can I access to that¿"

          I assume you mean the line?
          If selected then u can get it via
          http://doc.qt.io/qt-5/qgraphicsscene.html#selectedItems

          Note: that you might need to make the item selecteable
          http://doc.qt.io/qt-5/qgraphicsitem.html#setFlags
          QGraphicsItem::ItemIsSelectable

          AlvaroS 1 Reply Last reply Reply Quote 1
          • AlvaroS
            AlvaroS @mrjj last edited by

            @mrjj Thanks for answering.

            Yes, the line selected or lines selected.
            But I need the coordinates of the line (x_start, y_start, x_end,y_end)
            I did this:

            QList<QGraphicsItem*>a;
                a = scene->selectedItems();
            
                int num_item_selected;
                num_item_selected = a.size(); //Num of items selected.
            
                for (int i=0; i<num_item_selected; i++)
                {
                    QRectF itemBR = a.value(i)->boundingRect();
                    QPointF sceneBR = a.value(i)->scenePos();
                    //std::cout << sceneBR << std::endl;
                }
            
            

            but I can not get the coordinates...

            1 Reply Last reply Reply Quote 1
            • mrjj
              mrjj Lifetime Qt Champion last edited by

              It seems fine
              Did you read about
              http://doc.qt.io/qt-5/graphicsview.html#the-graphics-view-coordinate-system

              scenePos() is correct so you must be getting other values than
              expected?

              If you cast to an actual QGraphicsLineItem

              QGraphicsLineItem * line=qobject_cast<QGraphicsLineItem * >(a.value(i));
              if ( line ){
              http://doc.qt.io/qt-5/qgraphicslineitem.html#line
              }
              Maybe those values are the ones you expect ?

              AlvaroS 1 Reply Last reply Reply Quote 1
              • AlvaroS
                AlvaroS @mrjj last edited by

                @mrjj Yes, the scenePost() retunr always 0,0 ... It does not matter what line I select, the value is always 0,0 ...

                When I write your code it return me an error which is:

                error: no matching function for call to 'qobject_cast(QGraphicsItem*)'
                QGraphicsLineItem * linea =qobject_cast<QGraphicsLineItem * >(a.value(i));
                ^

                        QGraphicsLineItem * linea =qobject_cast<QGraphicsLineItem * >(a.value(i));
                        if (linea)
                        {
                            linea->line;
                        }
                
                1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion last edited by

                  @AlvaroS said:
                  oh sorry
                  try with qgraphicsitem_cast

                  AlvaroS 1 Reply Last reply Reply Quote 0
                  • AlvaroS
                    AlvaroS @mrjj last edited by

                    @mrjj That works fine.
                    Thanks a lot!!!

                    mrjj 1 Reply Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @AlvaroS last edited by

                      @AlvaroS
                      super. :)
                      If possible please mark as solved using the topic button.

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post