Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved In a QTreewidget, how could I retrieve selected items...?

    General and Desktop
    3
    11
    5531
    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.
    • M
      MSDQuick last edited by

      Hello;

      I have a QTreewidget in my project, and I would like to retrieve list of selected items, when I select them using dragging mouse over them:
      0_1504881966314_Selection.jpg

      Any idea?
      (or tutorial?)

      Thanks.
      MSD.

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

        signal: treeWidget->selectionModel(),&QItemSelectionModel::selectionChanged
        getter: treeWidget->selectionModel()->selectedIndexes()

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        M 1 Reply Last reply Reply Quote 2
        • M
          MSDQuick @VRonin last edited by

          @VRonin
          Thank you VRonin thanks for reply, but I'm new in QT and I don't know how should I use Signals and Slots :(

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

            @MSDQuick
            Hi
            The treeWidget->selectionModel()->selectedIndexes() returns a list.
            http://doc.qt.io/qt-5/qitemselectionmodel.html#selectedIndexes

            QModelIndexList selection = yourTableView->selectionModel()->selectedRows();
            
            // Multiple rows can be selected
            for(int i=0; i< selection.count(); i++)
            {
                QModelIndex index = selection.at(i);
                qDebug() << index.row();
            }
            

            You can then use a for loop to process the selected.

            To use the signal, you need a slot function that matches the signal
            void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)

            So you need a function like
            void xxxxx::TreeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
            }
            And connect signal to slot
            connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), this, SLOT(TreeSelectionChanged(const QItemSelection&,const QItemSelection&)));

            M 1 Reply Last reply Reply Quote 1
            • M
              MSDQuick @mrjj last edited by MSDQuick

              @mrjj
              You mean I have to put this in my mainwindow.h file :

              public slots:
                  void TreeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
              

              and this in mainwindow.cpp :

              void MainWindow::TreeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
              {
                  QModelIndexList selection =  ui->myTreeView->selectionModel()->selectedRows();
              
                    for(int i=0; i< selection.count(); i++)
                      {
                          QModelIndex index = selection.at(i);
                          qDebug() << index.row();
                       }
              }
              
              

              Right?

              mrjj M 2 Replies Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @MSDQuick last edited by mrjj

                @MSDQuick
                Hi , you are mixing a little.
                The TreeSelectionChanged is fired each time you select one
                so processing the list there might not be what you want.

                Depending on your design. Do you press a button after you have selected some?

                You can use the

                 QModelIndexList selection =  ui->myTreeView->selectionModel()->selectedRows();
                
                      for(int i=0; i< selection.count(); i++)
                        {
                

                without using the signal. Like in a button when pressing ok. etc.

                1 Reply Last reply Reply Quote 1
                • M
                  MSDQuick @MSDQuick last edited by

                  @MSDQuick
                  I have a TreeView click() event in my project.
                  So, what I want is : as soon as I click and drag the mouse in the TreeView , the signal be fired.

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

                    @MSDQuick
                    Ok then TreeSelectionChanged is what you want.

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

                      @mrjj
                      And is this correct:

                      void MainWindow::on_myTreeView_clicked(const QModelIndex &index)
                      {
                          ui->myTreeView->selectionModel(),&QItemSelectionModel::selectionChanged;    
                      }
                      
                      mrjj 1 Reply Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion @MSDQuick last edited by

                        @MSDQuick
                        Not at all.
                        In constructor of mainwindow, connect the signal and slot
                        connect(treeWidget->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), this, SLOT(TreeSelectionChanged(const QItemSelection&,const QItemSelection&)));

                        Then the treeWidget call your slot.

                        You should read over this
                        http://doc.qt.io/qt-5/signalsandslots.html

                        M 1 Reply Last reply Reply Quote 1
                        • M
                          MSDQuick @mrjj last edited by

                          @mrjj
                          Oh thanks, I got it. It works now...

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