Я создаю подкласс QStyledItemDelegate, чтобы отображать раскрывающийся список QCombobox в QTableView. Делегат определяет тип данных из модели таблицы, и если dataType == EnumMeta, отображается раскрывающийся список параметров.
В моей текущей реализации раскрывающийся список только отображается. после того, как пользователь дважды щелкнет ячейку таблицы. Затем пользователю необходимо снова щелкнуть раскрывающийся список, чтобы отобразить доступные параметры.
Вопрос:
Как всегда показывать QCombobox, и он будет открываться сразу же, когда пользователь щелкнет ячейку таблицы?
Пример кода
from enum import EnumMeta
from qgis.PyQt.QtWidgets import (
QStyledItemDelegate,
QWidget,
QComboBox,
)
from qgis.PyQt.QtGui import QPainter
from qgis.PyQt.QtCore import QModelIndex, QSize, QAbstractItemModel, Qt
from .shared_constants import CustomRoles
class BGCustomDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(BGCustomDelegate, self).__init__(parent)
self.setParent(parent)
def paint(self, painter: QPainter, option, index: QModelIndex) -> None:
QStyledItemDelegate.paint(self, painter, option, index)
def sizeHint(self, option, index: QModelIndex) -> QSize:
return QStyledItemDelegate.sizeHint(self, option, index)
def createEditor(self, parent: QWidget, option, index: QModelIndex) -> QWidget:
if index.data(CustomRoles.dataType.value) == EnumMeta:
editor = QComboBox(parent)
allowed_values_enum: EnumMeta = index.data(CustomRoles.allowedValues.value)
allowed_values = [e.value for e in allowed_values_enum]
editor.addItems(allowed_values)
return editor
else:
return QStyledItemDelegate.createEditor(self, parent, option, index)
def setEditorData(self, editor: QWidget, index: QModelIndex) -> None:
if index.data(CustomRoles.dataType.value) == EnumMeta:
editor: QComboBox
current_value = index.data(Qt.EditRole)
combo_index = editor.findText(current_value)
if combo_index >= 0:
editor.setCurrentIndex(combo_index)
else:
QStyledItemDelegate.setEditorData(self, editor, index)
def setModelData(
self, editor: QWidget, model: QAbstractItemModel, index: QModelIndex
) -> None:
if index.data(CustomRoles.dataType.value) == EnumMeta:
editor: QComboBox
current_value = editor.currentText()
model.setData(index, current_value, Qt.EditRole)
else:
QStyledItemDelegate.setModelData(self, editor, model, index)
Подробнее здесь: https://stackoverflow.com/questions/791 ... leview-not
В PyQT 5 всегда отображайте QCombobox, используя QStyledItemDelegate в QTableView (не только при редактировании ячейки) ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Получение размера элемента (QStyledItemDelegate?) в QTableView в PyQt5?
Anonymous » » в форуме Python - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Получение размера элемента (QStyledItemDelegate?) в QTableView в PyQt5?
Anonymous » » в форуме Python - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-