PyQt QTableWidget检索已编辑的字段

时间:2014-08-17 17:28:36

标签: python python-2.7 pyqt4 qtablewidget qtablewidgetitem

我是PyQt的新手,我在PyQt4和Python 2.7中制作了一个GUI(其中包括)控制少数微控制器的设置。为了方便地填充和显示设置列表,以及它们来自哪个控制器,我将它放入QTableWidget中,其中列是控制器,行是设置(所有控制器只是彼此重复,但并非所有设置都是从控制器到控制器的值相同。

我遇到的问题是,当用户编辑单元格时,我尝试使用

获取新值
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()只是错误检查

1 个答案:

答案 0 :(得分:2)

上次更新8/19/2014 0:29

  

我不太关心这个事件,因为我要循环   整个表格,我确实需要通过键盘改变数据   ' QtGui.QTableWidgetItem.setText'所以是的

好的,可能只创建事件然后键盘,但您必须实施QtGui.QTableWidget.focusInEvent(self, eventQFocusEvent)QtGui.QTableWidget.focusOutEvent(self, eventQFocusEvent)。所以,请看ny示例代码,希望是帮助;

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'通过键盘激活编辑。但是,如果你严格的话,我建议创建你自己的小部件并设置你自己的委托。 (但是,非常铁杆......)


我不知道你的手机中的数据是什么。 (它是另一个自定义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


此致