简体   繁体   中英

PyQt4: setVerticalHeaderLabels to checkbox

How can you setVerticalHeaderLabels of a QTableWidget to a checkbox if possible using python?

This would be the relevant part of the code...

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.table = QTableWidget()
        self.setCentralWidget(self.table)

        self.updateTable()

    def updateTable(self):
        table_rows = range(0,1)
        table_cols = range(0,1)

        self.table.setRowCount(len(table_rows))
        self.table.setColumnCount(len(table_cols))

        vertical_label = [self.tableCheckbox() for row in table_rows]
        self.table.setVerticalHeaderLabels(vertical_label)

    def tableCheckbox(self):
        chkBoxItem = QTableWidgetItem()
        chkBoxItem.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
        chkBoxItem.setCheckState(Qt.Unchecked)
        return chkBoxItem

def main():
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()

This code will raise TypeError: QTableWidget.setVerticalHeaderLabels(QStringList): argument 1 has unexpected type 'list' because QTableWidget.setVerticalHeaderLabels() is expecting a list of strings.

Probably you should use setVerticalHeaderItem and create your custom QTableWidgetItem subclass that looks like a checkbox.

QTableWidgetItem has the checkState and setCheckState methods, so something like what you're requiring should be possible.

Alas I've never had this need so I can't provide you with more insights.

edit: Just to clarify: setVerticalHeaderLabel(s) is completely wrong for what you're trying to do. It's just a convenience method to create a QTableWidgetItem with the given text and add it as an header. If you want customization you must use setVerticalHeaderItem .

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