[PyQt] Trouble implementing a TableView model

Bernard Van Der Stichele Bernard_VanDerStichele at otpp.com
Tue Aug 3 13:09:25 BST 2010


A while back, I had asked the group how I could implement a numeical sort 
on a TableWidget.
Someone kindly showed me how to implement a model, associated with a 
TableView. The sample code was posted here: 
http://diotavelli.net/PyQtWiki/Sorting%20numbers%20in%20columns
 
I'm having problems implementing this in my app. In this simple example, I 
use Qt Designer to create a MainForm containing a TableView (gridView), a 
LineEdit (queryBox) and a PushButton (runQueryBtn).
The designer files (.ui and ui_xxx.py) generated defines the TableView in 
a couple of lines - I have not used the Designer to change any of the 
default object properties.
 
The problem I have is that the TableView remains blank. I do not 
understand what I'm missing, as I tried to follow the same workflow as 
provided by the code linked-to above..
The code works, in that the data I want to display is retrieved from a 
database and I receive no runtime warnings or errors.
 
Any help is greatly appreciated... this is probably one of those simple 
newbie errors.
 
My application shell is as follows:
 
import os
import platform
import sys
import locale
import random
from PyQt4.QtCore import  *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
from ui_SortedTableView import *
 
class NumberSortModel(QSortFilterProxyModel): 
    def lessThan(self, left, right):
        lvalue = left.data().toDouble()[0]
        rvalue = right.data().toDouble()[0]
        return lvalue < rvalue

class AppShell(QMainWindow,Ui_MainWindow):
    def __init__(self,parent=None):
        super(AppShell,self).__init__(parent)
        self.setupUi(self)
        self.updateUi()
 
    def updateUi(self):
        
self.connect(self.runQuerybtn,SIGNAL("clicked()"),self.runQuerySlot)
        #setup the grid:
        #self.gridView.setMinimumSize(1000,600)
        self.queryBox.setText("Select Issuer, Cusip, Holding From Bonds 
Where Holding> 1000")
        #fill grid with initial data set:
        self.runQuery()
 
    def runQuerySlot(self):
        #run query with current sql string input:
        self.runQuery()
##
    def runQuery(self):
        self.query = self.queryBox.text()
        self.updateGrid()
 
    def updateGrid(self):

        data = self.getData()
        
        proxy = NumberSortModel()
        
        self.gridView.setSortingEnabled(False)
        print len(data), len(data[0])
        model = QStandardItemModel(len(data),len(data[0]))
       # model = QStandardItemModel(15,5)
        
        r = -1
        for row in data:
            r=r+1
            c=-1
            for col in row:
                c=c+1
                item = QStandardItem()
                item.setData(QVariant(col),Qt.DisplayRole)
                model.setItem(r,c,item)
            
        #retrieve headings from the fields in the Select query (does not 
work for 'Select * ' queries)
        querystring = str(self.query)
        querystring=querystring.upper()
        cut1 = querystring.find("SELECT ")+5
        cut2 = querystring.find("FROM")
        labels = querystring[cut1+1:cut2].split(",")
    
        for i in range(len(labels)):
                       proxy.setHeaderData(i,Qt.Horizontal, 
QVariant(str(labels[i])))
       
        proxy.setSourceModel(model)

        self.gridView.setModel(proxy)
        self.gridView.setSortingEnabled(True)
        
    def getData(self):
       ........ this retrieves data from a database and fills a List 
('datatable'). Each list item is itself a list of size 3.....
        return datatable
    
       
if __name__=="__main__":
    import sys
    app = QApplication(sys.argv)
    form=AppShell()
    form.show()
    app.exec_()
    





CONFIDENTIALITY WARNING
This e-mail message, including any attachment(s), is confidential. If we sent this communication to you in error, please do not disclose it to anyone else or use the information in it. Please notify the sender of the transmission error and then delete our communication from your system without printing, copying or forwarding it.  Thank you for your co-operation.

AVERTISSEMENT CONCERNANT LE CARACTERE CONFIDENTIEL DE L'INFORMATION

Le present courriel, y compris toute piece qui y est jointe, est confidentiel. Si nous vous avons envoye cette communication par erreur, nous vous prions de ne la divulguer a personne ni a utiliser l’information qu’elle contient. Veuillez informer l’expediteur de l’erreur de transmission et effacer ensuite notre communication de votre systeme sans l’imprimer, ni la copier ni la retransmettre. Nous vous remercions de votre cooperation.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100803/c48791a9/attachment.html>


More information about the PyQt mailing list