How to get notified of a change in QTreeView

I am trying to figure this out and it seems to me that I should use QItemSelectionModel, but I cannot find an example of how to connect things.

I defined in the .h file.

QItemSelectionModel* selectionModel;

Now in the view constructor, I install:

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) ); 

I thought there would be a predefined slot, but I cannot find it, so I added it (the syntax of which I found here )

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

I also tried replacing QItemSelection with QModelIndex, but it still doesn't work.

What do I need to do to just get notified when the selection has been changed, and obviously grab the newly selected item?

+4
source share
1 answer

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&)));
+5

All Articles