Python 3 + PyQt 5:从QTableWidget检索QDoubleSpinBox的值

时间:2018-07-09 11:20:19

标签: python python-3.x pyqt5 qtablewidget

由于某种原因,我不得不坚持在表中使用QDoubleSpinBox。我正在尝试使用valueChanged来获取最新的更改值,以调用名为onChange()的处理程序。但是结果似乎总是为“ 0.0”,甚至不在允许值范围内。 注意:   我的禁忌是用一个 在任何单元格的onChange处将获取一张具有不同可编辑单元格类型的表(预期使用单个onChange处理程序) 下面给出了可行的简单代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(0, 10, 401, 151))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setRowCount(1)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(0, item)
        self.tableWidget.setHorizontalHeaderItem(0, item)
        self.tableWidget.setVerticalHeaderItem(1, item)
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 0, item)
        item = QtWidgets.QDoubleSpinBox()
        item.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
        item.setRange(1, 1000)
        item.setDecimals(2)
        item.valueChanged.connect(self.onChange)
        self.tableWidget.setCellWidget(0, 0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 1, item)
        MainWindow.setCentralWidget(self.centralwidget)
        self.tableWidget.itemChanged.connect(self.onChange)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.verticalHeaderItem(0)
        item.setText(_translate("MainWindow", "1"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "x"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "y"))
        __sortingEnabled = self.tableWidget.isSortingEnabled()
        self.tableWidget.setSortingEnabled(False)
        self.tableWidget.setSortingEnabled(__sortingEnabled)

    def onChange(self):
        rows = self.tableWidget.rowCount()
        columns = self.tableWidget.columnCount()
        for row in range(rows):
            for column in range(columns):
                item = self.tableWidget.item(row, column)
                if item != QtWidgets.QTableWidgetItem:
                    item1 = QtWidgets.QDoubleSpinBox(self.tableWidget.cellWidget(row, column))
                    print("value1:", item1.value())
                else:
                    print("value:", item.text())
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

谢谢!

0 个答案:

没有答案