确定表格小部件中的单元格是否为空 [英] Determine if a cell is empty in table widget

查看:66
本文介绍了确定表格小部件中的单元格是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 tablewidget 时遇到问题.

问题

我在表格小部件(5 行和 1 列)中获取用户的输入.用户不必填写所有行.例如,假设他只填写了其中的 3 个.

然后我尝试使用以下方法在 python 中读取这些行:

tableWidget.item(1,1).text()

但如果我将某些行留空,则运行代码总会出现问题.

那么谁能告诉我如何检查单元格是否为空并且我可以跳过阅读它?

目前我使用以下两种可能性来检查那件事:

<块引用>

1) 基本逻辑

if len(tableWidget.item(4,1).text())!=0:print('单元格非空')

<块引用>

2) 来自 Qt 文档

if tableWidget.item(4,1).text().isEmpty():print('单元格为空')

但它们都不起作用!非常感谢!

代码(click_me)

from PyQt5 import QtCore, QtWidgets类 Ui_MainWindow(对象):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(800, 600)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)self.tableWidget.setGeometry(QtCore.QRect(120, 70, 256, 192))self.tableWidget.setRowCount(5)self.tableWidget.setObjectName("tableWidget")self.tableWidget.setColumnCount(1)item = QtWidgets.QTableWidgetItem()item.setText("选定文件")self.tableWidget.setHorizo​​ntalHeaderItem(0, item)self.pushButton_ok = QtWidgets.QPushButton("OK",self.centralwidget)self.pushButton_ok.setGeometry(QtCore.QRect(100, 290, 337, 23))self.pushButton_ok.setObjectName("pushButton_ok")MainWindow.setCentralWidget(self.centralwidget)QtCore.QMetaObject.connectSlotsByName(MainWindow)self.pushButton_ok.clicked.connect(lambda:self.click_me())def click_me(self):行 = self.tableWidget.rowCount()列表 = []对于范围(0,行)中的我:jar = self.tableWidget.item(i,0).text()如果 len(jar)!=0:listi.append(jar)如果 __name__ == "__main__":导入系统app = QtCore.QCoreApplication.instance()如果应用程序是无:app = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow()ui = Ui_MainWindow()ui.setupUi(主窗口)MainWindow.show()app.exec_()

解决方案

首先你已经创建了一个 QTableWidget 然后你已经建立了行数和列数并不意味着对于每个框中有一个 QTableWidgetItem,只有当您显式创建它们或通过用户所做的编辑创建它们时才会创建它们,这是由性能问题完成的.

因此空框可能表示此框没有关联的QTableWidgetItemQTableWidgetItem 文本为空.因此,您必须同时检查两者,因此正确的做法是先验证该项目是否存在,然后再验证该项目是否有文本:

def click_me(self):行 = self.tableWidget.rowCount()列表 = []对于范围(行)中的 i:it = self.tableWidget.item(i, 0)如果它:如果它.text():listi.append(it.text())打印(列表)

或者利用Python中从左到右验证布尔语句的事实,可以得到更紧凑的形式:

def click_me(self):行 = self.tableWidget.rowCount()列表 = []对于范围(行)中的 i:it = self.tableWidget.item(i, 0)如果它和 it.text():listi.append(it.text())

I am having issues with tablewidget.

Issue

I am getting the input from the user in a table widget (5 rows and 1 column). It is not necessary for the user to fill all the rows. Let’s say for instance, he fills only 3 of them.

Then I am trying to read those rows in python using:

tableWidget.item(1,1).text()

But if I leave some rows empty, there is always some problem running the code.

So can anyone tell me how to check if a cell is empty and I can skip reading it?

At the moment I am using two following possibilities to check that thing:

1) Basic Logic

if len(tableWidget.item(4,1).text())!=0:
    print('Cell is non-empty')

2) From Qt documentation

if tableWidget.item(4,1).text().isEmpty():
    print('Cell is empty')

But none of them work! Thanks a lot!

Code (click_me)

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(120, 70, 256, 192))
        self.tableWidget.setRowCount(5)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(1)
        item = QtWidgets.QTableWidgetItem()
        item.setText("Selected File")
        self.tableWidget.setHorizontalHeaderItem(0, item)
        self.pushButton_ok = QtWidgets.QPushButton("OK",self.centralwidget)
        self.pushButton_ok.setGeometry(QtCore.QRect(100, 290, 337, 23))
        self.pushButton_ok.setObjectName("pushButton_ok")
        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.pushButton_ok.clicked.connect(lambda:self.click_me())

    def click_me(self):
        rows  =  self.tableWidget.rowCount()
        listi = []
        for i in range(0,rows):
            jar = self.tableWidget.item(i,0).text()
            if len(jar)!=0:
                listi.append(jar)

if __name__ == "__main__":
    import sys
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()

解决方案

First of all that you have created a QTableWidget and then you have established the number of rows and columns does not imply that for each box there is a QTableWidgetItem, these will only be created if you create them explicitly or through the editing that the user does, this is done by performance issues.

So an empty box may indicate that this box does not have an associated QTableWidgetItem or that the QTableWidgetItem text is empty. So you have to check both, so the correct thing would be to verify first the existence of the item, and then if the item has text:

def click_me(self):
    rows  =  self.tableWidget.rowCount()
    listi = []
    for i in range(rows):
        it = self.tableWidget.item(i, 0)
        if it:
            if it.text():
                listi.append(it.text())
    print(listi)

Or taking advantage of the fact that in Python the Boolean statements are verified from left to right, a more compact form can be obtained:

def click_me(self):
    rows  =  self.tableWidget.rowCount()
    listi = []
    for i in range(rows):
        it = self.tableWidget.item(i, 0)
        if it and it.text():
            listi.append(it.text())

这篇关于确定表格小部件中的单元格是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆