如何验证 qtable 小部件不为空

Posted

技术标签:

【中文标题】如何验证 qtable 小部件不为空【英文标题】:How to validate the qtable widget is not empty 【发布时间】:2020-07-31 01:14:46 【问题描述】:

假设我有一个 QTableWidgetItem 和 Pushbutton 来获取 Matrix 值。我想确保在单击提交按钮时填充整个矩阵单元格。这样我就可以进一步计算这些值。它应该向用户发送一条消息,表明他填写了该项目。也不运行任何东西。

我还搜索了 cellEntered() cellChanged() 方法。我不知道如何确保用户在提交之前在所有单元格中输入数据。

如何为该 QTableWidgetItem 项使用验证器?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidget, QApplication, QMainWindow, QTableWidgetItem, QFileDialog,qApp, QAction,QStyledItemDelegate,QLineEdit
from pandas import DataFrame
from numpy.linalg import inv
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtCore import pyqtSlot
import sqlite3 
import os
import re
import numpy as np 
import pandas as pd
class NumericDelegate(QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        editor = super(NumericDelegate, self).createEditor(parent, option, index)
        if isinstance(editor, QLineEdit):
            reg_ex = QRegExp("[0-9]+.?[0-9],2")
            validator = QRegExpValidator(reg_ex, editor)
            editor.setValidator(validator)
        return editor
class Ui_MainWindow(object):
   
    def submit(self):
     
        rowCount = self.tableWidgetInput.rowCount()
        columnCount = self.tableWidgetInput.columnCount()
        max_sum = 0
        global TMatrix,Ri,Ci,RiplusCi,RiMinusCi,inputArray_2D
        if (rowCount==columnCount):
            size=rowCount
            print("The size of the matrxi is %d * %d "%(size,size))
            print("The Given  matrxi is",  "SUM of Row" )
            rowData =[]
            
            for row in range(size):
                for column in range (size):
                        widegetItem = self.tableWidgetInput.item(row,column)
                        
                        if widegetItem and widegetItem.text():
                            rowData.append(float(widegetItem.text()) )
                        else:
                            rowData.append('NULL')
            print(rowData)
            inputArray = np.array(rowData,dtype=np.float64)  ###convert the list into numpy array.
            print(inputArray)
            size_rowdata = len(rowData)
            print("The total number of elemets are ",size_rowdata)
            inputArray_2D = np.reshape(inputArray, (rowCount, columnCount))   ### Reshape the numpy array into 2D
            print(inputArray_2D)
            sumofCol = np.sum(inputArray_2D,axis = 0,dtype='float')  ###find the sum of Column
            sumofRow = np.sum(inputArray_2D,axis = 1,dtype='float') ### find the sum of Row     
            maxInCol = np.amax(sumofCol)
            maxInRows = np.amax(sumofRow)
            print( "The Sum of Column is : ",sumofCol)
            print( "The Sum of Row is :",sumofRow)
            print( "The Maximum value in the  Column is :",maxInCol)
            print( "The Maximum value in the  Row is  : ",maxInRows)  
                   
        else:
            print("The input  is not a Square matrix")
            print("Data is not Submitted Sucessfully")

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1500, 1200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.tableWidgetInput = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidgetInput.setGeometry(QtCore.QRect(30, 90, 1049, 520))
        self.tableWidgetInput.setObjectName("tableWidgetInput")
        self.tableWidgetInput.setColumnCount(2)
        self.tableWidgetInput.setRowCount(2)
        delegate = NumericDelegate(self.tableWidgetInput)
        self.tableWidgetInput.setItemDelegate(delegate)
        self.pushButton_submit = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_submit.setGeometry(QtCore.QRect(350, 625, 131, 51))
        self.pushButton_submit.setObjectName("pushButton_submit")                                        
        self.pushButton_submit.clicked.connect(self.submit)                                                    
        MainWindow.setCentralWidget(self.centralwidget) 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
       
        self.pushButton_submit.setText(_translate("MainWindow", "Submit"))
      
        
       
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_())

谢谢!

【问题讨论】:

我不明白你的问题,你能指出如果按下按钮并获得一个带有空文本的项目会发生什么? @eyllanesc 我想确保在单击提交按钮时用值填充整个矩阵单元格。这样我就可以进一步计算值并获得最终输出 嗯,我没问过你。假设在单元格 0x0 中它是一个空项目并且用户按下按钮,在这种情况下会发生什么?是否应该向用户显示一条消息,表明他填写了该项目?不运行任何东西?等 是的。应该向用户发送一条消息,表明他填写了该项目。也不运行任何东西 你应该在你的帖子中指出这一点 【参考方案1】:

好吧,我实际上并不知道这一点,但在您的问题关闭之前作为猜测,或者只是检查它是否不等于空的 qTable。所以做一个空着的,或者当场做一个。

【讨论】:

以上是关于如何验证 qtable 小部件不为空的主要内容,如果未能解决你的问题,请参考以下文章

Python - PyQt - QTable 小部件 - 添加行

WordPress的基本空小部件插件

如何从 Qtable 中获取值

使用 QtDesigner PyQt4 创建自定义小部件

Flutter:如何在构造函数中传递值,以便我可以重用我的小部件?

从 API 返回的 Text 小部件内的 Null 值