QYolk I - List widgets in PyQt4
Check out the new site at https://rkblog.dev.
14 July 2008
Comments
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.QYolk
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 '#####'
# -*- 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_())
a = QtGui.QTreeWidgetItem(self.ui.treeList)
a.setText(0, 'a')
a.setText(1, 'b')
a.setText(2, 'c')
# -*- 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_())
# -*- 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_())
Download
Download Sources
RkBlog
Check out the new site at https://rkblog.dev.
Comment article