[PyQt] Bugs galore in QAbstractTableModel???

Ian hobson42 at gmail.com
Sat Nov 27 21:27:55 GMT 2010

On 27/11/2010 21:07, Andreas Pakulat wrote:
> On 27.11.10 20:54:01, Ian wrote:
>> I am trying to use QAbstractTableModel and I am having more than
>> some difficulty.
>> If I return the correct number to columnCount I get no headers. If I
>> return a number that is too big, I get headers, but the model is
>> asked for headers and data for columns that don't exist!
>> Everywhere I return a String in the data()  routine, this is
>> displayed with a check box - even if I cast it to QVariant.
> There's a C++ class called QModelText which sanity-checks models, I
> believe that an older version was converted to python and is included in
> PyQt4. Run it on your model, fix the problems and see wether that helps.
Thanks for your reply Andreas,
I can find nothing about QModelText, and QModelTest appears to have a 
few bug reports and
nowhere to download it and no instructions as to how to run it, and is 
not on my hard disks.

So here are the model and view:

# coding=utf8
#  companyListModel.py   class of CompanyList
import couchdb
from couchObject import CouchObject
import json
from PyQt4.QtCore import *   # so no internal routines starting with Q!
from PyQt4.QtGui import *
import pprint

class CompanyListModel(QAbstractTableModel,CouchObject):
     ''' a company list holds data from a view of Companies '''

     def __init__(self):
         ''' initialise - read the view ready '''
         self.view = self.loadView('by_name')

     def rowCount(self, parent = None):
         ''' return No of rows of data. parent is a QModelIndex '''
         return len(self.view)

     def columnCount(self, parent = None):
         ''' return number of columns. parent = QModelIndex()
            id, name, cubref, address, town, contacts
         return 6

     def data(self, index, role):
         ''' return data as QVariant at index.row and index.col '''
         key = self.view.rows[index.row()].key
         idx = index.column()
         if  idx < len(key):
             val = key[idx]
             if val is None:
                 return QVariant()
             return QVariant(val)
         return QVariant()

     def all_fields(self):
         ''' return list of fields in view'''
         return [

     def headerData(self, col, orientation, role):
         ''' return the header data '''
         if orientation == Qt.Horizontal:
             tab = ['Name','Cub Ref','Street','Town','Contacts']
             if col < len(tab):
                 return tab[col]
         return None

# coding=utf8
#   companyListView - a company Listing window
import couchdb
from company import Company
from couchDbServer import Couch
from uuid import uuid4
import sys

from PyQt4.QtCore import *   # so no internal routines starting with Q!
from PyQt4.QtGui import *
from mainWindow import CubMainWindow
from companyListModel import CompanyListModel
# for testing
import pprint

class CompanyListView(CubMainWindow):
     ''' a window that lists Companies '''

     def __init__(self, cubic, name=None, parent=None):
         ''' initialise CompanyListView window '''
         print "NoteEditor initialising"
         self.setWindowTitle('Companies - No Company Selected')
         self.cubic = cubic
         self.model = CompanyListModel()
         # build panel with a VBoxLayout, a QTableView on top, and a 
HBoxLayout below with two buttons

         listPane = QWidget()
         layout = QVBoxLayout()
         # config tableView
         self.tableView = QTableView()
         scrollArea = QScrollArea()
         # add buttons
         addCompanyButton = QPushButton("&Add Company")
         removeCompanyButton = QPushButton("&Remove Company")
         quitButton = QPushButton("&Quit")
         buttonLayout = QHBoxLayout()
         # link up headers to change order
         header = self.tableView.horizontalHeader()
         # link buttons
         self.connect(addCompanyButton, SIGNAL('clicked()'), 
         self.connect(removeCompanyButton, SIGNAL('clicked()'), 
         self.connect(quitButton, SIGNAL('clicked()'), self.accept)
         QTimer.singleShot(10, self.initialLoad)

     def sortTable(self,section):
         ''' sortTable called '''
         print "sortTable called"
         print "Section is %s" % section

     def addCompany(self):
         ''' do addCompany action '''
         print 'addCompany called'

     def removeCompany(self):
         ''' remove comapny action '''
         print 'removeCompany Called'

     def accept(self):
         ''' quit '''

     def initialLoad(self):

More information about the PyQt mailing list