我已经实现了一个表模型(继承的QAbstractTableModel
(并将其添加到QTableView
。
我想添加委托给我的数据项使用自定义编辑小部件(例如,使用 QSlider
进行某些双重值(。las,在添加了委托后,表视图不再显示此列中的值。
谁能告诉我,如何解决?
这是代码:
# -*- coding: utf-8 -*-
import os, sys
from enum import IntEnum, unique
from collections import OrderedDict
from PyQt5 import QtCore, QtGui, QtWidgets
@unique
class BGModelCols(IntEnum):
alpha = 0
alpha_for_nans = 1
color_map = 2
is_boolean = 3
x_size_px = 4
y_size_px = 5
is_visible = 6
class ScanRadarSimulator(QtCore.QObject):
backgrounds_supported = ["height", "elevation", "visibility", "closing_angles", "land_rcs"]
@property
def background_names(self):
return [self.tr("Land height"), self.tr("Elevation"), self.tr("Visibility"), self.tr("Closing angles"),
self.tr("Land RCS")]
def __init__(self, parent=None):
super().__init__(parent)
class BackgroundTableModel(QtCore.QAbstractTableModel):
def __init__(self, radar_simulator, parent=None):
super().__init__(parent)
self._radar_simulator = radar_simulator
self._background_names = self._radar_simulator.background_names
assert isinstance(self._radar_simulator, ScanRadarSimulator)
self.column_names = {BGModelCols.alpha: self.tr("α-channel"),
BGModelCols.alpha_for_nans: self.tr("α-channel for NANs"),
BGModelCols.color_map: self.tr("Color map"),
BGModelCols.is_boolean: self.tr("Is boolean mask"),
BGModelCols.x_size_px: self.tr("X pixels"),
BGModelCols.y_size_px: self.tr("Y pixels"),
BGModelCols.is_visible: self.tr("Is visible")}
self._background_items = OrderedDict()
for bg_id in radar_simulator.backgrounds_supported:
bg_dict = {BGModelCols.alpha: 0.7,
BGModelCols.alpha_for_nans: 0.0,
BGModelCols.color_map: "jet",
BGModelCols.is_boolean: False,
BGModelCols.x_size_px: 4000,
BGModelCols.y_size_px: 4000,
BGModelCols.is_visible: False}
self._background_items[bg_id] = bg_dict
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self._radar_simulator.backgrounds_supported)
def columnCount(self, parent=QtCore.QModelIndex()):
return len(BGModelCols)
def flags(self, index=QtCore.QModelIndex()):
if index.isValid():
return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable
def data(self, index=QtCore.QModelIndex(), role=QtCore.Qt.DisplayRole):
if not index.isValid():
return QtCore.QVariant()
row, col = index.row(), index.column()
print("DATA", row, col)
col_item = BGModelCols(col)
if role == QtCore.Qt.DisplayRole:
return str(self._background_items[radar_simulator.backgrounds_supported[row]][col_item])
elif role == QtCore.Qt.EditRole:
return self._background_items[radar_simulator.backgrounds_supported[row]][col_item]
else:
return QtCore.QVariant()
def setData(self, index, value, role=QtCore.Qt.EditRole):
if not index.isValid():
return False
if role == QtCore.Qt.EditRole:
row, col = index.row(), index.column()
col_item = BGModelCols(col)
self._background_items[radar_simulator.backgrounds_supported[row]][col_item] = value
self.dataChanged.emit(index, index)
return True
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self.column_names[BGModelCols(section)]
elif orientation == QtCore.Qt.Vertical:
return self._background_names[section]
else:
return QtCore.QVariant()
class AlphaChannelDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent=None):
super().__init__(parent)
def createEditor(self, parent, option, index):
print("CREATING EDITOR")
slider = QtWidgets.QSlider(parent)
slider.setMinimum(0)
slider.setMaximum(100)
slider.setOrientation(QtCore.Qt.Horizontal)
return slider
def setModelData(self, editor, model, index):
# row, col = index.row(), index.column()
# col_item = BGModelCols(col)
# model._background_items[model._radar_simulator.backgrounds_supported[row]][col_item] = editor.value() / 100.
print("setModelData")
model.setData(index, editor.value() / 100., QtCore.Qt.EditRole)
def setEditorData(self, editor, index):
print("setEditorData")
row, col = index.row(), index.column()
col_item = BGModelCols(col)
val = int(index.model()._background_items[index.model()._radar_simulator.backgrounds_supported[row]][col_item] * 100)
editor.setValue(val)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def paint(self, painter, option, index):
QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_ItemViewItem, option, painter)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
radar_simulator = ScanRadarSimulator()
table_view = QtWidgets.QTableView()
alpha_delegate = AlphaChannelDelegate(table_view)
table_view.setItemDelegateForColumn(int(BGModelCols.alpha), alpha_delegate)
table_view.setEditTriggers(QtWidgets.QAbstractItemView.AllEditTriggers)
table_view.setModel(BackgroundTableModel(radar_simulator))
table_view.show()
sys.exit(app.exec_())
好吧,如果评论paint
方法,它将起作用。^_^