PDA

View Full Version : SetSelection QListView Pyqt



Chez
28th June 2010, 16:58
Hi, All.

My name is Chez, I'm new here and this is my first post on these forums, so hope the newbie section is the correct place...I'm an absolute beginner with QT as well.

I'm using pyqt with Python 2.6 in a deb environment.

Short Question: How do I select an item in a single column QListView class using setSelection...

Some details:

I have a QListView class called listBox. I fill the QlistView with a model (simple list of one column data) called listModel...this is made up of a pure python list.

I have a python class for my list handling, of which one of the methods deletes multiple occurrences of items in the list. On the pyqt side I am using a Signal for changing selection :

self.connect(self.listBox.selectionModel(),SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),self.slot)

and a method called slot...this is all ok...

However, long story short, what I want to be able to do is change the selected item in the QListView class listBox after I have deleted items from the model. I have looked at setSelection but I really have no clue what I am doing here, so any help would be useful.


List = [One, Two, One, Two, Three, One]

I want to via code select any item in the list....so that it is highlighted.

Thanks for any help you can provide.

p.s. A definitive example would be appreciated...

Chez
30th June 2010, 10:53
Ok Obviously I worded this request wrongly, let me see if I can make this more simple:

If I have a QListView listBox and a model for that view listModel which contains the following items

One
Two
Three
Four
Five

How do I, via code, make 'Four' the selection?

Thanks

JohannesMunk
30th June 2010, 11:16
Hi there!

I Only had to do that with QListWidgets before. But try setCurrentIndex.

QModelIndex index = listView->model()->index(rowYouWantToSelect,0);
listView->setCurrentIndex(index);

By default the selectionMode is QItemSelectionModel::SelectCurrent so that the current item is selected as well. If its not you can force this:

listView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);

Hope you can translate that to pyqt!

HIH

Johannes

Chez
30th June 2010, 17:27
Ok, guys, thanks for the pointers, but I managed it a few hours back. Another look at the online manuals, which, take note OT developers, are not as easy to follow as an ageing programmer would like, and I found the solution, simple really:

1. Create a QModelIndex for your model
index = self.listModel.index(0, 0, QModelIndex())

Use the QModelIndex as the first argument to a select for you selectionModel, which can either be the default selection model or another instantiation that can be passed to other views...and then use the same selection model as the second argument but with the Select method...
2. self.mySelectionModel.select(index, self.mySelectionModel.Select );

That's a selection I won't forget.

Chez
30th June 2010, 17:46
Ok, guys, thanks for the pointers, but I managed it a few hours back. Another look at the online manuals, which, take note OT developers, are not as easy to follow as an ageing programmer would like, and I found the solution, simple really:

1. Create a QModelIndex for your model
index = self.listModel.index(0, 0, QModelIndex())

Use the QModelIndex as the first argument to a select for you selectionModel, which can either be the default selection model or another instantiation that can be passed to other views...and then use the same selection model as the second argument but with the Select method...
2. self.mySelectionModel.select(index, self.mySelectionModel.Select );

That's a selection I won't forget.