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. In a QTreewidget, how could I retrieve selected items...?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 8.1k Views 2 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    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
    2
    • VRoninV VRonin

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

      M Offline
      M Offline
      MSDQuick
      wrote on last edited by
      #3

      @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 :(

      mrjjM 1 Reply Last reply
      0
      • M MSDQuick

        @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 :(

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

        @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
        1
        • mrjjM 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 Offline
          M Offline
          MSDQuick
          wrote on last edited by MSDQuick
          #5

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

          mrjjM M 2 Replies Last reply
          0
          • M 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?

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

            @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
            1
            • M 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?

              M Offline
              M Offline
              MSDQuick
              wrote on last edited by
              #7

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

              mrjjM 1 Reply Last reply
              0
              • M MSDQuick

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

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

                @MSDQuick
                Ok then TreeSelectionChanged is what you want.

                M 1 Reply Last reply
                0
                • mrjjM mrjj

                  @MSDQuick
                  Ok then TreeSelectionChanged is what you want.

                  M Offline
                  M Offline
                  MSDQuick
                  wrote on last edited by
                  #9

                  @mrjj
                  And is this correct:

                  void MainWindow::on_myTreeView_clicked(const QModelIndex &index)
                  {
                      ui->myTreeView->selectionModel(),&QItemSelectionModel::selectionChanged;    
                  }
                  
                  mrjjM 1 Reply Last reply
                  0
                  • M MSDQuick

                    @mrjj
                    And is this correct:

                    void MainWindow::on_myTreeView_clicked(const QModelIndex &index)
                    {
                        ui->myTreeView->selectionModel(),&QItemSelectionModel::selectionChanged;    
                    }
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @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
                    1
                    • mrjjM mrjj

                      @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 Offline
                      M Offline
                      MSDQuick
                      wrote on last edited by
                      #11

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

                      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