def paint(self, painter, option, index): if index.column() == 0: painter.save() icon = QtGui.QIcon() icon.addPixmap( QtGui.QPixmap(":/spotify/resources/icons/play_lgrey.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) palette = QApplication.palette() opt = QStyleOptionButton() opt.icon = icon opt.iconSize = QtCore.QSize(30, 30) opt.rect = option.rect color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else QtGui.QColor(index.model().data(index, Qt.BackgroundColorRole)) if self._pressed and self._pressed == (index.row(), index.column()): opt.state = QStyle.State_Enabled | QStyle.State_Sunken opt.icon = icon elif self._hover and self._hover == (index.row(), index.column()): print('oh man') opt.icon = icon else: opt.state = QStyle.State_Enabled | QStyle.State_Raised QApplication.style().drawControl(QStyle.CE_PushButtonLabel, opt, painter) painter.restore() else: QStyledItemDelegate.paint(self, painter, option, index)
def setModelData(self, editor, model, index): """ Get the data from our custom editor and stuffs it into the model. """ if index.column() == 3: model.setData(index, editor.starRating.starCount) else: QStyledItemDelegate.setModelData(self, editor, model, index)
def paint(self, painter, option, index): # if item selected, override default theme # Keeps verdict color for cells and use a bold font if option.state & QStyle.State_Selected: option.state &= ~QStyle.State_Selected option.font.setBold(True) QStyledItemDelegate.paint(self, painter, option, index) item = index.model().get_item(index) if item and item.downloading: # Draw progress bar progressBarOption = QStyleOptionProgressBar() progressBarHeight = option.rect.height() / 4 progressBarOption.rect = QRect( option.rect.x(), option.rect.y() + (option.rect.height() - progressBarHeight), option.rect.width(), progressBarHeight, ) progressBarOption.minimum = 0 progressBarOption.maximum = 100 progressBarOption.textAlignment = Qt.AlignCenter progressBarOption.progress = item.progress QApplication.style().drawControl(QStyle.CE_ProgressBar, progressBarOption, painter)
def paint(self, painter, option, index): """ Paint the items in the table. If the item referred to by <index> is a StarRating, we handle the painting ourselves. For the other items, we let the base class handle the painting as usual. In a polished application, we'd use a better check than the column number to find out if we needed to paint the stars, but it works for the purposes of this example. """ if index.column() == 3: starRating = StarRating(index.data()) # If the row is currently selected, we need to make sure we # paint the background accordingly. if option.state & QStyle.State_Selected: # The original C++ example used option.palette.foreground() to # get the brush for painting, but there are a couple of # problems with that: # - foreground() is obsolete now, use windowText() instead # - more importantly, windowText() just returns a brush # containing a flat color, where sometimes the style # would have a nice subtle gradient or something. # Here we just use the brush of the painter object that's # passed in to us, which keeps the row highlighting nice # and consistent. painter.fillRect(option.rect, painter.brush()) # Now that we've painted the background, call starRating.paint() # to paint the stars. starRating.paint(painter, option.rect, option.palette) else: QStyledItemDelegate.paint(self, painter, option, index)
def __init__(self, parent=None): QStyledItemDelegate.__init__(self, parent) self.timestamp_delegate = TimestampDelegate() self.float_delegate = FloatDelegate(2) self.default = QStyledItemDelegate() self.delegates = {}
def __init__(self, view: QTreeWidget): """ Overwrite QTreeWidget Item Edit Behaviour :param QTreeWidget view: View we replace delegates in """ super(KnechtValueDelegate, self).__init__(view) self.view = view self.default_delegate = QStyledItemDelegate(view)
def __init__(self, view): """ Basic item delegate that returns the views default item delegate or depending on the item type column: an appropriate custom render setting item delegate. :param modules.itemview.treeview.KnechtTreeView view: View we replace delegates in """ super(KnechtValueDelegate, self).__init__(view) self.view = view self.default_delegate = QStyledItemDelegate(view) self.setting_delegate = None
def paint(self, painter, option, index): if option.state & QStyle.State_Selected: item_color = index.data(role=Qt.TextColorRole) if item_color is None: item_color = Qt.black option.palette.setColor(QPalette.HighlightedText, item_color) color = self.combineColors(self.color_default, self.background(option, index)) option.palette.setColor(QPalette.Highlight, color) QStyledItemDelegate.paint(self, painter, option, index)
def testIntDelegate(self): """PYSIDE-1250: When creating a QVariant, use int instead of long long for anything that fits into a int. Verify by checking that a spin box is created as item view editor for int.""" item = QStandardItem() item.setData(123123, Qt.EditRole) # <-- QVariant conversion here model = QStandardItemModel() model.appendRow(item) style_option = QStyleOptionViewItem() delegate = QStyledItemDelegate() editor = delegate.createEditor(None, style_option, model.index(0, 0)) self.assertEqual(type(editor), QSpinBox)
def __init__(self, tolerance=None, allow_tail=True, colors=False, parent=None): self._parent = parent QStyledItemDelegate.__init__(self, parent) try: self._tolerance = int(tolerance) except (ValueError, TypeError): self._tolerance = self.DEFAULT_TOLERANCE self._allow_tail = allow_tail self._colors = colors self._color = None
def paint(self, painter, option, index): """ paint event handler @param painter: @type painter: @param option: @type option: @param index: @type index: """ rect = QRectF(option.rect) layer = self.parent().img.layersStack[-1 - index.row()] # mask and merged layer flag (for exposure fusion and HDR merge) if index.column() == 2: if self.parent().img is not None: if layer.maskIsSelected: text = index.data() + ' *' else: text = index.data() + ' ' if layer.mergingFlag: text = text + ' +' if layer.maskIsEnabled: painter.save() painter.setPen(Qt.red) painter.drawText(rect, text, QTextOption(Qt.AlignCenter)) painter.restore() return painter.drawText(rect, text, QTextOption(Qt.AlignCenter)) # visibility elif index.column() == 0: painter.save() bgColor = option.palette.color(QPalette.Window) bgColor = bgColor.red(), bgColor.green(), bgColor.blue() dark = (max(bgColor) <= 128) if option.state & QStyle.State_Selected: c = option.palette.color(QPalette.Highlight) painter.fillRect(rect, c) if self.parent().img.layersStack[-1 - index.row()].visible: px = self.inv_px1 if dark or ( option.state & QStyle.State_Selected) else self.px1 else: px = self.inv_px2 if dark or ( option.state & QStyle.State_Selected) else self.px2 painter.drawPixmap(rect, px, QRectF(0, 0, px.width(), px.height())) painter.restore() else: # call default QStyledItemDelegate.paint(self, painter, option, index)
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Freeze, self).__init__(self.windowMain) self.window = ToolSeq_Freeze.qUiLoader.load( ToolSeq_Freeze.userScriptDir + 'ToolSeq_Freeze.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Freeze.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Widget_01.installEventFilter(self) self.window.Text_Transforms.textChanged.connect(self.Event_Transforms) self.window.Button_ApplyTransformationMatrix.installEventFilter(self) self.window.Button_UnFreezeTranslate.installEventFilter(self) self.window.Button_UnFreezeScale.installEventFilter(self) self.window.show() maya_cmds.loadPlugin('ToolSeq_Freeze.py', quiet=True)
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Displacement, self).__init__(self.windowMain) self.window = ToolSeq_Displacement.qUiLoader.load( ToolSeq_Displacement.userScriptDir + 'ToolSeq_Displacement.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Displacement.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Widget_Shape.installEventFilter(self) self.window.Button_ImagePath.installEventFilter(self) self.window.Button_Apply.installEventFilter(self) self.window.show() maya_cmds.loadPlugin('ToolSeq_Displacement.py', quiet=True)
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Unaverage, self).__init__(self.windowMain) self.window = ToolSeq_Unaverage.qUiLoader.load( ToolSeq_Unaverage.userScriptDir + 'ToolSeq_Unaverage.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Unaverage.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Widget_01.installEventFilter(self) self.window.Text_Items.textChanged.connect(self.Event_Items) self.window.Button_Apply.installEventFilter(self) self.window.show() wMax = max(self.window.Label_Amount.width(), self.window.Label_Blend.width(), self.window.Label_Iterations.width()) self.window.Label_Amount.setFixedWidth(wMax) self.window.Label_Blend.setFixedWidth(wMax) self.window.Label_Iterations.setFixedWidth(wMax)
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Cameras, self).__init__(self.windowMain) self.window = ToolSeq_Cameras.qUiLoader.load( ToolSeq_Cameras.userScriptDir + 'ToolSeq_Cameras.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Cameras.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Button_P.installEventFilter(self) self.window.Button_TB.installEventFilter(self) self.window.Button_FB.installEventFilter(self) self.window.Button_LR.installEventFilter(self) maya_cmds.undoInfo(stateWithoutFlush=False) try: self.Activity_CreateAll() except Exception as e: print >> stderr, str(e) maya_cmds.undoInfo(stateWithoutFlush=True) self.window.show()
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Developer, self).__init__(self.windowMain) self.window = ToolSeq_Developer.qUiLoader.load( ToolSeq_Developer.userScriptDir + 'ToolSeq_Developer.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Developer.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Radio_Attributes.toggled.connect(self.Event_Attributes) self.window.Radio_CommandsMel.toggled.connect(self.Event_CommandsMel) self.window.Radio_CommandsPython.toggled.connect( self.Event_CommandsPython) self.window.Radio_VariablesMel.toggled.connect(self.Event_Variables) self.window.Line_Attributes.installEventFilter(self) self.window.Line_Search.installEventFilter(self) self.window.Button_Search.installEventFilter(self) self.window.Button_Act.installEventFilter(self) self.window.setEnabled(False) self.window.show() Thread(target=self.__init__later, args=()).start()
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance(long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_UV, self).__init__(self.windowMain) self.window = ToolSeq_UV.qUiLoader.load(ToolSeq_UV.userScriptDir + 'ToolSeq_UV.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_UV.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Widget_01.installEventFilter(self) self.window.Widget_012.installEventFilter(self) self.window.Text_Items.textChanged.connect(self.Event_Items) self.window.Combo_CutType.currentTextChanged.connect(self.Event_CutType) self.window.Button_Apply.installEventFilter(self) self.window.show() wMax1 = max(self.window.Check_Unfold.width(), self.window.Check_Optimize.width()) self.window.Check_Unfold.setFixedWidth(wMax1) self.window.Check_Optimize.setFixedWidth(wMax1) wMax2 = max(self.window.Label_TileSpace.width(), self.window.Label_ShellSpace.width()) self.window.Label_TileSpace.setFixedWidth(wMax2) self.window.Label_ShellSpace.setFixedWidth(wMax2)
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Namer, self).__init__(self.windowMain) self.window = ToolSeq_Namer.qUiLoader.load( ToolSeq_Namer.userScriptDir + 'ToolSeq_Namer.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Namer.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.List_IdenticalNames.itemSelectionChanged.connect( self.Action_SelectList) self.window.Button_AppendToList.installEventFilter(self) self.window.Button_ExtractFromList.installEventFilter(self) self.window.Button_ClearList.installEventFilter(self) self.window.Button_RenameAutoInList.installEventFilter(self) self.window.show()
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Select, self).__init__(self.windowMain) self.window = ToolSeq_Select.qUiLoader.load( ToolSeq_Select.userScriptDir + 'ToolSeq_Select.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Select.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Button_LR_Standart.installEventFilter(self) self.window.Button_LR_Pattern.installEventFilter(self) self.window.Button_SelectSoft.installEventFilter(self) self.window.Button_SelectHard.installEventFilter(self) self.window.Button_SelectBorder.installEventFilter(self) self.window.Button_SelectAngle.installEventFilter(self) self.window.Button_Operate.installEventFilter(self) self.window.Button_Clear.installEventFilter(self) self.window.Button_Show.installEventFilter(self) self.window.show()
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Uninstance, self).__init__(self.windowMain) self.window = ToolSeq_Uninstance.qUiLoader.load( ToolSeq_Uninstance.userScriptDir + 'ToolSeq_Uninstance.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Uninstance.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Widget_01.installEventFilter(self) self.window.Text_Transforms.textChanged.connect(self.Event_Transforms) self.window.Button_UninstanceHierarchy.installEventFilter(self) self.window.Widget_SourceShape.installEventFilter(self) self.window.Widget_TargetShape.installEventFilter(self) self.window.Button_AddInstancedShape.installEventFilter(self) self.window.Button_ReplaceInstancedShape.installEventFilter(self) self.window.show()
def __init__(self): ptrs.append(self) self.windowMain = wrapInstance( long(OpenMayaUI_v1.MQtUtil.mainWindow()), QMainWindow) super(ToolSeq_Pivot, self).__init__(self.windowMain) self.window = ToolSeq_Pivot.qUiLoader.load( ToolSeq_Pivot.userScriptDir + 'ToolSeq_Pivot.ui', self) self.window.destroyed.connect(lambda: ptrs_remove(self)) self.window.setWindowFlags(self.window.windowFlags() & ~Qt.WindowMinMaxButtonsHint) self.window.setAttribute(Qt.WA_DeleteOnClose) for qComboBox in self.window.findChildren(QComboBox): qComboBox.setItemDelegate(QStyledItemDelegate(qComboBox)) with open(ToolSeq_Pivot.userScriptDir + 'ToolSeq.qss', 'r') as fileStyleSheet: self.window.setStyleSheet(fileStyleSheet.read()) self.window.Widget_01.installEventFilter(self) self.window.Text_Transforms.textChanged.connect(self.Event_Transforms) self.window.Button_Minimum.installEventFilter(self) self.window.Button_Maximum.installEventFilter(self) self.window.Button_Center.installEventFilter(self) self.window.Button_CreateLocators.installEventFilter(self) self.window.Widget_Locator.installEventFilter(self) self.window.Button_MoveTransformsPivot.installEventFilter(self) self.window.Button_MatchTransformsPivot.installEventFilter(self) self.window.show()
def sizeHint(self, option, index): """ Returns the size needed to display the item in a QSize object. """ if index.column() == 3: starRating = StarRating(index.data()) return starRating.sizeHint() else: return QStyledItemDelegate.sizeHint(self, option, index)
def createEditor(self, parent, option, index): """ Creates and returns the custom StarEditor object we'll use to edit the StarRating. """ if index.column() == 3: editor = StarEditor(parent) editor.editingFinished.connect(self.commitAndCloseEditor) return editor else: return QStyledItemDelegate.createEditor(self, parent, option, index)
def createEditor(self, parent, option, index): if index.column() == 0: pushButton = QPushButton(parent) pushButton.hide() #pushButton.setIcon(QtGui.QPixmap(":/spotify/resources/icons/play_lgrey.png"), QtGui.QIcon.Normal, # QtGui.QIcon.Off) return pushButton else: return QStyledItemDelegate.createEditor(self, parent, option, index)
def sizeHint(self, option, index): """ Returns the size needed to display the item in a QSize object. """ imagen = QPixmap() try: imagen.loadFromData(index.data()) # , Qt.DisplayRole except TypeError: return QStyledItemDelegate.sizeHint(self, option, index) + QSize( 1, 1) size_hint = QSize(5 * imagen.width(), imagen.height()) + QSize(1, 1) return size_hint
def editorEvent(self, event: QEvent, model: 'PropModel', option: QStyleOptionViewItem, index: QModelIndex) -> bool: """ Change the data in the model and the state of the checkbox if the user presses the left mouse button or presses Key_Space or Key_Select and this cell is editable. Otherwise do nothing. :param event: The event that will take place to trigger the editor Event. :type event: QEvent :param model: The model that our delegate will render. :type model: PropModel :param option: Option for the kind've event that takes place. :type option: QStyleOptionViewItem :param index: Index of the events. :type index: QModelIndex :return: true if the given editor is a valid QWidget and the given event is handled; otherwise returns false. :rtype: bool """ event.type() if not (index.flags() & QtCore.Qt.ItemIsEditable) > 0: return False data = index.internalPointer() if index.column() != 1 or not isinstance( data, Property) or data.getType() != bool: return QStyledItemDelegate.editorEvent(self, event, model, option, index) if data.isReadOnly(): return False # Do not change the checkbox-state if event.type() == QEvent.MouseButtonPress: return False if event.type() == QEvent.MouseButtonRelease or event.type( ) == QEvent.MouseButtonDblClick: if event.button( ) != QtCore.Qt.LeftButton or not self.getCheckBoxRect( option).contains(event.pos()): return False if event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: if event.key() != QtCore.Qt.Key_Space and event.key( ) != QtCore.Qt.Key_Select: return False else: return False # Change the checkbox-state checkbox = QCheckBox('temp') checkbox.setChecked(not data.getValue()) self.setModelData(checkbox, model, index) return True
def testDelegates(self): widget = Widget() t = widget.treeView # When calling setItemDelegateForColumn using a separate variable # for the second argument (QAbstractItemDelegate), there was no problem # on keeping the reference to this object, since the variable was kept # alive (case A) # Contrary, when instantiating this argument on the function call # Using QStyledItemDelegate inside the call the reference of the # object was lost, causing a segfault. (case B) # Case A d = QStyledItemDelegate() # Using QStyledItemDelegate from a variable so we keep the reference alive # and we encounter no segfault. t.setItemDelegateForColumn(0, d) # This raised the Segmentation Fault too, because manually destroying # the object caused a missing refrence. del d # Getting the delegates a = t.itemDelegateForColumn(0) self.assertIsInstance(a, QStyledItemDelegate) # Case B t.setItemDelegateForColumn(1, QStyledItemDelegate()) # Getting the delegates b = t.itemDelegateForColumn(1) self.assertIsInstance(b, QStyledItemDelegate) # Test for Rows t.setItemDelegateForRow(0, QStyledItemDelegate()) self.assertIsInstance(t.itemDelegateForRow(0), QStyledItemDelegate) # Test for general delegate t.setItemDelegate(QStyledItemDelegate()) self.assertIsInstance(t.itemDelegate(), QStyledItemDelegate)
def paint(self, painter: QStylePainter, option: QStyleOptionViewItem, index: QModelIndex) -> None: """ Paint a checkbox without the label. :param painter: This draws the widget. :type painter: QStylePainter :param option: Option for the style of checkbox. :type option: QStyleOptionViewItem :param index: Index for the painted checkbox. :type index: QModelIndex :return: None :rtype: NoneType """ QStyledItemDelegate.paint(self, painter, option, index) if index.column() == 1 and isinstance( index.internalPointer(), Property) and index.internalPointer().getType() == bool: checked = index.internalPointer().getValue() check_box_style_option = QtWidgets.QStyleOptionButton() if (index.flags() & QtCore.Qt.ItemIsEditable) > 0: check_box_style_option.state |= QtWidgets.QStyle.State_Enabled else: check_box_style_option.state |= QtWidgets.QStyle.State_ReadOnly if checked: check_box_style_option.state |= QtWidgets.QStyle.State_On else: check_box_style_option.state |= QtWidgets.QStyle.State_Off check_box_style_option.rect = self.getCheckBoxRect(option) check_box_style_option.state |= QtWidgets.QStyle.State_Enabled QtWidgets.QApplication.style().drawControl( QtWidgets.QStyle.CE_CheckBox, check_box_style_option, painter)
def paint(self, painter, option, index): """ Paint the items in the table. If the item referred to by <index> is a StarRating, we handle the painting ourselves. For the other items, we let the base class handle the painting as usual. In a polished application, we'd use a better check than the column number to find out if we needed to paint the stars, but it works for the purposes of this example. """ if option.state & QStyle.State_Enabled: if option.state & QStyle.State_Active: color_group = QPalette.Normal else: color_group = QPalette.Inactive else: color_group = QPalette.Disabled if option.state & QStyle.State_Selected: painter.fillRect( option.rect, option.palette.color(color_group, QPalette.Highlight)) imagen = QPixmap() try: imagen.loadFromData(index.data()) # , Qt.DisplayRole except TypeError: return QStyledItemDelegate.paint(self, painter, option, index) imagen = imagen.scaled(QSize(16, 16), Qt.KeepAspectRatio) width = imagen.width() height = imagen.height() """Para calcular el lugar donde se tiene que dibujar 1. option.rect te da: (0, 0, width() , height() ) of the image 2. Del punto en el que hay que empezar a dibujar, le sumamos la mitad del ancho de la celda 3. Y luego le restamos el ancho del objeto para que quede en la mitad """ x = option.rect.x() + (option.rect.width() / 2) - (width / 2) y = option.rect.y() + (option.rect.height() / 2) - (height / 2) # x,y cordenadas - 32,32 ancho y alto de la imagen - la imagen painter.drawPixmap(x, y, imagen) pen = painter.pen() painter.setPen(option.palette.color(QPalette.Mid))
class KnechtValueDelegate(QStyledItemDelegate): def __init__(self, view: QTreeWidget): """ Overwrite QTreeWidget Item Edit Behaviour :param QTreeWidget view: View we replace delegates in """ super(KnechtValueDelegate, self).__init__(view) self.view = view self.default_delegate = QStyledItemDelegate(view) def createEditor(self, parent: QWidget, option: QStyleOptionViewItem, index: QModelIndex) -> QWidget: editor = QLineEdit(parent) current_value = index.model().data(index, Qt.EditRole) editor.setText(current_value) return editor def setModelData(self, editor, model, index): current_value = index.data(Qt.DisplayRole) new_value = editor.text() if new_value == current_value: return if not index.parent().isValid(): # Do not edit Top-Level items self.view.info_overlay.display( _('Action List Namen können nicht editieren werden.'), 3000) return LOGGER.debug('Editor Model Data %s %s %s %s', new_value, index.data(Qt.DisplayRole), index.row(), index.parent().row()) undo_cmd = ItemEditUndoCommand(current_value, new_value, index, view_name=self.view.objectName()) self.view.undo_stack.push(undo_cmd) self.view.undo_stack.setActive(True) return self.default_delegate.setModelData(editor, model, index)
def __init__(self, parent): QStyledItemDelegate.__init__(self, parent)