Platform:
    Win32  PyQt4.3.0-GPL  with Qt-4.3.1-opensource,  Python 2.5
Symptoms:
    QTableWiget sort by a long string column, after remove some rows,
then sort again will deak lock or crashed.  Sometimes can got a msg
from console:
    "Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there."

Test Program:
   see attachment: testTbw.py
Usage:
   run testTbw.py   and click some cell wait a while program will crashed.

Maybe it's caused by a GC problem or TableWidget's model r/w competition.
Anyone can help me find root cause and how to fix it?

-- 
I'm the one, powered by nEO
#!/usr/bin/env python
#coding=utf-8


import sys
from random import randint
from PyQt4.QtGui import QTableWidget,QMainWindow,QWidget,QApplication
from PyQt4.QtGui import QVBoxLayout, QPushButton
from PyQt4.QtCore import QTimer, SIGNAL, QModelIndex, QVariant, Qt


def genLongString():
    longString = ''
    #gen a 128 chars string
    for i in range(128):
        randAscii = chr(randint(64, 122))
        longString += randAscii
    return longString

class mainWindow(QMainWindow):

    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        layout = QVBoxLayout()
        self.addTimer = QTimer()
        self.delTimer = QTimer()
        self.table = QTableWidget(self.centralWidget)
        self.setupTable()
        self.btn_remove = QPushButton("&Remove", self.centralWidget)
        layout.addWidget(self.table)
        layout.addWidget(self.btn_remove)
        self.connect(self.btn_remove, SIGNAL("clicked()"), self.removeRows)
        self.connect(self.addTimer, SIGNAL("timeout()"), self.insertRows)
        self.connect(self.delTimer, SIGNAL("timeout()"), self.removeRows)
        self.resize(300, 300)
        self.centralWidget.setLayout(layout)

    def setupTable(self):
        #self.table.setRowCount(1)
        self.table.setColumnCount(7)
        self.table.hideColumn(5)
        self.table.sortByColumn(6)
        self.table.resizeColumnsToContents()
        self.table.resizeRowsToContents()

    def insertRows(self):
        model = self.table.model()
        self.table.setSortingEnabled(False)
        model.insertRows(0, 3)
        for r in range(3):
            for c in range(6):
                s = "%s-%s" % (r,c)
                idx = model.index(r, c, QModelIndex())
                model.setData(idx, QVariant(s), Qt.DisplayRole)
            #Fill col 7 with a long string
            longString = genLongString()
            idx = model.index(r, 6)
            model.setData(idx, QVariant(longString), Qt.DisplayRole)
        self.table.setSortingEnabled(True)
        self.table.resizeColumnsToContents()
        self.table.resizeRowsToContents()
        self.table.sortByColumn(6)

    def removeRows(self):
        row = self.table.currentRow()
        if row == -1:
            print "Please click any cell for select a row to be remove."
        print "remove row:",row
        self.table.removeRow(row)


app = QApplication(sys.argv)
main = mainWindow()
main.show()
main.insertRows()
# If uncoment below line, will cause a dead loop, seem a bug too.
#main.table.setCurrentCell(1,2)
main.addTimer.start(1*1000)
main.delTimer.start(1*1100)
app.exec_()
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to