PDA

View Full Version : Connect list items



ecce
12th April 2015, 09:41
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

wysota
12th April 2015, 11:06
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.

ecce
12th April 2015, 11:50
Thanks! Worked perfectly, although I'm not sure excatly what I'm doing. Lack of Python knowledge, probably. Here's a snippet of code:


class MainWindow(QtGui.QMainWindow):

def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("Test application")
self.setGeometry(0, 0, 1000, 600)

itemList = QtGui.QListView()
itemList.setMaximumWidth(150)
self.itemlistModel = QtGui.QStandardItemModel(itemList)
itemList.setModel(self.itemlistModel)

# Add item
item1 = QtGui.QStandardItem()
item1.setText("Item1")
self.itemlistModel.appendRow(item1)

item2 = QtGui.QStandardItem()
item2.setText("Item2")
self.itemlistModel.appendRow(item2)
itemList.clicked.connect(self.itemClicked)

def itemClicked(self, index):
print "Clicked: " + str(index.row())



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

anda_skoa
12th April 2015, 11:59
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,
_