Results 1 to 10 of 10

Thread: How to set selected item of a QListWidget?

  1. #1
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set selected item of a QListWidget?

    I have an instance of QListWidget which gets refreshed frequently,
    (by refreshing I mean removing all items then re-filling)

    so after refreshing I want it to select the first item, while it is by default selecting none.

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    this might work for u:

    Qt Code:
    1. void [QTCLASS]QItemSelectionModel[/QTCLASS]::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    oops i put it wrong:
    Qt Code:
    1. void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set selected item of a QListWidget?

    Thanks for replying,
    I saw that function in Qt Assistant, but I don't really know much about the "Qt model/view framework", so I was hoping for an example that demonstrates selecting the first item...

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    ok so, QListWidget inherits QListView..QListView inherits QAbstractItemView which is the base class of all views..QAbstractItemView provides many functions that'll allow u modify your view at the basic level..u can do what u want in a number of ways.

    Qt Code:
    1. QListWidget *list = new QListWidget();
    2. list->setSelectionMode(QAbstractItemView::SingleSelection);
    3.  
    4. QModelIndex modelIndex = list->rootIndex(); // u have to find the model index of the first item here
    5. list->setCurrentIndex(modelIndex);
    To copy to clipboard, switch view to plain text mode 

    before going further ahead, give a read to http://doc.trolltech.com/4.4/model-v...roduction.html just an hour of reading this will give u a lot of information about mvc in qt

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

    Lawand (5th April 2009)

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    so after refreshing I want it to select the first item, while it is by default selecting none.
    It wont select itself since you deleted all the items. Aftter filling call this -
    Qt Code:
    1. listWidget->item(0)->setSelected(true);
    To copy to clipboard, switch view to plain text mode 
    and you are done This is assuming you want to seleced first item, as you said. For other row, you select some other row.

    QModelIndex modelIndex = list->rootIndex();
    Am afraid in case of listwidget this might not be valid. Also when you talk about QListWidget, you talk in terms of rows and QListWidgetItem.
    You are not concerned about the internal behaviour. QListWidget manages its own model internally, and you dont have access to it.

  8. The following user says thank you to aamer4yu for this useful post:

    Lawand (5th April 2009)

  9. #7
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    yes, u r right, u r not concerned about those values when using QListWidget..just because the guy is new to it..i was explaining in terms of mvc itself..i wrote there are number of ways to do it .. plus yes rootIndex() might not be valid..i just wanted to show the use of QModelIndex..and how one should get model indexes

  10. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    just because the guy is new to it..i was explaining in terms of mvc itself..
    Were u trying to scare him lolz...

  11. #9
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set selected item of a QListWidget?

    well, I tried this one:
    Qt Code:
    1. QListWidget *list = new QListWidget();
    2. list->setSelectionMode(QAbstractItemView::SingleSelection);
    3.  
    4. QModelIndex modelIndex = list->rootIndex(); // u have to find the model index of the first item here
    5. list->setCurrentIndex(modelIndex);
    To copy to clipboard, switch view to plain text mode 

    but it didn't work, so I am gonna stick with this one for now:
    Qt Code:
    1. listWidget->item(0)->setSelected(true);
    To copy to clipboard, switch view to plain text mode 

    and I'll try to read the model-view-introduction sometimes later...


    Thanks for your help.

  12. #10
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set selected item of a QListWidget?

    I don't think he could have gotten scared of 4 lines of code wrote it just to motivate him to read abt mvc..thats not wrong i guess..and it's not working cuz of the model index..I guess I should hv saved myself all the trouble & wrote ur one line example..but that's how it is

Similar Threads

  1. How to get parent of selected item in TreeView?
    By RavenS in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2009, 13:27
  2. Changing selected item color in non-current window.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2007, 07:09
  3. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 16:00
  4. Replies: 1
    Last Post: 19th April 2007, 22:23
  5. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 09:56

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.