QTreeView programmatic selection

TreeView programmatic selection

Create the UI as follows, drop QTreeView, a couple of QLineEdits, and a search QPushButton. The UI can look something like the following, with the TreeView on top:


Implement the code in mainwindow.h as follows:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <QMap>
#include <QModelIndex>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QStandardItemModel model;
    QItemSelectionModel *selectionModel;
    void init();
    QMap<QString,QModelIndex> searchItemToIndexMap;

private slots:
    void onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
    void on_pushButton_clicked();
};
#endif // MAINWINDOW_H 
Notice that the QMap holds a QString to QModelIndex mapping. This is so that when the user searches for a particular string, it is looked up in the dictionary and selected in the TreeView. Note also the selection model and the Standard Item model. on_pushButton_clicked() is triggered when the Search button is clicked. The code in mainwindow.cpp follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItem>
#include <QObject>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
   QStringList headers;
   headers << "My Items";
   model.setHorizontalHeaderLabels(headers);
   QModelIndexList list;

   QStandardItem *root = model.invisibleRootItem();
   for(int p = 0; p < 5; p++)
   {
       QStandardItem *parentItem = new QStandardItem(QString("Parent %0").arg(p));
       root->appendRow(parentItem);
       searchItemToIndexMap[QString("Item %1").arg(p)] = parentItem->index();
       for(int c = 0; c < 5; c++)
       {
           QStandardItem *childItem = new QStandardItem(QString("Child %0").arg(c));
           parentItem->appendRow(childItem);
           for(int s = 0; s < 5; s++)
           {
               QStandardItem *subchild = new QStandardItem(QString("SubChild %0").arg(s));
               childItem->appendRow(subchild);
           }
       }
   }
   ui->treeView->setModel(&model);
   selectionModel = new QItemSelectionModel(&model);
   ui->treeView->setSelectionModel(selectionModel);
   connect(selectionModel,&QItemSelectionModel::selectionChanged,this,&MainWindow::onSelectionChanged);
}

void MainWindow::onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    auto list = selected.indexes();
    if(list.length() == 0)
    {
        return;
    }
    auto firstIndex = list.first();
    auto selectedData = ui->treeView->model()->data(firstIndex).toString();
    ui->lineEdit->setText(selectedData);
}


void MainWindow::on_pushButton_clicked()
{
   if(searchItemToIndexMap.contains(ui->searchLineEdit->text()))
   {
       selectionModel->clearSelection();
       selectionModel->select(searchItemToIndexMap[ui->searchLineEdit->text()],QItemSelectionModel::Select);
   }
}
In the MainWindow constructor, the init function is called, which initializes the tree view model, selection model, and sets up the tree view. selectionChanged signal is connected to a slot.
When the button is clicked, the contents of the searchLineEdit are looked up in the dictionary to see if there is a corresponding QModelIndex. If there is, the select method of the selectionModel is invovked with that QModelIndex object.
Then, the onSelectionChanged slot is triggered. Here the list of selected indexes is obtained (only 1 index). The data function of the QStandardItemModel is called by passing in the QModelIndex object, and a QString representation is obtained and passed to the setText method of the lineEdit.
The final application looks similar to the following screenshot:

Comments

Popular posts from this blog

QTreeView and QTableView dynamic changes

C++ strings and string_view