Select items in a Scene when I press a button.



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


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

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



  • @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)


  • Qt Champions 2016

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



  • @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!!


  • Qt Champions 2016

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

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



  • @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!


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.