VVcvcv
VVcvcv

Reputation: 31

How can I implement multiselection in Qt in TreeView when user holds key CTRL on keyboard ?

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    MyFileSystemModel model;
};

My file cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    model.setRootPath(QDir::rootPath());

     ui->treeView->setModel(&model);
     ui->treeView->setSelectionMode(QAbstractItemView::MultiSelection);
     ui->treeView->setDragEnabled(true);
}

I used the MultiSelection property, but it works only when I click on items in treeview with mouse, I don't want to select a few items without pressing CTRL on keyboard. How can I check if user pressed CTRL and then select items ?

Upvotes: 3

Views: 456

Answers (1)

Farhad
Farhad

Reputation: 4181

QTreeview has a virtual function to set selection mode.

You can set the mode to multiselection like this:

QTreeView treeView;
treeView.setSelectionMode(QAbstractItemView::MultiSelection);

Also for Multiselection with ctrl key use this:

QTreeView treeView;
treeView.setSelectionMode(QAbstractItemView::ExtendedSelection);

more info about QTreeview here.

Upvotes: 4

Related Questions