Hi, I'm trying to have a checkable column plus label with other columns in a table. I can't use QTableWidget as one of the other columns is a ComboBox. After much trying, googling and reading of Mark's excellent new book I just can't get it to work. I'm clearly doing something wrong
Main problem is that the label doesn't display unless it's being clicked. Also I can't check the checkbox unless I've previously double-clicked in the area where the label is. I'm using PyQt 4.3.1 and Python 2.5.1 on SUSE 10.2. Any help, especially any real code examples etc as I'm really stuck and confused, much appreciated. Here's my trimmed down code with just the single column for the checkbox to show my problem. It should run as is. Thanks, Chris #!/usr/bin/env python import sys, os print os.environ["PYTHONPATH"] print sys.version from PyQt4.QtCore import * from PyQt4.QtGui import * class window(QWidget): def __init__(self, parent = None, myList = None): QWidget.__init__(self, parent) self.colsTable = myTableView(parent = self, numRows = 2, dataObj = myList) mainLayout = QHBoxLayout() mainLayout.addWidget(self.colsTable) self.setLayout(mainLayout) index1 = self.colsTable.model.index(0, 0, QModelIndex()) self.colsTable.model.setData(index1,Qt.Checked ,Qt.CheckStateRole) class myTableView(QTableView): def __init__(self, parent = None, numRows = 0, dataObj = None): super(myTableView, self).__init__(parent) self.dataObj = dataObj # Setup the model self.model = myModel(dataObj = dataObj) self.setModel(self.model) # Add the single column self.model.insertColumn(0) # Add the rows for dummy in range(numRows): self.model.insertRow(0) self.itemDelegate = myDelegate(label = "Advanced", parent = parent) self.setItemDelegate(self.itemDelegate) class myDelegate(QItemDelegate): def __init__(self, label = None, parent = None): super(myDelegate, self).__init__(parent) self.label = label def createEditor(self, parent, option, index): editor = QCheckBox(self.label, parent) return editor def setEditorData(self, editor, index): value = index.model().data(index, Qt.CheckStateRole).toInt() if value[0] == 0: checkState = Qt.Unchecked else: checkState = Qt.Checked editor.setCheckState(checkState) # Set the label again, just in case. editor.setText(self.label) def setModelData(self, editor, model, index): value = editor.checkState() model.setData(index, value, Qt.CheckStateRole) def updateEditorGeometry(self, editor, option, index): editor.setGeometry(option.rect) class myModel(QStandardItemModel): def __init__(self, parent=None, dataObj = None): super(myModel, self).__init__(parent) self.dataObj = dataObj def data(self, index, role=Qt.DisplayRole): if role == Qt.CheckStateRole: if self.dataObj[index.row()] == 0: return QVariant(Qt.Unchecked) else: return QVariant(Qt.Checked) else: return QVariant() def setData(self, index, value, role): if role == Qt.CheckStateRole: if value == Qt.Checked: self.dataObj[index.row()] = 1 else: self.dataObj[index.row()] = 0 else: print "Set data with no CheckStateRole" self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index, index) return True def flags(self, index): if index.column() == 0: return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable else: return Qt.ItemIsEnabled myList = [] myList.append(0) myList.append(1) a = QApplication(sys.argv) w = window(myList = myList) w.show() sys.exit(a.exec_()) ____________________________________________________________________________________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo.com/ _______________________________________________ PyQt mailing list PyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt