[PyQt] Signal on Selection in QTreeView

fossks at cox.net fossks at cox.net
Mon Mar 17 17:22:05 GMT 2008

  I'm trying to use a list that contains a media library react to a user clicking on an item in the list.  When clicked, the details of the item would show up in a GroupBox elsewhere on the screen.  However, at this time I'm a little stumped on Lists/Trees/Models/Views.

   I'm including the function that is called when the library is opened.  You can see that I have a connection to the listview call (self.lv).   I also have a backup in case this doesn't work which is a button to get the details.  However, I would like to have it such that whichever item is selected/highlighted, would get shown in the item details group box.

  I've likely missed the implementation of this by a mile, and I need some help.    I've checked for examples but either they don't seem to do the same as I want or they are too complicated.

  Please let me know if anyone has some advice.


______________________________ Code Below _________________________

    def openlibrary(self):
	if self.library_opened:
		self.library_opened = True
	print "open library"
	self.index = 0
	self.total_list = []
# open library file
# read in each line

	libfile = open(self.MEDIA_LOC + "/mpyqt_library/library.txt","r")
	for count in libfile.readlines():			
		entry = self.parse(count)
		print entry
		self.total_list = self.total_list + [entry]

	list_data = self.total_list
	self.lm = QStandardItemModel(0,10)
	self.lm.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Type"))
	self.lm.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("Title"))
	self.lm.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("File Location"))
	self.lm.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Artists"))
	self.lm.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("Album"))
	self.lm.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant("Length"))
	self.lm.setHeaderData(6, QtCore.Qt.Horizontal, QtCore.QVariant("Resolution"))
	self.lm.setHeaderData(7, QtCore.Qt.Horizontal, QtCore.QVariant("Encoding"))
	self.lm.setHeaderData(8, QtCore.Qt.Horizontal, QtCore.QVariant("Image Location"))
	self.lm.setHeaderData(9, QtCore.Qt.Horizontal, QtCore.QVariant("Remarks"))

	for i in range(len(list_data)):
		self.lm.setData(self.lm.index(0, 0), QtCore.QVariant(list_data[i][0]))
		self.lm.setData(self.lm.index(0, 1), QtCore.QVariant(list_data[i][1]))
		self.lm.setData(self.lm.index(0, 2), QtCore.QVariant(list_data[i][2]))
		self.lm.setData(self.lm.index(0, 3), QtCore.QVariant(list_data[i][3]))
		self.lm.setData(self.lm.index(0, 4), QtCore.QVariant(list_data[i][4]))
		self.lm.setData(self.lm.index(0, 5), QtCore.QVariant(list_data[i][5]))
		self.lm.setData(self.lm.index(0, 6), QtCore.QVariant(list_data[i][6]))
		self.lm.setData(self.lm.index(0, 7), QtCore.QVariant(list_data[i][7]))
		self.lm.setData(self.lm.index(0, 8), QtCore.QVariant(list_data[i][8]))
		self.lm.setData(self.lm.index(0, 9), QtCore.QVariant(list_data[i][9]))
	self.lv = QTreeView()

	self.connect(self.lv, SIGNAL("currentTextChanged(const QString&)"), self.item_selected)
	self.detailsButton = QPushButton("Get Item Details")
	self.connect(self.detailsButton, SIGNAL('clicked()'), self.show_lib_entry)

More information about the PyQt mailing list