0

我正在制作一个翻译文本的程序(见截图)

截屏

我有三个班

用于显示编辑项目的窗口的类:

class StyleDelegate(QStyledItemDelegate):
        def __init__(self, parent=None):
            super(StyleDelegate, self).__init__()
        def createEditor(self, widget, style, index):
            self.mainWidget = QWidget(widget)
            self.line = QLineEdit() # line for input text 
            self.delButton= QPushButton('❌')  # button for delete current item
            self.trnButton = QPushButton('➕')  # button for make translation text in another QListView
            self.qhbLayout = QHBoxLayout()
            self.qhbLayout.addWidget(self.line)
            self.qhbLayout.addWidget(self.delButton)
            self.qhbLayout.addWidget(self.trnButton)
            self.mainWidget.setLayout(self.qhbLayout)
            return self.mainWidget
        # there is still a lot of code in this place

用于存储、添加、删除和编辑数据的类:

    class TranslateListModel(QAbstractListModel):
        def __init__(self, parent=None):
            super(TranslateListModel, self).__init__()
            self.words = ['1', '2', '3', '4']

        def removeItem(self, index):
            self.beginRemoveRows(index, index.row(), index.row())
            del self.words[index.row()]
            self.endRemoveRows()
            return True
        # there is still a lot of code in this place

程序的主要类:

class QTranslate(QtWidgets.QDialog, log.Ui_Dialog):
     def __init__(self):
        super().__init__()
        self.originalModel = TranslateListModel() 
        self.translateModel = TranslateListModel() 

        self.styleDelegate = StyleDelegate()

        self.originalLV.setModel(self.originalModel) 
        #QListView from Ui_Dialog

        self.translateLV.setModel(self.translateModel)
        #QListView from Ui_Dialog

        self.originalLV.setItemDelegate(self.styleDelegate)
        self.translateLV.setItemDelegate(self.styleDelegate)
    # there is still a lot of code in this place

如何使用 QStyledItemDelegate 实现按钮删除当前项目并更改另一个 QListView 中的翻译?我无法在 StyleDelegate 类之外访问这些按钮以将它们与 TranslateListModel 类的方法相关联。

4

1 回答 1

1

一种可能的解决方案是为委托创建信号并将它们连接到将删除或添加项目的函数,然后在单击按钮时发出这些信号:

class StyleDelegate(QStyledItemDelegate):
    deleteRequested = QtCore.pyqtSignal(int)
    translateRequested = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(StyleDelegate, self).__init__()
    def createEditor(self, widget, style, index):
        # note: I removed the "self" references as they're unnecessary
        mainWidget = QWidget(widget)
        line = QLineEdit()
        delButton= QPushButton('❌')
        trnButton = QPushButton('➕')
        qhbLayout = QHBoxLayout()
        qhbLayout.addWidget(line)
        qhbLayout.addWidget(delButton)
        qhbLayout.addWidget(trnButton)
        mainWidget.setLayout(qhbLayout)
        delButton.clicked.connect(
            lambda _, row=index.row(): self.deleteRequested.emit(row))
        trnButton.clicked.connect(
            lambda _, row=index.row(): self.translateRequested.emit(row))
        return mainWidget


class QTranslate(QtWidgets.QDialog, log.Ui_Dialog):
    def __init__(self):
        # ...
        self.originalLV.setItemDelegate(self.styleDelegate)
        self.styleDelegate.deleteRequested.connect(self.deleteRow)
        self.styleDelegate.translateRequested.connect(self.translateRow)

    def deleteRow(self, row):
        # ...

    def translateRow(self, row):
        # ...

请注意,您应该始终为每个视图使用唯一的委托实例,如文档中所述:

警告:您不应在视图之间共享同一委托实例。这样做可能会导致不正确或不直观的编辑行为,因为连接到给定委托的每个视图都可能收到 closeEditor() 信号,并尝试访问、修改或关闭已关闭的编辑器。

于 2020-04-20T20:43:50.307 回答