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

zar*_*zar 4 c++ qt

我试图解决这个问题,似乎我必须使用QItemSelectionModel,但我找不到如何连接的示例.

我在.h文件中定义了.

QItemSelectionModel* selectionModel;
Run Code Online (Sandbox Code Playgroud)

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

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) ); 
Run Code Online (Sandbox Code Playgroud)

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

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

我也尝试用QModelIndex替换QItemSelection但仍然不起作用.

我需要做什么才能在选择发生变化时获得通知,而不是显然获取新选择的项目?

azf*_*azf 5

的QObject ::连接方法应采用如下:

QObject::connect(sender, SIGNAL(signal_method), receiver, SLOT(slot_method));
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下应该是这样的

connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), this, SLOT(mySelectionChanged(const QItemSelection&,const QItemSelection&)));
Run Code Online (Sandbox Code Playgroud)