我对 PyQt 相当陌生,我正在 PyQt4 和 Python 2.7 中制作一个 GUI,它(除其他外)控制一些微 Controller 的设置。为了轻松填充和显示设置列表以及它们来自哪个 Controller ,我将其放入 QTableWidget 中,其中列是 Controller ,行是设置(所有 Controller 只是彼此重复,但并非所有设置都会 Controller 之间的值相同)。
我遇到的问题是,当用户编辑单元格时,我尝试获取新值
value = self.Settings1Table.item(n, s).text()
但这仅检索我在填充期间放入单元格中的值,而不是刚刚通过键盘输入的值。我已经阅读过有关方法 currentText()
的内容,但据我了解,这要求每个单元格都是自己的小部件,而且我不确定这是如何完成的。
整个代码相当大,我觉得没有必要完整地发布它,但如果需要更多代码,我很乐意提供。感谢您的帮助,我希望能够到来。
编辑:这是迭代表中每个项目的方法,它应该获取当前值,但现在只返回我通过 item.setText(str) 设置的值(需要是该值用户通过键盘输入)
def ApplyAll1(self):
if not self.CheckHealth():
for s in xrange(NumOfSegs):
n = 0
for item in Settings1List:
value = self.Settings1Table.item(n, s).text()
print value
else:
self.MsgCntrField.setText("CONNECTION ERROR")
self.CheckHealth() 只是错误检查
最佳答案
最后更新时间:2014 年 8 月 19 日 0 : 29
I don't really care about the event, since I am going to loop through the entire table, I do need the data changed by the keyboard instead of 'QtGui.QTableWidgetItem.setText' so yes
好的,可以先创建事件,然后仅创建键盘,但是您必须实现 QtGui.QTableWidget.focusInEvent(self, eventQFocusEvent)
和 QtGui.QTableWidget.focusOutEvent(self, eventQFocusEvent)
。因此,请参阅示例代码,希望有所帮助;
import sys
from PyQt4 import QtGui, QtCore
class QCustomTableWidget (QtGui.QTableWidget):
def __init__ (self, parent = None):
super(QCustomTableWidget, self).__init__(parent)
self.focusKeyboardOn = False
# Setup row & column data
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5']
self.setRowCount(len(listsVerticalHeaderItem))
for index in range(self.rowCount()):
self.setVerticalHeaderItem(index, QtGui.QTableWidgetItem(listsVerticalHeaderItem[index]))
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4']
self.setColumnCount(5)
listsHorizontalHeaderItem = ['Option 1', 'Option 2']
self.setColumnCount(len(listsHorizontalHeaderItem))
for index in range(self.columnCount()):
self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))
def dataChanged (self, topLeftQModelIndex, bottomRightQModelIndex):
row = topLeftQModelIndex.row()
column = topLeftQModelIndex.column()
dataQTableWidgetItem = self.item(row, column)
if (self.currentItem() == dataQTableWidgetItem) and (self.focusKeyboardOn == True):
self.emit(QtCore.SIGNAL('currentKeyboardDataChanged'), row, column, dataQTableWidgetItem)
self.emit(QtCore.SIGNAL('dataChanged'), row, column, dataQTableWidgetItem)
QtGui.QTableWidget.dataChanged(self, topLeftQModelIndex, bottomRightQModelIndex)
def focusInEvent (self, eventQFocusEvent):
self.focusKeyboardOn = False
QtGui.QTableWidget.focusInEvent(self, eventQFocusEvent)
def focusOutEvent (self, eventQFocusEvent):
self.focusKeyboardOn = True
QtGui.QTableWidget.focusOutEvent(self, eventQFocusEvent)
class QCustomWidget (QtGui.QWidget):
def __init__(self, parent = None):
super(QCustomWidget, self).__init__(parent)
self.myQCustomTableWidget = QCustomTableWidget(self)
self.myQLabel = QtGui.QLabel('Track edited data', self)
myQVBoxLayout = QtGui.QVBoxLayout()
myQVBoxLayout.addWidget(self.myQLabel)
myQVBoxLayout.addWidget(self.myQCustomTableWidget)
self.setLayout(myQVBoxLayout)
self.connect(self.myQCustomTableWidget, QtCore.SIGNAL('currentKeyboardDataChanged'), self.setTrackData)
self.myQCustomTableWidget.setItem(0, 0, QtGui.QTableWidgetItem('Test'))
self.myQCustomTableWidget.setItem(1, 1, QtGui.QTableWidgetItem('Work'))
def setTrackData (self, row, column, dataQTableWidgetItem):
self.myQLabel.setText('Last updated\nRow : %d, Column : %d, Data : %s' % (row + 1, column + 1, str(dataQTableWidgetItem.text())))
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())
注意:有 1 个情况会出现 BUG:如果您在通过键盘进行编辑时通过“QtGui.QTableWidgetItem.setText”进行设置。但如果你的情况严格,我建议创建你自己的小部件并设置你自己的项目委托(delegate)。 (但是,非常硬核...)
我不知道你的单元格中的数据是什么。 (这是另一个自定义QWidget
,或者只是普通数据QTableWidgetItem
)
无论如何,当用户编辑单元格时,您尝试使用此方法 QAbstractItemView.dataChanged (self, QModelIndex topLeft, QModelIndex BottomRight)
获取新值。此方法返回已编辑的数据位置,并使用QTableWidgetItem QTableWidget.item (self, int row, int column)
从索引获取数据。 (这就是你所说的问题)但是这项工作仅编辑已关闭(不在编辑期间)。
示例;
import sys
from PyQt4 import QtGui, QtCore
class QCustomTableWidget (QtGui.QTableWidget):
def __init__ (self, parent = None):
super(QCustomTableWidget, self).__init__(parent)
# Setup row & column data
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5']
self.setRowCount(len(listsVerticalHeaderItem))
for index in range(self.rowCount()):
self.setVerticalHeaderItem(index, QtGui.QTableWidgetItem(listsVerticalHeaderItem[index]))
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4']
self.setColumnCount(5)
listsHorizontalHeaderItem = ['Option 1', 'Option 2']
self.setColumnCount(len(listsHorizontalHeaderItem))
for index in range(self.columnCount()):
self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))
def dataChanged (self, topLeftQModelIndex, bottomRightQModelIndex):
row = topLeftQModelIndex.row()
column = topLeftQModelIndex.column()
dataQTableWidgetItem = self.item(row, column)
print '###### Data Changed ######'
print 'row :', row + 1
print 'column :', column + 1
print 'data :', dataQTableWidgetItem.text()
self.emit(QtCore.SIGNAL('dataChanged'), row, column, dataQTableWidgetItem)
QtGui.QTableWidget.dataChanged(self, topLeftQModelIndex, bottomRightQModelIndex)
class QCustomWidget (QtGui.QWidget):
def __init__(self, parent = None):
super(QCustomWidget, self).__init__(parent)
self.myQCustomTableWidget = QCustomTableWidget(self)
self.myQLabel = QtGui.QLabel('Track edited data', self)
myQVBoxLayout = QtGui.QVBoxLayout()
myQVBoxLayout.addWidget(self.myQLabel)
myQVBoxLayout.addWidget(self.myQCustomTableWidget)
self.setLayout(myQVBoxLayout)
self.connect(self.myQCustomTableWidget, QtCore.SIGNAL('dataChanged'), self.setTrackData)
def setTrackData (self, row, column, dataQTableWidgetItem):
self.myQLabel.setText('Last updated\nRow : %d, Column : %d, Data : %s' % (row + 1, column + 1, str(dataQTableWidgetItem.text())))
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())
QAbstractItemView.dataChanged (self, QModelIndex topLeft, QModelIndex BottomRight)
引用:http://pyqt.sourceforge.net/Docs/PyQt4/qabstractitemview.html#dataChanged
问候,
关于python - PyQt QTableWidget 检索编辑字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25351754/