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. Select items in a Scene when I press a button.
Forum Updated to NodeBB v4.3 + New Features

Select items in a Scene when I press a button.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 3.2k Views 1 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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    Hi there!!

    I have a scene with items. I would like to do that when user press a button, all the item can be selectable and then the users can select the item that they want.

    how can I do that? Which class should I use?
    Thanks a lot.

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

      hi
      Have you seen the
      http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html
      it can select an item and delete etc.

      You might need to set
      item->setFlag(QGraphicsItem::ItemIsSelectable, true);
      on your items.

      AlvaroSA 1 Reply Last reply
      0
      • mrjjM mrjj

        hi
        Have you seen the
        http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html
        it can select an item and delete etc.

        You might need to set
        item->setFlag(QGraphicsItem::ItemIsSelectable, true);
        on your items.

        AlvaroSA Offline
        AlvaroSA Offline
        AlvaroS
        wrote on last edited by
        #3

        @mrjj First of all thanks for answering.
        The problem is that I do not understand the way that users can select items.

        mrjjM 1 Reply Last reply
        0
        • AlvaroSA AlvaroS

          @mrjj First of all thanks for answering.
          The problem is that I do not understand the way that users can select items.

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

          @AlvaroS
          hi
          Well the framework can do it for you if you tell it its ok with
          youritem->setFlag(QGraphicsItem::ItemIsSelectable, true);

          Then user can just click on it.
          The default is no flag so you need this i think.

          AlvaroSA 1 Reply Last reply
          1
          • mrjjM mrjj

            @AlvaroS
            hi
            Well the framework can do it for you if you tell it its ok with
            youritem->setFlag(QGraphicsItem::ItemIsSelectable, true);

            Then user can just click on it.
            The default is no flag so you need this i think.

            AlvaroSA Offline
            AlvaroSA Offline
            AlvaroS
            wrote on last edited by
            #5

            @mrjj thanks again my friend.

            This is my code where I construc the scene:

                QGraphicsLineItem *line;
                QGraphicsScene *scene;
            
            for (int i=0;i<num_lines;i++)
                            {
                                words = lines.at(i).split(" ", QString::SkipEmptyParts); // Split each line in words
                                lines_struct[i].x_start= words.at(0).toDouble(); //We take the first word and convert it to Double because it was QString type before
                                lines_struct[i].y_start= words.at(1).toDouble(); //Second word
                                lines_struct[i].x_end= words.at(2).toDouble(); //Third word
                                lines_struct[i].y_end= words.at(3).toDouble(); //Fourth word
            
                                QPen blue(Qt::blue); //Draw lines in blue
                                blue.setWidth(0.7); //Lines width
                                line = scene->addLine((lines_struct[i].x_start),(lines_struct[i].y_start),(lines_struct[i].x_end),(lines_struct[i].y_end), blue);         
                            }
                                /*
                                 * Adjust scene (map) to graphicsview
                                 */
                               ui->graphicsView->scale(ui->graphicsView->width()/scene->width(),ui->graphicsView->height()/scene->height()); // Adjust map to the graphicsView using a scale
                               qreal x, y;
                               x=ui->graphicsView->width()/scene->width(); // Get scale X
                               y=ui->graphicsView->height()/scene->width(); // Get Scale Y
                               // line->setFlag(QGraphicsItem::ItemIsSelectable);
                               // line->setFlag(QGraphicsItem::ItemIsMovable);
                               ui->graphicsView->setFixedSize(10+(x*scene->width()),10+(y*scene->height())); //We fit the size of graphicsView just adding pixel to fitting well
                    }
            }
            }
            
            void MainWindow::on_Select_items_clicked()
            {
                line->setFlag(QGraphicsItem::ItemIsSelectable, true);
            }
            

            When User click on Select_items I want that they can select items that they want.
            But it does not work. (Qt does not give me errors)

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

              @AlvaroS said:
              Hi
              You need to set it on all items
              ...
              line = scene->addLine(xxx....
              line->setFlag(QGraphicsItem::ItemIsSelectable, true); // set for all 10
              }
              not later :)

              Did u try the Diagram sample ?
              Its available directly to run.

              AlvaroSA 1 Reply Last reply
              0
              • mrjjM mrjj

                @AlvaroS said:
                Hi
                You need to set it on all items
                ...
                line = scene->addLine(xxx....
                line->setFlag(QGraphicsItem::ItemIsSelectable, true); // set for all 10
                }
                not later :)

                Did u try the Diagram sample ?
                Its available directly to run.

                AlvaroSA Offline
                AlvaroSA Offline
                AlvaroS
                wrote on last edited by
                #7

                @mrjj Thanks a lot.
                That is work!
                How can I do to when the user is on item the mouse show a hand instead of a tipical arrow??

                Thanks again!!

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

                  hi
                  ui->widget->setCursor(Qt::OpenHandCursor);
                  Might need other hand. :)

                  And this to restore the cursor back to normal
                  ui->widget->setCursor(Qt::ArrowCursor);

                  AlvaroSA 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    hi
                    ui->widget->setCursor(Qt::OpenHandCursor);
                    Might need other hand. :)

                    And this to restore the cursor back to normal
                    ui->widget->setCursor(Qt::ArrowCursor);

                    AlvaroSA Offline
                    AlvaroSA Offline
                    AlvaroS
                    wrote on last edited by
                    #9

                    @mrjj Thanks a lot. It works fine for me but I have a problem.
                    First a put the code that it works fine because maybe it helps to someone:

                                        //QGraphicsLineItem line_scene[i] = line;
                                        line->setFlag(QGraphicsItem::ItemIsSelectable, true); // Every items can be selected
                                        line->setFlag(QGraphicsItem::ItemIsMovable, true);    //Every items can be movabled
                                        line->setCursor(Qt::PointingHandCursor); // When the cursor is on a line item it change to a hand cursor instead of a tipical arrow.
                    

                    Now I have a problem because the line items are too narrow and it is dificult to select them.

                    I did this:

                    //To draw items
                    
                                    QPen blue(Qt::blue); //Draw lines in blue
                                    blue.setWidth(0); //Lines width
                    
                                    line = scene->addLine(( ........... , blue);
                    
                    
                    //To fit scene to the graphicsView:
                    
                    ui->graphicsView->scale(ui->graphicsView->width()/scene->width(),ui->graphicsView->height()/scene->height()); // Adjust map to the graphicsView using a scale
                                       qreal x, y;
                                       x=ui->graphicsView->width()/scene->width(); // Get scale X
                                       y=ui->graphicsView->height()/scene->width(); // Get Scale Y
                                       ui->graphicsView->setFixedSize(5+(x*scene->width()),5+(y*scene->height())); //We fit the size of graphicsView just adding pixel to fitting well
                    

                    The problem is that when I write more than 0 in blue.widht, the line items are really thick and I think that it is because it scale the width as well in ui->graphicsView->scale.....

                    How can I solved that?

                    Thanks a lot!

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

                      hi
                      When you set a pen's width it will scale with there view
                      you can try
                      blue.setCosmetic(true);

                      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