如何从 Qlistwidget 未选中的项目中删除 QPlainTextEdit 中的匹配字符串(文本)?
How to remove matched string ( text ) in QPlainTextEdit from Qlistwidget unchecked item?
下面是我的示例代码:
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 中的项目“一个”,它会将“一个”设置到 QplainTextEdit 中,并且当我取消选中该项目时应删除相同的项目。可以的话可以吗?
不要修改 Qt Designer 生成的代码,而是创建另一个 class 继承
来自适当的小部件并使用初始 class 来填充它。
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_())
更新
And Can we do it when checkbox checked only , I mean the text should appended into Plaintextedit when checkbox checked. But it appends it even when selected the item.
Sorry, I forgot to add One more thing in the question. Some times the list has not only single words but also two two words like First name and last names also like lists = ["Fname1 Lname1", "Fname2 Lname2", "Fname3 LName3"]
. Can we do it like this?
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_())
下面是我的示例代码:
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 中的项目“一个”,它会将“一个”设置到 QplainTextEdit 中,并且当我取消选中该项目时应删除相同的项目。可以的话可以吗?
不要修改 Qt Designer 生成的代码,而是创建另一个 class 继承 来自适当的小部件并使用初始 class 来填充它。
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_())
更新
And Can we do it when checkbox checked only , I mean the text should appended into Plaintextedit when checkbox checked. But it appends it even when selected the item.
Sorry, I forgot to add One more thing in the question. Some times the list has not only single words but also two two words like First name and last names also like
lists = ["Fname1 Lname1", "Fname2 Lname2", "Fname3 LName3"]
. Can we do it like this?
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_())