[PyQt] QTableWidget editing question

Baz Walter bazwal at ftml.net
Fri Nov 27 19:44:32 GMT 2009

Steve Zatz wrote:
> I am new to qt and pyqt but have been using wxPython for a long time.
> I am converting a moderately complex application from wxPython to pyqt
> and have been impressed with the ease of using pyqt.
> Here's my question and I apologize for the verbiage.  The ability to
> get at the default widget text editor in a QTableWidget appears pretty
> limited.  You can initiate editing (editItem) but can only indirectly
> try to determine if the editor is still open or has been recently
> closed.  I am using the return key to initiate editing of the cells in
> a particular column but the problem is when the user hits the return
> key while editing an item.  Normal user interface would have that
> return key close the editor but since I am trapping for the return the
> app tries to open editing on the item that is already being edited and
> writes "edit:  editing failed" to the console.  Now I am redirecting
> both stdout and stderr but this message appears to be written by qt
> directly to the console and is not going through pyqt.  So, sorry for
> the longwinded note but any thoughts on either or both of the
> following:
> 1) can that "editing failed" message be trapped somehow and/or

obviously, the warning message is caused by your attempt to override the 
default behaviour for initiating editing (which is plaform specific). 
but since you didn't post any code, it's hard to suggest what you could 
do about it :)

> 2) is there any straightforward way to know that a QTableWidget item
> is in the process of being edited

there are probably several different ways to do this. here's one:

import sys
from PyQt4.QtGui import QApplication, QTableWidget

class TableWidget(QTableWidget):
     def __init__(self, rows, columns):
         QTableWidget.__init__(self, rows, columns)

     def edit(self, index, trigger, event):
         if QTableWidget.edit(self, index, trigger, event):
             print 'editing:', index.row(), index.column()
             return True
         return False

if __name__ == '__main__':
     app = QApplication(sys.argv)
     table = TableWidget(3, 3)
     table.resize(350, 150)
     table.move(300, 300)

More information about the PyQt mailing list