The Gooier the Better :)

It’s time to get GUI with our AddressBook object.  There is a file named qttable.py in the HSAPPS N: drive that creates the table shown below with some sample data displayed.

qt-table

Copy the file to your H: drive and test it.  If you see the table above you’re on the right track.

Now the challenge.  Instead of a list of someData objects being displayed, we want our address book data to be displayed.  To do this we need to add out AddressBook and Contact classes to the program.  Then, we replace the list data with our .contactlist and adjust the number of columns to accommodate our data.  After these changes are implemented, the table now looks like the one below.

qtaddresstable

The structure of your program will look something like this:

#!/usr/bin/env python

# Sample code to display a simple table of data using Qt PySide

import sys 
from PySide import QtGui, QtCore

# **********************************************************
# Sample Data Class for display in table
# **********************************************************

     # Put your AddressBook and Contact class definitions here!!!!
 
# **********************************************************
# TableWindow GUI class to display someData
# **********************************************************
class TableWindow(QtGui.QWidget):
 
	def __init__(self, parent=None):

		# Sample column headers
		title = [ #Put column headings here!!! ]
		
                # Make myAddressBook here !!!
                # Make myAddressBook here !!!
                # Make myAddressBook here !!!
                # Make myAddressBook here !!!

		# Set up Window
		super(TableWindow, self).__init__(parent)

		# Add a table to the window
		colcnt = len(title)
		rowcnt = len(data)
		self.tablewidget = QtGui.QTableWidget(rowcnt, colcnt)

		# Add a vertical header to the table
		vheader = QtGui.QHeaderView(QtCore.Qt.Orientation.Vertical)
		vheader.setResizeMode(QtGui.QHeaderView.ResizeToContents)
		self.tablewidget.setVerticalHeader(vheader)

		# Add a horizontal header to the window
		hheader = QtGui.QHeaderView(QtCore.Qt.Orientation.Horizontal)
		hheader.setResizeMode(QtGui.QHeaderView.ResizeToContents)
		self.tablewidget.setHorizontalHeader(hheader)

		# Set the titles for the horizontal header (column headings)
		self.tablewidget.setHorizontalHeaderLabels(title)

		# Fill the table with objects from the data list

                # CHANGE THE FOR LOOP BELOW TO DISPLAY YOUR INFO INSTEAD OF MINE
                # CHANGE THE FOR LOOP BELOW TO DISPLAY YOUR INFO INSTEAD OF MINE
                # CHANGE THE FOR LOOP BELOW TO DISPLAY YOUR INFO INSTEAD OF MINE

		for i in range(rowcnt):
			#Each of these item/self.tablewidget pairs adds a column of data
			item = QtGui.QTableWidgetItem(str(data[i].name))
			self.tablewidget.setItem(i, 0, item)

			item = QtGui.QTableWidgetItem(str(data[i].rank))
			self.tablewidget.setItem(i, 1, item)

			item = QtGui.QTableWidgetItem(str(data[i].serial))
			self.tablewidget.setItem(i, 2, item)
 
		# Add the table to the window layout
		layout = QtGui.QHBoxLayout()
		layout.addWidget(self.tablewidget)
		self.setLayout(layout)
 
# **********************************************************
# Main Program DO NOT CHANGE THE MAIN PROGRAM!!!!!!!
# **********************************************************
app = QtGui.QApplication(sys.argv)
widget = TableWindow()
widget.show()
widget.raise_()
sys.exit(app.exec_())
 

When you get the program working, please submit using this submission link.