Skip to content

Instantly share code, notes, and snippets.

Created May 18, 2017 01:14
Show Gist options
  • Save anonymous/9d368e6ba274763fedf3be2c46b49be5 to your computer and use it in GitHub Desktop.
Save anonymous/9d368e6ba274763fedf3be2c46b49be5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
from PySide import QtGui, QtCore
# Big thing to takeaway. You mostly work on models. You can totally customize the views, but
# for simple things you're basically just building a model and letting the view display it for you.
def simple():
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.setWindowTitle('Simple View')
lay = QtGui.QVBoxLayout()
widget.setLayout(lay)
# Now let's create a model. This model will be boring and won't have much going on.
listOfThings = ['Thing1', 'Thing2', 'Thing3', 'Thing4', 'Thing5']
model = QtGui.QStandardItemModel()
for thing in listOfThings:
# We'll use a boring standard item. This basically holds information about
# a particular 'cell' in the model. If this was a more complex data set this item
# could have children as well
thingItem = QtGui.QStandardItem()
# Now that we have an item we can set some data on that item. We'll use a 'role'
# for the data. Each role can have a different purpose for the view.
# In QT there a number of built in roles that the view will understand automagically.
# The ones I use most are...
# QtCore.Qt.DisplayRole
# QtCore.Qt.DecorationRole
# QtCore.Qt.ToolTipRole
# Check the docs for more info.
# For now we'll only care about the DisplayRole. It's what the user will see in the interface
thingItem.setData(thing, QtCore.Qt.DisplayRole)
# Since this is a listview we'll only have one item in our row. If you had more items in this row
# you could pass it to a tableview or tell the view which column of data to display in the listview.
model.appendRow(thingItem)
# Create a view. Nothing crazy to do.
view = QtGui.QListView(widget)
# Tell our view what model it's using
view.setModel(model)
lay.addWidget(view)
widget.show()
sys.exit(app.exec_())
def notSoSimple():
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.setWindowTitle('Simple View')
lay = QtGui.QVBoxLayout()
widget.setLayout(lay)
# In this one we'll add some extra data to our model.
dictOfThings = {'thing1' : ['really important info for thing1', 'Thing1 Not Normal Data'],
'thing2' : ['really important info for thing2', 'Thing2 Not Normal Data'],
'thing3' : ['really important info for thing3', 'Thing3 Not Normal Data'],
'thing4' : ['really important info for thing4', 'Thing4 Not Normal Data']}
model = QtGui.QStandardItemModel()
for thing, data in dictOfThings.iteritems():
thingItem = QtGui.QStandardItem()
# Now I want my user to see 'thing1' as a title, and not how it's stored under the hood
# So I will set my display data accordingly
thingItem.setData(thing.title(), QtCore.Qt.DisplayRole)
# I have some important info I want displayed as a tooltip, so I'll put that in here too
thingItem.setData(data[0], QtCore.Qt.ToolTipRole)
# Now we get to the even more fun part! I have some strange data that I want to be related to this item,
# but there's not a normal place to put it. No ItemDataRole exists for my weird data.
# Now we can use a UserRole. You can basically have any number of UserRoles and you can define what
# each one does according to your application needs.
# I normally don't use UserRole directly, but start it incremented.
originalNameRole = QtCore.Qt.UserRole + 1
coolDataRole = QtCore.Qt.UserRole + 2
# If it's not clear, these Roles are just an enum representing an integer. UserRole are anything 32 and up.
thingItem.setData(thing, originalNameRole)
thingItem.setData(data[1], coolDataRole)
model.appendRow(thingItem)
view = QtGui.QListView(widget)
view.setModel(model)
lay.addWidget(view)
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
# simple()
notSoSimple()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment