Results 1 to 3 of 3

Thread: QListView (QT4.7): How can i get the selected Item

  1. #1
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4

    Default QListView (QT4.7): How can i get the selected Item

    Hi, i am total newbie at QT, and i'm writing a chat-application, whose GUI hast a
    QListView to show a list of Chat-Partners. Ich get a problem when i want to get the selected Item(supposed only one item can be selected) in the QListView: I've read int the QT4.7-DOC from nokia, but there are no such fuctions like firstChild(), sibling(),QListViewItemIterator(), which can be used to traverse the QListView. They have been there in old QT.

    Here ist the relevant code:
    in Chat.h:
    QStandardItemModel* buddyIconModel; //buddyIconModel cotains the buddies model.
    //in the Chat.ui file is a buddyList( inQListView ) defined.
    in Chat.cpp:
    buddyIconModel = new QStandardItemModel();
    this->findChild<QListView*>("buddyList")->setModel(buddyIconModel) ;

    supposed buddyIconModel has following dat
    (net1, name1)
    (net2, name2)
    (net3, name3)

    Now i try to write a slot to handle the clicked()-signal from the buddyList. I want to get the Name of the Selected Item. which function of QListView should i use? Ich have tried with the selectedIndexes(), but it is protected and the compiler reports error.

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListView (QT4.7): How can i get the selected Item

    Does this work?
    Qt Code:
    1. QModelIndexlist selected = buddyIconModel->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4

    Cool Re: QListView (QT4.7): How can i get the selected Item

    Quote Originally Posted by joyer83 View Post
    Does this work?
    Qt Code:
    1. QModelIndexlist selected = buddyIconModel->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 
    The compiler says it is Ok! Thank you!Still got two questions if you can take me forwards:
    buddyIconModel ist the Mode of the View buddyList. 1)Is this the same code when ich write like this?
    Qt Code:
    1. QModelIndexlist selected = this->findChild<QListView*>("buddyList")->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 
    2) What will the fuction return if there were no Item really selected by user? Null or a Pointer of QModelIndexlist in Null?

Similar Threads

  1. Replies: 14
    Last Post: 2nd October 2012, 03:10
  2. Replies: 1
    Last Post: 12th January 2011, 06:52
  3. popupmenu for QTreeWidget's item when a particular item selected.
    By vinod sharma in forum Best Practices in Qt Programming
    Replies: 1
    Last Post: 22nd January 2010, 10:45
  4. Replies: 1
    Last Post: 20th January 2010, 08:38
  5. QListview set selected item
    By Freeman551 in forum Qt Programming
    Replies: 1
    Last Post: 25th December 2009, 00:17

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.