How to avouid duplicate entries into QlistWidget when adding new item
-
Re: Remove duplicate entries from QListWidget?
When add item button is clicked, for the first time one item is added to the list. On the second occasion two items are added to the existing list (2* second item) . How can this behavior be avoided. I am not sure how to add minimal reproducible code here.
-
@bidd said in How to avouid duplicate entries into QlistWidget when adding new item:
self.dialog_TL.show() self.ui_Tl.tl_confirm.clicked.connect(lambda: self.parse_data('North_South_List'))
Here is my guess.
tl_prop()
is a slot, called each timeadd
is clicked. It does not just show a dialog, it also seems to connect a button'sclicked
signal. If that is called multiple times there will be multipleconnect
s on the button. Soparse_data()
will be called multiple times, and that does theitem = QListWidgetItem()
.Which is as I suggested might be the case originally. You should essentially only do
object.signal.connect()
once, at the time theobject
is created (or at the time the slot object is created, whichever is later). I think you can move it into yourconnectSignalsSlots()
.Quite separately: You do not need the
str()
in all thosestr(....[current]text())
expressions. Qt C++'sQString
s will be converted tostr
s for you in Python. -
@bidd said in How to avouid duplicate entries into QlistWidget when adding new item:
I am not sure how to add minimal reproducible code here
Show the code where you add entries.
You're apparently doing something wrong if second time two items are added. -
@bidd said in How to avouid duplicate entries into QlistWidget when adding new item:
or the first time one item is added to the list. On the second occasion two items are added
Are you perchance doing a new
connect()
where you added the (first) item? If you have multiple connects from same signal->slot the slot gets executed multiple times. -
@JonB hope this is minimal
import sys
from PyQt5 import QtCorefrom PyQt5.QtWidgets import (
QApplication, QDialog, QMainWindow, QMessageBox,QListWidget,QListWidgetItem
)
from tl_cluster_dialog_ui import Ui_tl_cluster_define
from tl_prop_dialog_ui import Ui_Trafficlightpropertyclass Virtual_TL_details:
def init(self):
self.id = None
self.state = None
self.arduino_id = None
self.vehicle = None
self.desired = None
self.time = Noneclass Window(QMainWindow, Ui_tl_cluster_define):
def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.connectSignalsSlots() self.msg = QMessageBox() self.msg.setIcon(QMessageBox.Warning) self.msg.setWindowTitle("Warning Message") self.dialog_TL = QDialog() self.ui_Tl = Ui_Trafficlightproperty() self.ui_Tl.setupUi(self.dialog_TL) self.to_check = [self.ui_Tl.tl_id_value, self.ui_Tl.tl_arduino_value] self.to_check_v_t = [self.ui_Tl.tl_vehicle_value, self.ui_Tl.tl_time_value] self.modify_TL = False def connectSignalsSlots(self): self.add.clicked.connect(self.tl_prop) # self.delete_2.clicked.connect(self.delete_selected) # self.view.clicked.connect(self.view_selected) def tl_prop(self): self.dialog_TL.show() self.ui_Tl.tl_confirm.clicked.connect(lambda: self.parse_data('North_South_List')) def parse_data(self, name): #selected_index = getattr(self, name).selectedIndexes() self.dialog_TL.close() item = QListWidgetItem() item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled ) item.setCheckState(QtCore.Qt.Unchecked) item.setText('TL - ' + str(self.ui_Tl.tl_id_value.text())) item.details = Virtual_TL_details() item.details.id = str(self.ui_Tl.tl_id_value.text()) item.details.state = str(self.ui_Tl.tl_state_value.currentText()) item.details.arduino_id = str(self.ui_Tl.tl_arduino_value.text()) try: item.details.vehicle = str(self.ui_Tl.tl_vehicle_value.text()) item.details.time = float(self.ui_Tl.tl_time_value.text()) item.details.desired = str(self.ui_Tl.tl_desired_value.currentText()) except Exception: pass if getattr(self, name.rsplit("_", 1)[0]).isChecked(): getattr(self, name).addItem(item) getattr(self, name).clearSelection()
if name == "main":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_()) -
@bidd said in How to avouid duplicate entries into QlistWidget when adding new item:
self.dialog_TL.show() self.ui_Tl.tl_confirm.clicked.connect(lambda: self.parse_data('North_South_List'))
Here is my guess.
tl_prop()
is a slot, called each timeadd
is clicked. It does not just show a dialog, it also seems to connect a button'sclicked
signal. If that is called multiple times there will be multipleconnect
s on the button. Soparse_data()
will be called multiple times, and that does theitem = QListWidgetItem()
.Which is as I suggested might be the case originally. You should essentially only do
object.signal.connect()
once, at the time theobject
is created (or at the time the slot object is created, whichever is later). I think you can move it into yourconnectSignalsSlots()
.Quite separately: You do not need the
str()
in all thosestr(....[current]text())
expressions. Qt C++'sQString
s will be converted tostr
s for you in Python.