Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selection changed notification in QTreeView

Tags:

c++

qt

I am trying to figure this out and it seems like I have to use QItemSelectionModel but I can't find an example how to wire things up.

I have defined in .h file.

QItemSelectionModel* selectionModel;

Now in constructor of the view, I set:

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 predefined slot but I can't find one so I added this one (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 still doesn't work.

What do I need to do in order to simply get notified when selection has changed and than obviously grab the newly selected item?

like image 380
zar Avatar asked Feb 16 '15 19:02

zar


1 Answers

the QObject::connect method should be used as follow :

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

so in your case it should be something like

connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), this, SLOT(mySelectionChanged(const QItemSelection&,const QItemSelection&)));
like image 187
azf Avatar answered Sep 24 '22 12:09

azf