In QT4 and PyQT4 we have 3 list widgets -
List View is a simple list,
Tree View and a
Table View. Those widgets are available as parts of the lists framework or as "plain" widgets (Model and Item Based) We will use the second option - "Item Based":
For this example I used
Tree View. When you right-click on the widget a menu will appear with "
Edit Items" option, which allows us to add columns to the widget. The important thing is that each column has it number starting from 0.:
When you look at
QTreeWidget documentation note that it inherits
QTreeView, which has a lot of useful methods.
On cheeseshop there is
Yolk for managing installed with
easy_install Python modules. Install it with:
easy_install yolk
And then:
yolk -l
And you will see all installed packages. We will make such list in PyQT4 with QTreeWidget. The data will be provided by:
from yolk import yolklib
packages = yolklib.Distributions()
for pkg in packages.get_distributions('all'):
print[0]
print[1]
print '#####'
The
get_distributions method returns a generator with package name + version and package status (Active/not-active). I've made a simple GUI in QTDesigner with a QTreeWidget (name
treeList) in a window named
QYolk. I've saved the GUI as
qyolk.ui and generating
qyolk.py:
Here is a base version of
start.py:
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from qyolk import Ui_QYolk
from yolk import yolklib
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_QYolk()
self.ui.setupUi(self)
# set the widths of the columns
self.ui.treeList.setColumnWidth(0,200)
self.ui.treeList.setColumnWidth(1,100)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
The
setColumnWidth is from QTreeView and it sets the column widths. "0" is the first column, and "1" is the second one. Now we need to add items to the list. We have to use
QTreeWidgetItem:
a = QtGui.QTreeWidgetItem(self.ui.treeList)
a.setText(0, 'a')
a.setText(1, 'b')
a.setText(2, 'c')
QTreeWidgetItem requires a
QTreeWidget to which the item should be added. Method
setText(Column ID, Text) is used to set the text of a column. For lists or generators we use a loop,
start.py:
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from qyolk import Ui_QYolk
from yolk import yolklib
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_QYolk()
self.ui.setupUi(self)
# set the widths of the columns
self.ui.treeList.setColumnWidth(0,200)
self.ui.treeList.setColumnWidth(1,100)
# generator which retuns list of installed packages
packages = yolklib.Distributions()
for pkg in packages.get_distributions('all'):
a = QtGui.QTreeWidgetItem(self.ui.treeList)
pk = str(pkg[0]).split(' ')
if pkg[1]:
status = 'Active'
else:
status = 'Not Active'
a.setText(0, pk[0])
a.setText(1, pk[1])
a.setText(2, status)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
And the list is read. We can extend the application with more features. For example
setTextColor (for QTreeWidgetItem) changes the text colour. Why not make not active packages grey?:
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from qyolk import Ui_QYolk
from yolk import yolklib
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_QYolk()
self.ui.setupUi(self)
# set the widths of the columns
self.ui.treeList.setColumnWidth(0,200)
self.ui.treeList.setColumnWidth(1,100)
# generator which retuns list of installed packages
packages = yolklib.Distributions()
for pkg in packages.get_distributions('all'):
a = QtGui.QTreeWidgetItem(self.ui.treeList)
pk = str(pkg[0]).split(' ')
if pkg[1]:
status = 'Active'
else:
status = 'Not Active'
a.setTextColor(0, QtGui.QColor(128, 128, 128))
a.setTextColor(1, QtGui.QColor(128, 128, 128))
a.setTextColor(2, QtGui.QColor(128, 128, 128))
a.setText(0, pk[0])
a.setText(1, pk[1])
a.setText(2, status)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
setTextColor(Column ID, QtGui.QColor(R, G, B)) - Where R, G, B is a colour in the RGB notation. Finally, I got a PyQT4 application that looks like this:
Download Sources
- Added: 14.07.2008 by riklaunim