[英]PyQt4: setVerticalHeaderLabels to checkbox
你怎么能setVerticalHeaderLabels
一個的QTableWidget
一個復選框,如果可能的使用Python?
這將是代碼的相關部分...
#!/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()
此代碼將引發TypeError: QTableWidget.setVerticalHeaderLabels(QStringList): argument 1 has unexpected type 'list'
因為QTableWidget.setVerticalHeaderLabels()
需要一個字符串列表。
可能應該使用setVerticalHeaderItem
並創建看起來像復選框的自定義QTableWidgetItem
子類。
QTableWidgetItem
具有checkState
和setCheckState
方法,因此應該可以實現您所需要的東西。
las,我從來沒有這種需求,所以我無法為您提供更多見解。
編輯:只是為了澄清: setVerticalHeaderLabel(s)
對於您要執行的操作是完全錯誤的。 這只是使用給定文本創建QTableWidgetItem
並將其添加為標題的一種便捷方法。 如果要自定義,則必須使用setVerticalHeaderItem
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.