Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to avouid duplicate entries into QlistWidget when adding new item
Forum Updated to NodeBB v4.3 + New Features

How to avouid duplicate entries into QlistWidget when adding new item

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 965 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bidd
    wrote on last edited by
    #1

    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.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • B bidd

      @JonB hope this is minimal

      import sys
      from PyQt5 import QtCore

      from 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_Trafficlightproperty

      class Virtual_TL_details:
      def init(self):
      self.id = None
      self.state = None
      self.arduino_id = None
      self.vehicle = None
      self.desired = None
      self.time = None

      class 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_())

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #7

      @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 time add is clicked. It does not just show a dialog, it also seems to connect a button's clicked signal. If that is called multiple times there will be multiple connects on the button. So parse_data() will be called multiple times, and that does the item = QListWidgetItem().

      Which is as I suggested might be the case originally. You should essentially only do object.signal.connect() once, at the time the object is created (or at the time the slot object is created, whichever is later). I think you can move it into your connectSignalsSlots().

      Quite separately: You do not need the str() in all those str(....[current]text()) expressions. Qt C++'s QStrings will be converted to strs for you in Python.

      B 1 Reply Last reply
      1
      • B bidd

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        B 1 Reply Last reply
        1
        • B bidd

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @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.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @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.

            B Offline
            B Offline
            bidd
            wrote on last edited by bidd
            #4
            This post is deleted!
            JonBJ 1 Reply Last reply
            0
            • B bidd

              This post is deleted!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @bidd
              Maybe someone else will look through this length of code. Otherwise, at least for my part, please produce a minimal example which illustrates the issue. Literally 90+% of the above looks irrelevant.

              B 1 Reply Last reply
              0
              • JonBJ JonB

                @bidd
                Maybe someone else will look through this length of code. Otherwise, at least for my part, please produce a minimal example which illustrates the issue. Literally 90+% of the above looks irrelevant.

                B Offline
                B Offline
                bidd
                wrote on last edited by
                #6

                @JonB hope this is minimal

                import sys
                from PyQt5 import QtCore

                from 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_Trafficlightproperty

                class Virtual_TL_details:
                def init(self):
                self.id = None
                self.state = None
                self.arduino_id = None
                self.vehicle = None
                self.desired = None
                self.time = None

                class 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_())

                JonBJ 1 Reply Last reply
                0
                • B bidd

                  @JonB hope this is minimal

                  import sys
                  from PyQt5 import QtCore

                  from 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_Trafficlightproperty

                  class Virtual_TL_details:
                  def init(self):
                  self.id = None
                  self.state = None
                  self.arduino_id = None
                  self.vehicle = None
                  self.desired = None
                  self.time = None

                  class 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_())

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @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 time add is clicked. It does not just show a dialog, it also seems to connect a button's clicked signal. If that is called multiple times there will be multiple connects on the button. So parse_data() will be called multiple times, and that does the item = QListWidgetItem().

                  Which is as I suggested might be the case originally. You should essentially only do object.signal.connect() once, at the time the object is created (or at the time the slot object is created, whichever is later). I think you can move it into your connectSignalsSlots().

                  Quite separately: You do not need the str() in all those str(....[current]text()) expressions. Qt C++'s QStrings will be converted to strs for you in Python.

                  B 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @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 time add is clicked. It does not just show a dialog, it also seems to connect a button's clicked signal. If that is called multiple times there will be multiple connects on the button. So parse_data() will be called multiple times, and that does the item = QListWidgetItem().

                    Which is as I suggested might be the case originally. You should essentially only do object.signal.connect() once, at the time the object is created (or at the time the slot object is created, whichever is later). I think you can move it into your connectSignalsSlots().

                    Quite separately: You do not need the str() in all those str(....[current]text()) expressions. Qt C++'s QStrings will be converted to strs for you in Python.

                    B Offline
                    B Offline
                    bidd
                    wrote on last edited by
                    #8

                    @JonB

                    Thank you. your guidance helped have a closer look and redefine things. Cheers

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved