QTableWidget无法获取用户输入的数据

时间:2016-01-14 06:19:10

标签: python pyqt4

我使用QtDesigner创建一个带有QTableWidget的简单表单。我运行程序,然后用数字填充几个单元格。当我尝试使用.itemAt(1,1).itemAt(2,1)访问数据时,我只能获取单元格(1,1)中的值。这由getDataFromTable函数中的print语句显示。

我是Python新手。任何帮助是极大的赞赏。我的代码和QtDesigner的代码如下。

from Ui_SimpleTable import Ui_MainWindow

import sys
from PyQt4 import QtGui, QtCore

class Main(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, ):
        super (Main,self).__init__()
        self.setupUi(self)
        self.pbGetData.clicked.connect(self.getDataFromTable)


    def getDataFromTable (self, item):
        print ("Data from Table")
        test = self.dataTable.itemAt(1,1)
        print (test.text())
        test2 = self.dataTable.itemAt(2,1)
        print (test2.text())


if __name__ == '__main__':

    app=QtGui.QApplication(sys.argv)
    main=Main()
    main.show()
    QtGui.QTableWidget.itemAt
    sys.exit(app.exec_())
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(701, 632)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.dataTable = QtGui.QTableWidget(self.centralwidget)
        self.dataTable.setGeometry(QtCore.QRect(50, 30, 371, 511))
        self.dataTable.setRowCount(30)
        self.dataTable.setColumnCount(3)
        self.dataTable.setObjectName(_fromUtf8("dataTable"))
        self.pbGetData = QtGui.QPushButton(self.centralwidget)
        self.pbGetData.setGeometry(QtCore.QRect(480, 30, 75, 23))
        self.pbGetData.setObjectName(_fromUtf8("pbGetData"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 701, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pbGetData.setText(_translate("MainWindow", "Get Data", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

您需要使用item(row, col)方法而不是itemAt(x, y)方法。

def getDataFromTable (self, item):
        print ("Data from Table")
        test = self.dataTable.item(1,1)
        print (test.text())
        test2 = self.dataTable.item(2,1)
        print (test2.text())

这将为您提供位于从零开始的行/列位置的QTableWidgetItem。此QTableWidgetItem包含用户输入的值,您可以使用此示例中的.text()方法获取该值。

假设我们启动应用程序并从顶部开始按从左到右的顺序输入字母:

# awesome QTableWidget representation with rows and columns
   0   1   2
  +---------+
0 |a | b | c|
  +--+---+--+
1 |d | e | f|
  +--+---+--+
2 |g | h | i|
  +---------+

您在行/列位置e获得(1, 1),在行/列位置h获得(2, 1)

➜  /tmp  python3 -B app.py
Data from Table
e
h