Results 1 to 4 of 4

Thread: Connect list items

  1. #1
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Connect list items

    Hi all,

    I'm creating a small application using PySide. The application has a list to the left (QListView at the moment, fiddeling around a bit) and a QStackedWidget to the right. When I click an item in the list, the corresponding widget should be poped from the stack.

    I'm stuck on connecting a click event in the list items. I've managed to make things happen if you check/uncheck a checkable QListView item, but I don't want the items to be checkable, just clicking on them should trigger the function.

    What's a suitable widget to use as a list? QListView, QListWidget or something else? How do you connect the model properly to achieve this behaviour? Right now I only need one column, but having the option to use more in the future would be nice.

    Cheers,

    ecce

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Connect list items

    You should connect signals from the view rather than those from the model. You click or otherwise select an item in the view. The model is not influenced in any way by your action. So one of proper signals to use is the clicked() signal in QListView.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    ecce (12th April 2015)

  4. #3
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Connect list items

    Thanks! Worked perfectly, although I'm not sure excatly what I'm doing. Lack of Python knowledge, probably. Here's a snippet of code:

    Qt Code:
    1. class MainWindow(QtGui.QMainWindow):
    2.  
    3. def __init__(self):
    4. QtGui.QMainWindow.__init__(self)
    5. self.setWindowTitle("Test application")
    6. self.setGeometry(0, 0, 1000, 600)
    7.  
    8. itemList = QtGui.QListView()
    9. itemList.setMaximumWidth(150)
    10. self.itemlistModel = QtGui.QStandardItemModel(itemList)
    11. itemList.setModel(self.itemlistModel)
    12.  
    13. # Add item
    14. item1 = QtGui.QStandardItem()
    15. item1.setText("Item1")
    16. self.itemlistModel.appendRow(item1)
    17.  
    18. item2 = QtGui.QStandardItem()
    19. item2.setText("Item2")
    20. self.itemlistModel.appendRow(item2)
    21. itemList.clicked.connect(self.itemClicked)
    22.  
    23. def itemClicked(self, index):
    24. print "Clicked: " + str(index.row())
    To copy to clipboard, switch view to plain text mode 

    Coming from the C++/PHP world, to me the index object appears out of nowhere. How does the itemClicked() function gets it's arguments?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect list items

    Quote Originally Posted by ecce View Post
    Coming from the C++/PHP world, to me the index object appears out of nowhere. How does the itemClicked() function gets it's arguments?
    That's no different than in C++.
    The index is the argument of the signal, see QAbstractItemView::itemClicked() and is passed to the slot connected to that signal.

    Cheers,
    _

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

    ecce (12th April 2015)

Similar Threads

  1. How to use QSignalMapper for a list of QAction items
    By Sai Kamat in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2014, 11:45
  2. How to use list of items?
    By Squall in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2012, 01:22
  3. Replies: 1
    Last Post: 23rd April 2011, 18:33
  4. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 14:06
  5. delete items from list box
    By vvbkumar in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2006, 20:08

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.