繁体   English   中英

PyQt 更新 QTableWidget 中的某个单元格

[英]PyQt update a certain cell in a QTableWidget

我有一张看起来像这样的表:

================================
|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 |
===================================

通过添加这个:

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

这是一个有用的代码:

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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM