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 append Item text in EditText in QComboBox

How to append Item text in EditText in QComboBox

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
6 Posts 2 Posters 420 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.
  • C Offline
    C Offline
    crisp_toby
    wrote on 7 Sept 2024, 02:33 last edited by
    #1

    I'm programming a Combobox widget for a Tag system.
    I subclassed a QComboBox as TabComboBox. It has an editable LineEdit, i already set it. As shown in pic, it should have two features:
    1.popup still shown unless the area out of popup clicked (Solved)
    2.append item text behind current LineEdit text when item clicked
    tagcombobox.png
    I tried to build a variable in TabComboBox to store current LineEdit text, and connect currentIndexChanged signal to my custom function_add_tag . But when I click item, if item text is Toby, current LineEdit text will change to Toby,Toby, it should be Bob,Toby.
    Here is my code, contains popup holder.

    class TagComboBox(QComboBox):
        def __init__(self, tag_list, parent=None):
            super(TagComboBox, self).__init__(parent)
            self.setEditable(True)
            self.setFrame(False)
            self.addItems(tag_list)
            self.currentIndexChanged.connect(self._add_tag)
    
        def hidePopup(self):
            popup_view = self.view()
            if popup_view.rect().contains(popup_view.mapFromGlobal(QCursor().pos()))==False:
                super().hidePopup()
    
        def _add_tag(self, index):
            tag_str = self.itemText(index)
            src_str = self.lineEdit().text()
            dist_str = src_str + ',' + tag_str
            self.lineEdit().setText(dist_str)
    
    P 1 Reply Last reply 7 Sept 2024, 03:21
    0
    • C crisp_toby
      7 Sept 2024, 02:33

      I'm programming a Combobox widget for a Tag system.
      I subclassed a QComboBox as TabComboBox. It has an editable LineEdit, i already set it. As shown in pic, it should have two features:
      1.popup still shown unless the area out of popup clicked (Solved)
      2.append item text behind current LineEdit text when item clicked
      tagcombobox.png
      I tried to build a variable in TabComboBox to store current LineEdit text, and connect currentIndexChanged signal to my custom function_add_tag . But when I click item, if item text is Toby, current LineEdit text will change to Toby,Toby, it should be Bob,Toby.
      Here is my code, contains popup holder.

      class TagComboBox(QComboBox):
          def __init__(self, tag_list, parent=None):
              super(TagComboBox, self).__init__(parent)
              self.setEditable(True)
              self.setFrame(False)
              self.addItems(tag_list)
              self.currentIndexChanged.connect(self._add_tag)
      
          def hidePopup(self):
              popup_view = self.view()
              if popup_view.rect().contains(popup_view.mapFromGlobal(QCursor().pos()))==False:
                  super().hidePopup()
      
          def _add_tag(self, index):
              tag_str = self.itemText(index)
              src_str = self.lineEdit().text()
              dist_str = src_str + ',' + tag_str
              self.lineEdit().setText(dist_str)
      
      P Offline
      P Offline
      Pl45m4
      wrote on 7 Sept 2024, 03:21 last edited by Pl45m4 9 Jul 2024, 03:58
      #2

      @crisp_toby said in How to append Item text in EditText in QComboBox:

      I tried to build a variable in TabComboBox to store current LineEdit text, and connect currentIndexChanged signal to my custom function_add_tag . But when I click item, if item text is Toby, current LineEdit text will change to Toby,Toby, it should be Bob,Toby.

      You want Bob, Toby but you get Toby, Toby, correct?!

      You know what currentIndexChanged does? It is emitted after the text changed... so you already get the new, updated index and text from it...
      In your function you store the current text and text at index in a string and display it...
      However at the time your function is called both are identical as the lineEdit was updated and the notifier signal was fired, which you catch and in turn call your function.

      You need to store/update this

      src_str = self.lineEdit().text()
      

      somewhere else and not inside the signal-connected function for your current "iteration". You can save it there for the next one.

      Something like:

          def _add_tag(self, index):
              tag_str = self.itemText(index)
              dist_str = self.old_str + ',' + tag_str
              ## this is bad as in C++ the lineEdit is const, might work in Python though
              self.lineEdit().setText(dist_str)
              ## update former selection for next text update
              self.old_str = self.itemText(index)
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      C 2 Replies Last reply 7 Sept 2024, 04:48
      1
      • P Pl45m4
        7 Sept 2024, 03:21

        @crisp_toby said in How to append Item text in EditText in QComboBox:

        I tried to build a variable in TabComboBox to store current LineEdit text, and connect currentIndexChanged signal to my custom function_add_tag . But when I click item, if item text is Toby, current LineEdit text will change to Toby,Toby, it should be Bob,Toby.

        You want Bob, Toby but you get Toby, Toby, correct?!

        You know what currentIndexChanged does? It is emitted after the text changed... so you already get the new, updated index and text from it...
        In your function you store the current text and text at index in a string and display it...
        However at the time your function is called both are identical as the lineEdit was updated and the notifier signal was fired, which you catch and in turn call your function.

        You need to store/update this

        src_str = self.lineEdit().text()
        

        somewhere else and not inside the signal-connected function for your current "iteration". You can save it there for the next one.

        Something like:

            def _add_tag(self, index):
                tag_str = self.itemText(index)
                dist_str = self.old_str + ',' + tag_str
                ## this is bad as in C++ the lineEdit is const, might work in Python though
                self.lineEdit().setText(dist_str)
                ## update former selection for next text update
                self.old_str = self.itemText(index)
        
        C Offline
        C Offline
        crisp_toby
        wrote on 7 Sept 2024, 04:48 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • P Pl45m4
          7 Sept 2024, 03:21

          @crisp_toby said in How to append Item text in EditText in QComboBox:

          I tried to build a variable in TabComboBox to store current LineEdit text, and connect currentIndexChanged signal to my custom function_add_tag . But when I click item, if item text is Toby, current LineEdit text will change to Toby,Toby, it should be Bob,Toby.

          You want Bob, Toby but you get Toby, Toby, correct?!

          You know what currentIndexChanged does? It is emitted after the text changed... so you already get the new, updated index and text from it...
          In your function you store the current text and text at index in a string and display it...
          However at the time your function is called both are identical as the lineEdit was updated and the notifier signal was fired, which you catch and in turn call your function.

          You need to store/update this

          src_str = self.lineEdit().text()
          

          somewhere else and not inside the signal-connected function for your current "iteration". You can save it there for the next one.

          Something like:

              def _add_tag(self, index):
                  tag_str = self.itemText(index)
                  dist_str = self.old_str + ',' + tag_str
                  ## this is bad as in C++ the lineEdit is const, might work in Python though
                  self.lineEdit().setText(dist_str)
                  ## update former selection for next text update
                  self.old_str = self.itemText(index)
          
          C Offline
          C Offline
          crisp_toby
          wrote on 7 Sept 2024, 10:44 last edited by
          #4

          @Pl45m4 It works! But the last line self.old_str = self.itemText(index), it could be a mistake, self.old_str should updated by self.lineEdit().text().
          I test the code, found some new problem.
          As I modified, I use activated signal to connect _add_tag, and set_value function for outer delegate initiate data at the beginning, old_str updated in this function.
          As shown in picture, if I click one Item, item text append in LineEdit, then I delete that item text, and click other Item, deleted Item text appeared again.
          wrong-tag-combobox.drawio.png
          It should be old_str not be updated when edit LineText, but when I connect the editTextChanged signal to _update_old_str, Former problem like Toby,Toby happened again.
          here is the modified code.

          
          class TagComboBox(QComboBox):
              def __init__(self, tag_list, parent=None):
                  super(TagComboBox, self).__init__(parent)
                  self.setEditable(True)
                  self.setFrame(False)
                  self.addItems(tag_list)
                  self.old_str = ''
                  self.activated.connect(self._add_tag)
                  self.editTextChanged.connect(self._update_tag)
          
              def hidePopup(self):
                  popup_view = self.view()
                  if popup_view.rect().contains(popup_view.mapFromGlobal(QCursor().pos()))==False:
                      super().hidePopup()
          
              def _add_tag(self, index):
                  tag_str = self.itemText(index)
                  dist_str = self.old_str + ',' + tag_str
                  self.lineEdit().setText(dist_str)
                  self.old_str = self.lineEdit().text()
                  
              def _update_old_str(self):
                  self.old_str = self.lineEdit().text()
          
              def get_value(self):
                  return self.lineEdit().text()
          
              def set_value(self, value):
                  self.lineEdit().setText(value)
                  self.old_str = value
          
          P 1 Reply Last reply 7 Sept 2024, 13:44
          0
          • C crisp_toby
            7 Sept 2024, 10:44

            @Pl45m4 It works! But the last line self.old_str = self.itemText(index), it could be a mistake, self.old_str should updated by self.lineEdit().text().
            I test the code, found some new problem.
            As I modified, I use activated signal to connect _add_tag, and set_value function for outer delegate initiate data at the beginning, old_str updated in this function.
            As shown in picture, if I click one Item, item text append in LineEdit, then I delete that item text, and click other Item, deleted Item text appeared again.
            wrong-tag-combobox.drawio.png
            It should be old_str not be updated when edit LineText, but when I connect the editTextChanged signal to _update_old_str, Former problem like Toby,Toby happened again.
            here is the modified code.

            
            class TagComboBox(QComboBox):
                def __init__(self, tag_list, parent=None):
                    super(TagComboBox, self).__init__(parent)
                    self.setEditable(True)
                    self.setFrame(False)
                    self.addItems(tag_list)
                    self.old_str = ''
                    self.activated.connect(self._add_tag)
                    self.editTextChanged.connect(self._update_tag)
            
                def hidePopup(self):
                    popup_view = self.view()
                    if popup_view.rect().contains(popup_view.mapFromGlobal(QCursor().pos()))==False:
                        super().hidePopup()
            
                def _add_tag(self, index):
                    tag_str = self.itemText(index)
                    dist_str = self.old_str + ',' + tag_str
                    self.lineEdit().setText(dist_str)
                    self.old_str = self.lineEdit().text()
                    
                def _update_old_str(self):
                    self.old_str = self.lineEdit().text()
            
                def get_value(self):
                    return self.lineEdit().text()
            
                def set_value(self, value):
                    self.lineEdit().setText(value)
                    self.old_str = value
            
            P Offline
            P Offline
            Pl45m4
            wrote on 7 Sept 2024, 13:44 last edited by
            #5

            I wasn't sure if you want to show only the last and the new selection as a pair or if you want to append every new selected item continuously.

            @crisp_toby said in How to append Item text in EditText in QComboBox:

            It should be old_str not be updated when edit LineText, but when I connect the editTextChanged signal to _update_old_str, Former problem like Toby,Toby happened again.

            Really think about what you are doing and what is happening.
            The problem is easy to solve if you know when which string is updated to which value.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            C 1 Reply Last reply 8 Sept 2024, 00:57
            0
            • P Pl45m4
              7 Sept 2024, 13:44

              I wasn't sure if you want to show only the last and the new selection as a pair or if you want to append every new selected item continuously.

              @crisp_toby said in How to append Item text in EditText in QComboBox:

              It should be old_str not be updated when edit LineText, but when I connect the editTextChanged signal to _update_old_str, Former problem like Toby,Toby happened again.

              Really think about what you are doing and what is happening.
              The problem is easy to solve if you know when which string is updated to which value.

              C Offline
              C Offline
              crisp_toby
              wrote on 8 Sept 2024, 00:57 last edited by
              #6

              @Pl45m4 OK, I will study it further, Thank you for your help.

              1 Reply Last reply
              0
              • C crisp_toby has marked this topic as solved on 8 Sept 2024, 00:58

              5/6

              7 Sept 2024, 13:44

              • Login

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