0

下面是我的示例代码:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(691, 327)
        self.listWidget = QtWidgets.QListWidget(Form)
        self.listWidget.setGeometry(QtCore.QRect(70, 40, 256, 192))
        self.listWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.listWidget.setObjectName("listWidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(360, 40, 261, 191))
        self.plainTextEdit.setTabChangesFocus(True)
        self.plainTextEdit.setReadOnly(True)
        self.plainTextEdit.setObjectName("plainTextEdit")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

        lists = ["one","two","Three"]
        for i in lists:
            item = QtWidgets.QListWidgetItem(i)
            item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
            item.setCheckState(QtCore.Qt.Unchecked)
            self.listWidget.addItem(item)

        self.listWidget.itemClicked.connect(self.on_listWidget_itemClicked)

    def on_listWidget_itemClicked(self, item):
        lines = self.plainTextEdit.toPlainText()
        line_list = []
        #if item.listWidget().itemWidget(item) != None: 
        if item.checkState() == QtCore.Qt.Checked:
            self.plainTextEdit.appendPlainText(item.text())
        elif item.checkState() == QtCore.Qt.Unchecked:
            print(item.text())
            
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

在这个例子中,我将 QListWidget 与 ItemIsUserCheckable 和 QPlainTextEdit 一起使用。我能够使用复选框从 Qlistwidget 获取项目到 QPlainTextEdit。如果我检查 Qlistwidget 中的项目,它可以将文本设置为 QPlaintextEdit。但是当我取消选中项目框时,我想删除或删除 QplainTextEdit 中的相同文本。例如,如果我在 Qlistwidget “one”中选中该项目,它会将“one”设置为 QplainTextEdit,并且当我取消选中该项目时应该删除相同的项目。如果可以的话,有可能怎么办?

4

1 回答 1

1

不要修改 Qt Designer 生成的代码,而是创建另一个继承自相应小部件的类并使用初始类来填充它。

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(691, 327)
        self.listWidget = QtWidgets.QListWidget(Form)
        self.listWidget.setGeometry(QtCore.QRect(70, 40, 256, 192))
        self.listWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.listWidget.setObjectName("listWidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(360, 40, 261, 191))
        self.plainTextEdit.setTabChangesFocus(True)
        self.plainTextEdit.setReadOnly(True)
        self.plainTextEdit.setObjectName("plainTextEdit")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
          
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))


class MainWindow(QtWidgets.QWidget, Ui_Form):                             
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        lists = ["one","two","Three"]
        for i in lists:
            item = QtWidgets.QListWidgetItem(i)
            item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
            item.setCheckState(QtCore.Qt.Unchecked)
            self.listWidget.addItem(item)
            
#        self.listWidget.itemClicked.connect(self.on_listWidget_itemClicked)

    def on_listWidget_itemClicked(self, item):
        if item.checkState() == QtCore.Qt.Checked:
            self.plainTextEdit.appendPlainText(item.text())
        elif item.checkState() == QtCore.Qt.Unchecked:
            text = self.plainTextEdit.toPlainText()            # 1
            text = text.replace(item.text(), '')               # 2 
            _list = text.split()                               # 3
            text = '\n'.join(_list)                            # 4
            self.plainTextEdit.setPlainText(text)              # 5  


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = MainWindow() #QtWidgets.QWidget()
#    ui = Ui_Form()
#    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

在此处输入图像描述

在此处输入图像描述


更新

我们可以在仅选中复选框时这样做吗,我的意思是在选中复选框时文本应该附加到 Plaintextedit 中。但即使选择了该项目,它也会附加它。

抱歉,我忘了在问题中添加一件事。有时列表中不仅有单个单词,而且还有两个两个单词,例如 First name 和 last names 也 like lists = ["Fname1 Lname1", "Fname2 Lname2", "Fname3 LName3"]。我们可以这样做吗?

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(691, 327)
        self.listWidget = QtWidgets.QListWidget(Form)
        self.listWidget.setGeometry(QtCore.QRect(70, 40, 256, 192))
        self.listWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.listWidget.setObjectName("listWidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(360, 40, 261, 191))
        self.plainTextEdit.setTabChangesFocus(True)
        self.plainTextEdit.setReadOnly(True)
        self.plainTextEdit.setObjectName("plainTextEdit")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
          
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))


class MainWindow(QtWidgets.QWidget, Ui_Form):                             
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        
        self.lists = {                                                   # !!! self.lists = {...}
            "Fname1 Lname1": 0, 
            "Fname2 Lname2": 0, 
            "Fname3 LName3": 0
        }  
#        lists = ["one", "two", "Three"]
        for i in self.lists.keys():                                       # + .keys()
            item = QtWidgets.QListWidgetItem(i)
            item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
            item.setCheckState(QtCore.Qt.Unchecked)
            self.listWidget.addItem(item)
           
#        self.listWidget.itemClicked.connect(self.on_listWidget_itemClicked)

    def on_listWidget_itemClicked(self, item):    
        #                                         +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvv
        if item.checkState() == QtCore.Qt.Checked and self.lists[item.text()] == 0:
            self.plainTextEdit.appendPlainText(item.text())
            self.lists[item.text()] = 1                                   # +
        elif item.checkState() == QtCore.Qt.Unchecked:
            text = self.plainTextEdit.toPlainText()
            text = text.replace(item.text(), '')
#            _list = text.split()              
            _list = text.split('\n')                                      # + '\n'
            _list = [ i for i in _list if i ]                             # +
            text = '\n'.join(_list)              
            self.plainTextEdit.setPlainText(text)   
            self.lists[item.text()] = 0                                   # +


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = MainWindow() 
    Form.show()
    sys.exit(app.exec_())

在此处输入图像描述

于 2020-09-06T20:02:07.467 回答