Hey there,
So I’m building a little tool for myself that gives the user the option of files to choose. But I want to make sure they’ve selected something.
Right now the Qlistwidget returns a default value of the first item, but I would like it to return a none value.
self.listWidget = QtGui.QListWidget(self)
for vdu in vduList:
self.listWidget.addItem(vdu)
self.listWidget.move(10,10)
self.listWidget.setFixedHeight(90)
self.listWidget.setFixedWidth(230)
self.vdu = self.listWidget.currentItem().text()
print self.vdu
The lame way would be to append a null vlaue to the beginning of the list but I’m sure there must be a better way?
use .selectedItems() to get a list of items that are selected. You can also use the underlying QItemSelectionModel by calling .selectionModel() and then access several methods that are helpful when it comes to selections.
Thanks. The behaviour is now correct.
But print self.listWidget.selectedItems() returns the PyQt object but it doesn’t have a text method to convert it to a string.
Is there a conversion function? Didn’t see it in the documentation.
Sorry! Just very new to PyQt.
EDIT: in the meantime, I just set the list to select the first item so atleast it’s apparant to the user.
Hmm…I was trying the .text() but it kept telling me that method didn’t exist. But I’m thinking maybe the result was nested inside a list (files at work).
I’ll check when I head in tomorrow.
for i in self.listWidget.selecteditems():
print i.text()
Or if self.listWidget.currentItem().text() returns some text even if you don’t select any items,
maybe this is because of focus policy.
I experienced similar issue working with older version of PyQt though it was QTableWidget
@hsith, didn’t know about the focus issues. I’m going to investigate those.
I think our system builds are a little outdated for Qt and Py (well definitely for py). Some of the bugs are pretty arbitrary.
BTW, just out of curiosity, anyone got any tips/links on doing the following:
Having a listWidget or similar with a listing of the scene assets along with a checkbox next to each asset listing.
I’d also like to be able to drag and drop the order of the listings instead of a button that goes up or down.
I know I’ve seen a tutorial on this but for the life of me I can’t find it. Got a old-fashioned solution in place till I can update it.
Download and install Qt for c++ so you get your hand on the documentation, Qt Assistant.exe, it’s amazing and by just looking at the documentation about QListWidget you should be able to figure out how to make the listWidget implementation and it owns anything you find on the net.
I can suggest two ways to do this.
The first way would be just sticking with QListWidget and use the already implemented functionality for all of the functions you are looking for (checkbox, drag / drop reordering) etc. But this couples your actual data to the actual QListWidgetItems that QListWidget operates with, don’t worry about this if you DON’T plan on having multiple QListWidgets alive and synced.
The second way would be to use a QListView and reimplement QAbstractListModel and then use listView.setModel( model ). This way you can set the same model on several views and don’t worry about syncing them. But for all the requirements you described above you would need to reimplement 7 methods on the subclass of QAbstractListModel which would be
.data(…) // returns data to view to display, need to act on QtCore.Qt.DisplayRole and QtCore.Qt.CheckStateRole. The rest are optional.
.setData(…) // need to ONLY act on QtCore.Qt.CheckStateRole to set checked or not on the data
.rowCount(…) // returns how many items view should display.
.mimeTypes(…) // returns what drag / drop types this model can accept, you define the type names to whatever you want.
.mimeData(…) // returns the actual data that should be dragged out of the view owning this model.
.dropMimeData(…) // accepts the data that is dropped on top of the view owning this model.
.flags(…) // what operations are allowed (drag, drop, click, select) etc.
If you need to stick with second approach you will need to read up on QListView and QAbstractListModel and this should be piece of cake. Other than that just like I said if you don’t need to sync several views to each other I would just stick with the first approach as that will be alot faster to implement. We are talking about time and not complexity as both are relatively trivial to implement.
@Lonewolf, Thanks for the really comprehensive post.
Going to spend some time this weekend trying it out at home. (all the Qt SDK apps at work don’t render properly)