Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • I have the following setup:

    connect(m_ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged,
            this, &CentralWidget::on_treeViewSelectionChanged);
    
    .
    .
    .
    void CentralWidget::on_treeViewSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    {
        Q_UNUSED(deselected)
    
        qDebug() << "selection" << selected;
        .
        .
        .
    }
    

    When I call

    m_ui->treeView->selectionModel()->setCurrentIndex(
        m_sqlTreeModel->index(itemIndex.row() + 1, 0, parentIndex),
        QItemSelectionModel::ClearAndSelect
    );
    

    Then selected in on_treeViewSelectionChanged contains a valid selection. But if I call it like this with the current row and not row + 1:

    m_ui->treeView->selectionModel()->setCurrentIndex(
        m_sqlTreeModel->index(itemIndex.row(), 0, parentIndex),
        QItemSelectionModel::ClearAndSelect
    );
    

    Then selected in on_treeViewSelectionChanged contains an invalid selection. What is the reason of this? Is there another way to trigger QItemSelectionModel::selectionChanged. I would like to trigger selectionChanged again, with the current selection. How can I achieve this?


  • Lifetime Qt Champion

    Hi,

    Can you explain why you want to trigger selection changed again on an already selected index ?

    By the way, did you check the other flags like Current ?



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



  • try:

    treeView->setSelectionMode(QAbstractItemView::SingleSelection);
    

  • Lifetime Qt Champion

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



  • @mpergand Still the same.



  • Looking at my ui, I see:
    selectionBehavior=SelectItems

    Hope it helps ...



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


  • Lifetime Qt Champion

    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



  • @SGaist That sounds like a pretty good plan.

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

    Liek this?

    TreeItem * m_treeItem = (treeItem);
    

  • Lifetime Qt Champion

    That's just a copy of a pointer.

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



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


  • Lifetime Qt Champion

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


Log in to reply