Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top