Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCompleter change value in all ComboBoxs



  • Hi,

    i'm trying to set Qcompleter and seems work fine except for this problem. I have a lot of ComboBox and when i edit one of them using Qcompleter fuction the program change all value in the other comboboxs. Why?

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets as qtw
    from PyQt5 import QtGui as qtg
    from PyQt5 import QtCore as qtc
    from PyQt5.QtWidgets import QCompleter, QTableWidget, QTableWidgetItem
    from PyQt5.QtGui import QFont, QStandardItem, QStandardItemModel, QFont
    import csv
    
    class MainWindow(qtw.QWidget):
    
       def __init__(self):
           super().__init__()
    
           groupbox_colazione = qtw.QGroupBox('COLAZIONE')
           groupbox_colazione.setMaximumWidth(400)
           groupbox_colazione_layout = qtw.QGridLayout()    
    
           with open ('prova_lavorazione.csv') as file:
               
               lista_alimenti = csv.reader(file, delimiter=';')
    
               completamento = []
    
               for row in lista_alimenti:
           
                   completamento.append(row[1])
    
               
           completer = qtw.QCompleter(completamento)
    
           self.elenco_alimenti_colazione = []
           self.grammi_colazione = []
    
           for i in range (18):
    
              elenco_alimenti = qtw.QComboBox()
              elenco_alimenti.setMaximumWidth(200) 
              elenco_alimenti.setEditable(True)
    
              self.elenco_alimenti_colazione.append(elenco_alimenti) 
    
              grammi_colazione = qtw.QLineEdit(placeholderText='Grammi..')   
              grammi_colazione.setMaximumWidth(50)  
    
              self.grammi_colazione.append(grammi_colazione)   
    
           with open ('prova_lavorazione.csv') as file:
               
               lista_alimenti = csv.reader(file, delimiter=';')
    
               completamento = []
    
               for i in range (18):
    
                   self.elenco_alimenti_colazione[i].addItem('')
    
               for row in lista_alimenti:
    
                   for i in range (18):
    
                       self.elenco_alimenti_colazione[i].addItem(row[1])
                       self.elenco_alimenti_colazione[i].setCompleter(completer)
    
           groupbox_colazione = qtw.QGroupBox('COLAZIONE')
           groupbox_colazione.setMaximumWidth(900)
           groupbox_colazione_layout = qtw.QGridLayout()
           groupbox_colazione.setLayout(groupbox_colazione_layout)
    
           for i in range(6):
    
               groupbox_colazione_layout.addWidget(self.elenco_alimenti_colazione[i], i, 0)
    
           for i in range(6):
               
               groupbox_colazione_layout.addWidget(self.grammi_colazione[i], i, 1)
    
           y = 0
    
           for i in range(6,12):
    
               groupbox_colazione_layout.addWidget(self.elenco_alimenti_colazione[i], y, 2)
               y += 1
    
           y = 0
    
           for i in range(6,12):
               
               groupbox_colazione_layout.addWidget(self.grammi_colazione[i], y, 3)
               y += 1
    
           y = 0
    
           for i in range(12,18):
    
               groupbox_colazione_layout.addWidget(self.elenco_alimenti_colazione[i], y, 4)
               y += 1
    
           y = 0
    
           for i in range(12,18):
               
               groupbox_colazione_layout.addWidget(self.grammi_colazione[i], y, 5)
               y += 1
    
       
           main_layout = qtw.QGridLayout()
           main_layout.addWidget(groupbox_colazione, 0, 0) 
    
           self.setLayout(main_layout)   
    
           self.show()
    
    if __name__ == '__main__':
    
       app = qtw.QApplication(sys.argv)
       mw = MainWindow()
       sys.exit(app.exec())
    

  • Lifetime Qt Champion

    Hi,

    Please use the coding tags for your code, it's almost unreadable.

    Also, please make it a minimal runnable script so it can be more easily tested.



  • Thanks, done ! :p


  • Lifetime Qt Champion

    @Falassion
    Hi
    it seem you are sharing the completer amount the combo boxes ?



  • @mrjj said in QCompleter change value in all ComboBoxs:

    @Falassion
    Hi
    it seem you are sharing the completer amount the combo boxes ?

    Do you mean i have to use different completer? How i can do?

    I need comboboxes with the same items list and a completer for each one.


  • Lifetime Qt Champion

    @Falassion

    You just need to do
    completer = qtw.QCompleter(completamento)
    for each

    self.elenco_alimenti_colazione[i].setCompleter(completer)

    They can use same list (completamento)

    Im 100% python noob but might be like

     for i in range (18):
          self.elenco_alimenti_colazione[i].addItem(row[1])
          self.elenco_alimenti_colazione[i].setCompleter( qtw.QCompleter(completamento) )
    
    

    so each gets its own instance (but same list )



  • @mrjj said in QCompleter change value in all ComboBoxs:

    @Falassion

    You just need to do
    completer = qtw.QCompleter(completamento)
    for each

    self.elenco_alimenti_colazione[i].setCompleter(completer)

    They can use same list (completamento)

    Im 100% python noob but might be like

     for i in range (18):
          self.elenco_alimenti_colazione[i].addItem(row[1])
          self.elenco_alimenti_colazione[i].setCompleter( qtw.QCompleter(completamento) )
    
    

    so each gets its own instance (but same list )

    Thanks ! It work! :D


Log in to reply