Ejemplo n.º 1
0
    def setModelData(self, editor: QWidget, model: QAbstractItemModel,
                     index: QModelIndex):

        text = editor.text()
        if text == "":
            text = editor.placeholderText()
        # Update the "default" text to the previous value edited in
        self._default_text = text
        model.setData(index, text, Qt.DisplayRole)
Ejemplo n.º 2
0
    def setModelData(self, editor: QWidget, model: QAbstractItemModel,
                     index: QModelIndex) -> None:
        editor: QFileDialog
        result: int = editor.result()
        if result == QDialog.Accepted:
            # if accepted, this means that the user also wanted to overwrite the file
            dstPath: str = editor.selectedFiles()[0]
            dstPath: Path = Path(dstPath)

            model.setData(index, dstPath, Qt.EditRole)
Ejemplo n.º 3
0
    def editorEvent(
        self,
        event: QtCore.QEvent,
        model: QtCore.QAbstractItemModel,
        option: QStyleOptionViewItem,
        index: QtCore.QModelIndex,
    ) -> bool:
        """Called when an event has occured in the editor.

        This can be used to customize how the delegate handles mouse/key events
        """
        if (event.type() == event.MouseButtonRelease
                and event.button() == Qt.RightButton):
            self.show_context_menu(index, model, event.globalPos(),
                                   option.widget)

        # if the user clicks quickly on the visibility checkbox, we *don't*
        # want it to be interpreted as a double-click.  We want the visibilty
        # to simply be toggled.
        if event.type() == event.MouseButtonDblClick:
            self.initStyleOption(option, index)
            style = option.widget.style()
            check_rect = style.subElementRect(
                style.SE_ItemViewItemCheckIndicator, option, option.widget)
            if check_rect.contains(event.pos()):
                cur_state = index.data(Qt.CheckStateRole)
                if model.flags(index) & Qt.ItemIsUserTristate:
                    state = Qt.CheckState((cur_state + 1) % 3)
                else:
                    state = Qt.Unchecked if cur_state else Qt.Checked
                return model.setData(index, state, Qt.CheckStateRole)
        # refer all other events to the QStyledItemDelegate
        return super().editorEvent(event, model, option, index)
Ejemplo n.º 4
0
    def editorEvent(self, event: QtCore.QEvent,
                    model: QtCore.QAbstractItemModel,
                    option: 'QStyleOptionViewItem',
                    index: QtCore.QModelIndex) -> bool:
        decorationRect = option.rect

        if event.type() == QEvent.MouseButtonPress and calculate_middle_rect(
                decorationRect, 20, 20).contains(event.pos()):
            item = index.model().data(index, Qt.UserRole)

            if isinstance(item, QVariant):
                return QStyledItemDelegate.editorEvent(self, event, model,
                                                       option, index)

            value = item['value']
            type = item['type']
            if type == 'checkbox':
                # 数据转换
                value = 1 if value == 0 else 0
                model.setData(index, value, Qt.DisplayRole)

        return QStyledItemDelegate.editorEvent(self, event, model, option,
                                               index)