如果未选中项目,如何从 Qlistidget 中删除 QPlaintextEdit 中的确切文本?
How to remove exact text from QPlaintextEdit from Qlistidget if unchecked item?
我想在取消选中项目时从 Qlistwidget 的 QPlaintextEdit 中删除完全匹配的文本。
它正在删除项目,但也删除其他匹配的文本。例如:有“Fname1 Lname1”,
“Fname2 Lname2”、“Fname3 LName3”、“Fname”、“Lname1”。我选择了所有项目,我想删除带有未选中项目“Fname”的项目。它删除了单个“Fname”并删除了“Fname”中的“ Fname1 Lname1”,输出为“1 Lname1”。我只想删除确切的项目(文本)。怎么办?
下面是我的示例代码:
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 = {
"Fname1 Lname1": 0,
"Fname2 Lname2": 0,
"Fname3 LName3": 0,
"Fname":0,
"lname1":0
}
for i in self.lists.keys():
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
def on_listWidget_itemClicked(self, item):
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('\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_())
试一试:
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 = {
"Fname1 Lname1": 0,
"Fname2 Lname2": 0,
"Fname3 LName3": 0,
"Fname":0,
"lname1":0
}
for i in self.lists.keys():
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
def on_listWidget_itemClicked(self, item):
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('\n')
# _list = [ i for i in _list if i ] # ---
_list = [ i for i in _list if i != item.text() ] # +++
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_())
我想在取消选中项目时从 Qlistwidget 的 QPlaintextEdit 中删除完全匹配的文本。 它正在删除项目,但也删除其他匹配的文本。例如:有“Fname1 Lname1”, “Fname2 Lname2”、“Fname3 LName3”、“Fname”、“Lname1”。我选择了所有项目,我想删除带有未选中项目“Fname”的项目。它删除了单个“Fname”并删除了“Fname”中的“ Fname1 Lname1”,输出为“1 Lname1”。我只想删除确切的项目(文本)。怎么办? 下面是我的示例代码:
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 = {
"Fname1 Lname1": 0,
"Fname2 Lname2": 0,
"Fname3 LName3": 0,
"Fname":0,
"lname1":0
}
for i in self.lists.keys():
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
def on_listWidget_itemClicked(self, item):
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('\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_())
试一试:
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 = {
"Fname1 Lname1": 0,
"Fname2 Lname2": 0,
"Fname3 LName3": 0,
"Fname":0,
"lname1":0
}
for i in self.lists.keys():
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
def on_listWidget_itemClicked(self, item):
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('\n')
# _list = [ i for i in _list if i ] # ---
_list = [ i for i in _list if i != item.text() ] # +++
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_())