【问题标题】:PyQt update a certain cell in a QTableWidgetPyQt 更新 QTableWidget 中的某个单元格
【发布时间】:2020-05-17 15:43:03
【问题描述】:

我有一张如下所示的表格:

================================
|Checkbox| Account_name | Info |
================================

当我选择一个帐户时,我想在该帐户上更新带有文本的“信息”单元格,例如 Selected 这就是我构建表格的方式:

        self.mainAccountTable.setCellWidget(currentRowCount, 0, pWidget)
        self.mainAccountTable.setItem(currentRowCount, 1, QTableWidgetItem(name[1]))
        self.mainAccountTable.setItem(currentRowCount, 2, QTableWidgetItem(info[2]))

这就是我知道哪些帐户被检查的方式(它是一种监控点击的方法,它连接到表格小部件):

for account in range(self.mainAccountTable.rowCount()):
   if self.mainAccountTable.cellWidget(account, 0).findChild(type(QCheckBox())).isChecked():

但是我如何为已检查的帐户更新带有特定消息的“信息”单元格?我试过了 : self.mainAccountTable.setItem(self.mainAccountTable.item(account, 2).text("Selected"))

===================================
|Checked| Account_name | Selected |
===================================

【问题讨论】:

    标签: python checkbox pyqt cell


    【解决方案1】:

    通过添加:

    self.YourTableName.item(row, column).setText("Put here whatever you want!")
    

    【讨论】:

    • 感谢分享!
    【解决方案2】:

    这是一个有用的代码:

    from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget, QTableWidget, QTableWidgetItem
    from PyQt5.QtCore import QSize, Qt
    
    
    class MainWindow(QMainWindow):
        # Override class constructor
        def __init__(self):
            # You must call the super class method
            QMainWindow.__init__(self)
    
            self.setMinimumSize(QSize(480, 80))         # Set sizes 
            self.setWindowTitle("Работа с QTableWidget")    # Set the window title
            central_widget = QWidget(self)              # Create a central widget
            self.setCentralWidget(central_widget)       # Install the central widget
    
            grid_layout = QGridLayout(self)         # Create QGridLayout
            central_widget.setLayout(grid_layout)   # Set this layout in central widget
    
            table = QTableWidget(self)  # Create a table
            table.setColumnCount(3)     #Set three columns
            table.setRowCount(1)        # and one row
    
            # Set the table headers
            table.setHorizontalHeaderLabels(["Header 1", "Header 2", "Header 3"])
    
            #Set the tooltips to headings
            table.horizontalHeaderItem(0).setToolTip("Column 1 ")
            table.horizontalHeaderItem(1).setToolTip("Column 2 ")
            table.horizontalHeaderItem(2).setToolTip("Column 3 ")
    
            # Set the alignment to the headers
            table.horizontalHeaderItem(0).setTextAlignment(Qt.AlignLeft)
            table.horizontalHeaderItem(1).setTextAlignment(Qt.AlignHCenter)
            table.horizontalHeaderItem(2).setTextAlignment(Qt.AlignRight)
    
            # Fill the first line
            table.setItem(0, 0, QTableWidgetItem("Text in column 1"))
            table.setItem(0, 1, QTableWidgetItem("Text in column 2"))
            table.setItem(0, 2, QTableWidgetItem("Text in column 3"))
    
            # Do the resize of the columns by content
            table.resizeColumnsToContents()
    
            grid_layout.addWidget(table, 0, 0)   # Adding the table to the grid
    
    
    if __name__ == "__main__":
        import sys
    
        app = QApplication(sys.argv)
        mw = MainWindow()
        mw.show()
        sys.exit(app.exec())
    

    【讨论】:

    • "NameError: name 'QTableWidgetItem' is not defined" :(
    • QtWidgets.QTableWidgetItem (!!)
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2011-01-04
    • 2014-07-10
    • 2012-04-05
    相关资源
    最近更新 更多