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

时间:2015-02-16 19:57:08

标签: c++ qt

我试图解决这个问题,似乎我必须使用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) ); 

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

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

我也尝试用QModelIndex替换QItemSelection,但仍然无法正常工作。

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

1 个答案:

答案 0 :(得分:5)

应使用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&)));
相关问题