return False def setModelData(self, editor, model, index): ''' The user wanted to change the old state in the opposite. ''' model.setData(index, 1 if int(index.data()) == 0 else 0, QtCore.Qt.EditRole) if __name__ == '__main__': import sys app = QApplication(sys.argv) model = QStandardItemModel(4, 3) tableView = QTableView() tableView.setModel(model) delegate = CheckBoxDelegate(None) tableView.setItemDelegateForColumn(1, delegate) for row in range(4): for column in range(3): index = model.index(row, column, QModelIndex()) model.setData(index, 1) tableView.setWindowTitle("Check Box Delegate") tableView.show() sys.exit(app.exec_())
def __init__(self, parent, title, data, id=0, hidden_columns=[], index_column=None, childSubWindow={}, type=1, file_path=None): """ :param title: Window title :param data: Table show data :param hidden_columns: Columns to be hidden :param index_column: which column is index column :param childSubWindow: When double click each row, which child table should be opened Sample: { "title":"", "type": "table", "table_name": "option/underlyings/%underlying_order_book_id%", "where:"", "select":"", "hidden_columns":[], "index_column":[], "childSubWindow":{}, } :return: """ self.parent = parent self.window = parent.window self.mdi_area = parent.mdi_area if index_column: data.index = list(data[index_column]) data.index.name = index_column subWindow = QMdiSubWindow() setattr(subWindow, "subWindowType", 0) setattr(subWindow, "btData", data) setattr(subWindow, "btId", id) setattr(subWindow, "btType", type) setattr(subWindow, "btFilePath", file_path) setattr(subWindow, "childSubWindow", childSubWindow) setattr(subWindow, "hidden_columns", hidden_columns) tableView = QTableView() # 双击列的信号 tableView.horizontalHeader().sectionDoubleClicked.connect( lambda event: self.onTableViewColumnDoubleClicked(event, None)) # 双击行的信号 tableView.verticalHeader().sectionDoubleClicked.connect( self.onTableViewRowDoubleClicked) # 双击cell的信号 tableView.doubleClicked.connect(self.onTableViewCellDoubleClicked) # 右键列 headers = tableView.horizontalHeader() headers.setContextMenuPolicy(Qt.CustomContextMenu) headers.customContextMenuRequested.connect( lambda event: self.onTableViewColumnClicked(event, tableView)) headers.setSelectionMode(QAbstractItemView.SingleSelection) cornerButton = tableView.findChild(QAbstractButton) cornerButton.customContextMenuRequested.connect( self.onCornerButtonRightClicked) tableView.setWindowTitle(title) tableView.setWindowIcon(QtGui.QIcon("../icon/sheet.png")) proxyModel = QtCore.QSortFilterProxyModel(subWindow) mode = pandas_mode.PandasModel(data) proxyModel.setSourceModel(mode) tableView.setModel(proxyModel) self._hide_columns(tableView, data, hidden_columns) systemMenu = subWindow.systemMenu() last_action = systemMenu.actions()[-1] display_setting = self.window.findChild(QAction, "display_action") systemMenu.insertAction(last_action, display_setting) subWindow.setAttribute(Qt.WA_DeleteOnClose) subWindow.setWidget(tableView) self.mdi_area.addSubWindow(subWindow) subWindow.show()