PiRK
PiRK

Reputation: 1055

not displaying old value when editing cell in a QTableWidget

I have a basic QTableWidget, created with this python code:

from silx.gui import qt
app = qt.QApplication([])
qtw = qt.QTableWidget()
qtw.show()
qtw.setColumnCount(8)
qtw.setRowCount(7)
app.exec_()

The from silx.gui import qt line is just a wrapper that finds out the installed qt wrapper (PyQt4, PyQt5 or PySide) and flattens the qt namespace.

The resulting table has a strange behavior when I edit a cell: as expected, the old text is highligted when I double-click the cell, but the unusual behavior is that the old text remains visible and the new text overlaps with the old one while I'm typing it, until I press enter or I click another cell.

I would like the old text to disappear as soon as I start typing the new one. I know it's possible, because I have an example of program that features a qTableWidget with the behavior I would like to have.

But I cannot find where in that program the cell editing behavior is altered. How can I do this?

Example of "spam" and "eggs" overlayed.

[example of "spam" and "eggs" overlayed[1]

EDIT: the code sample without the wrapper business

from PyQt5.Qt import QApplication, QTableWidget, qVersion
app =QApplication([])
print(qVersion())
qtw = QTableWidget()
qtw.show()
qtw.setColumnCount(8)
qtw.setRowCount(7)
app.exec_()

With PyQt4, use this import (also remove the print(qVersion()) line):

from PyQt4.QtGui import QApplication, QTableWidget

Upvotes: 2

Views: 1584

Answers (4)

Quitapenas
Quitapenas

Reputation: 11

In my case, above problem comes when I set background color of QWidgetTable to transparent. When I remove the setting, there is no old data overlays the new one anymore. Hope it helps.

Upvotes: 1

Ernie
Ernie

Reputation: 21

My method:

class MyDelegate(QItemDelegate):

    def setEditorData(self,editor,index):
        editor.setAutoFillBackground(True)
        editor.setText(index.data().toString())

Upvotes: 2

Brendan Abel
Brendan Abel

Reputation: 37519

Generally, edit behavior is controlled via QItemDelegates. Typically, this is done to provide more advanced editing, or to filter input data or perform some side effects (like update a database) when edits are made. But you can also use it to just clear the editor presented to the user when editing.

class MyDelegate(QItemDelegate):

    def setEditorData(self, editor, index):
        # Normally, this would set the text of the editor to the current
        # value of the cell.  If you do nothing here, it will be blank.
        editor.clear()

qtw = QTableWidget()
delegate = MyDelegate(qtw)
qtw.setItemDelegate(delegate)

Upvotes: 1

Drayke
Drayke

Reputation: 397

You could try connecting the signal emited by QTableWidget cellClicked(int row, int column) with a slot created for clearing the entry.
http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html#connecting-disconnecting-and-emitting-signals

Upvotes: 0

Related Questions