Results 1 to 5 of 5

Thread: SetSelection QListView Pyqt

  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11 Windows

    Default SetSelection QListView Pyqt

    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...
    Last edited by Chez; 28th June 2010 at 18:08. Reason: updated contents

  2. #2
    Join Date
    Jun 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: SetSelection QListView Pyqt

    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
    Last edited by Chez; 30th June 2010 at 10:58.

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SetSelection QListView Pyqt

    Hi there!

    I Only had to do that with QListWidgets before. But try setCurrentIndex.
    Qt Code:
    1. QModelIndex index = listView->model()->index(rowYouWantToSelect,0);
    2. listView->setCurrentIndex(index);
    To copy to clipboard, switch view to plain text mode 

    By default the selectionMode is QItemSelectionModel::SelectCurrent so that the current item is selected as well. If its not you can force this:
    Qt Code:
    1. listView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
    To copy to clipboard, switch view to plain text mode 

    Hope you can translate that to pyqt!

    HIH

    Johannes

  4. #4
    Join Date
    Jun 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: SetSelection QListView Pyqt

    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.

  5. #5
    Join Date
    Jun 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: SetSelection QListView Pyqt

    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.

  6. The following user says thank you to Chez for this useful post:

    fschober (25th March 2020)

Similar Threads

  1. lineEdit's setSelection - How to show more text?
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2010, 17:22
  2. QLibrary in PyQt
    By Urthas in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 18:46
  3. PyQt : Signal like this
    By prashant in forum Qt Programming
    Replies: 5
    Last Post: 2nd October 2009, 20:03
  4. PyQt
    By helvin in forum Newbie
    Replies: 1
    Last Post: 15th April 2009, 08:56
  5. setSelection() in custom view
    By krudio in forum Qt Programming
    Replies: 13
    Last Post: 6th February 2008, 20:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.