gpt4 book ai didi

python - PyQt QTableWidget 检索编辑字段

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:08 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com