如何在QTreeView中获取选择更改通知

How to get selection changed notification in QTreeView

本文关键字:选择 通知 获取 QTreeView      更新时间:2023-10-16

我正在努力解决这个问题,似乎我必须使用QItemSelectionModel,但我找不到如何连接的示例。

我已经在.h文件中定义了。

QItemSelectionModel* selectionModel;

现在在视图的构造函数中,我设置:

selectionModel = ui->treeView->selectionModel();
// the following line is not compiling!
connect(ui->treeView->selectionModel(), SIGNAL( ui->treeView->selectionModel(const QModelIndex&, const QModelIndex &) ),
        this, this->selectionChanged ( QItemSelection & sel,  QItemSelection & desel) ); 

我以为会有预定义的插槽,但我找不到,所以我添加了这个(我在这里找到的语法)

void MyDialog::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    qDebug() << "Item selection changed";
}

我还尝试用QModelIndex替换QItemSelection,但仍然不起作用。

当选择发生变化时,我需要做什么才能得到通知,而不是明显地抓住新选择的项目?

QObject::connect方法应按如下方式使用:

QObject::connect(sender, SIGNAL(signal_method), receiver, SLOT(slot_method));

所以在你的情况下,它应该是类似的东西

connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), this, SLOT(mySelectionChanged(const QItemSelection&,const QItemSelection&)));