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. How to set the current index of a tree view to the selected row?
Forum Updated to NodeBB v4.3 + New Features

How to set the current index of a tree view to the selected row?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 1.8k Views 3 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.
  • M Offline
    M Offline
    mpergand
    wrote on last edited by
    #4

    try:

    treeView->setSelectionMode(QAbstractItemView::SingleSelection);
    
    I 1 Reply Last reply
    0
    • I Infinity

      @SGaist It works with the flags QItemSelectionModel::SelectCurrent | QItemSelectionModel::Clear, but there is still one caveat. It only selects the first column in the tree view. How can I make sure that the whole row is selected? I tried it with:

      m_ui->treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
      

      but this seems not to work for a tree view.

      I want to edit the data of the selected row in a form. When the user clicks cancel I would like to reestablish the original data. This is done in on_selectionChanged. That is the reason why I want to trigger selection changed again.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @Infinity said in How to set the current index of a tree view to the selected row?:

      I want to edit the data of the selected row in a form. When the user clicks cancel I would like to reestablish the original data. This is done in on_selectionChanged. That is the reason why

      To me if you modify the content of an entry without using the standard delegates to do so, the actual change should be submitted only when the OK button is pressed so if cancel is, you don't have to do anything.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      0
      • M mpergand

        try:

        treeView->setSelectionMode(QAbstractItemView::SingleSelection);
        
        I Offline
        I Offline
        Infinity
        wrote on last edited by
        #6

        @mpergand Still the same.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #7

          Looking at my ui, I see:
          selectionBehavior=SelectItems

          Hope it helps ...

          1 Reply Last reply
          0
          • SGaistS SGaist

            @Infinity said in How to set the current index of a tree view to the selected row?:

            I want to edit the data of the selected row in a form. When the user clicks cancel I would like to reestablish the original data. This is done in on_selectionChanged. That is the reason why

            To me if you modify the content of an entry without using the standard delegates to do so, the actual change should be submitted only when the OK button is pressed so if cancel is, you don't have to do anything.

            I Offline
            I Offline
            Infinity
            wrote on last edited by
            #8

            @SGaist I don't use delegates. I have a stackedWidget next to the treeView with an edit button. If the edit button is clicked, a new form is opened in the stackedWidget. This form has an accept button and a cancel button and some lineEdit fields. The lineEdit fields are populated with the values of the treeItem. When the accept button is clicked, the values the treeItem will be updated. This part works fine. When the cancel button is clicked the lineEdit fields should be repopulated with the values of the threeItem. I populate the lineEdit fields when on_selectionChanged is called. Therefore my plan was to call on_selectionChanged again on the same row. Does that make sense?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #9

              A simpler way would be to keep these values as defaults when you load them within your editor and reload them when cancel is called. This has the benefit of decoupling your editor and your model as well as tree view properly

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              I 1 Reply Last reply
              0
              • SGaistS SGaist

                A simpler way would be to keep these values as defaults when you load them within your editor and reload them when cancel is called. This has the benefit of decoupling your editor and your model as well as tree view properly

                I Offline
                I Offline
                Infinity
                wrote on last edited by
                #10

                @SGaist That sounds like a pretty good plan.

                How can I implicitly share (shallow copy) a treeItem?

                Liek this?

                TreeItem * m_treeItem = (treeItem);
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  That's just a copy of a pointer.

                  What is your code currently to load the data related to an item ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  I 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    That's just a copy of a pointer.

                    What is your code currently to load the data related to an item ?

                    I Offline
                    I Offline
                    Infinity
                    wrote on last edited by
                    #12

                    @SGaist Like this

                    void FormAccount::on_accountSelectionChanged(SqlTreeItemAccount *accountTreeItem)
                    {
                        qDebug() << "account form selection Changed";
                    
                        // Get the selected tree item
                        m_accountTreeItem = accountTreeItem;
                    
                        m_ui->lineEditLabel->setText(m_accountTreeItem->value("label").toString());
                    }
                    

                    I would like to have a shallow copy of accountTreeItem in m_accountTreeItem.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      From the looks of it, you only want a copy of the content of the tree item.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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