我有一个基本的QTableWidget,用下面的python代码创建:
from silx.gui import qt
app = qt.QApplication([])
qtw = qt.QTableWidget()
qtw.show()
qtw.setColumnCount(8)
qtw.setRowCount(7)
app.exec_()
from silx.gui import qt
行只是一个包装器,用于查找已安装的qt包装器(PyQt4、PyQt5或PySide)并展平qt名称空间。
当我编辑一个单元格时,得到的表格有一种奇怪的行为:正如预期的那样,当我双击单元格时,旧文本被高亮显示,但不寻常的行为是,当我键入旧文本时,旧文本仍然可见,并且新文本与旧文本重叠,直到我按enter键或单击另一个单元格。
我希望当我开始输入新文本时,旧文本就会消失。我知道这是可能的,因为我有一个程序的例子,它具有我想要的行为的qTableWidget。
但是我找不到程序中单元格编辑行为被改变的地方。我怎么才能做到这一点?
“垃圾邮件”和“鸡蛋”叠加的示例。
[
编辑:没有包装器业务的代码示例
from PyQt5.Qt import QApplication, QTableWidget, qVersion
app =QApplication([])
print(qVersion())
qtw = QTableWidget()
qtw.show()
qtw.setColumnCount(8)
qtw.setRowCount(7)
app.exec_()
对于PyQt4,使用此导入(同时删除print(qVersion())
行):
from PyQt4.QtGui import QApplication, QTableWidget
发布于 2017-08-15 19:50:42
我的方法是:
class MyDelegate(QItemDelegate):
def setEditorData(self,editor,index):
editor.setAutoFillBackground(True)
editor.setText(index.data().toString())
发布于 2016-09-09 01:15:05
通常,编辑行为是通过QItemDelegates
控制的。通常,这样做是为了提供更高级的编辑,或者在进行编辑时过滤输入数据或执行一些副作用(如更新数据库)。但您也可以使用它在编辑时仅清除呈现给用户的编辑器。
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)
发布于 2017-08-30 10:31:29
在我的例子中,当我将QWidgetTable的背景颜色设置为透明时,出现了上述问题。当我删除设置时,不再有旧数据覆盖新数据。希望能有所帮助。
https://stackoverflow.com/questions/39387842
复制相似问题