简体   繁体   中英

PyQt update a certain cell in a QTableWidget

I have a table that looks like this:

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

When I select an account I want to update on that account the "Info" cell with a text, for example Selected This how I build my table:

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

This is how I know which accounts are checked (it a method that is monitoring the clicks and it's connected to the table widget):

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

but How I can update for checked account the "Info" cell with particular message? I tried : 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!")

Here's a useful code for this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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