0

I’ve got a QTableView based on a QSqlTableModel.

I want to filter displayed information: a user chooses one year in a combobox, and one or more perons in a multiple choices list. The QTableView data is filtered by these two widgets. My problem is that the data is correctly filtered on year, but only on the first personn selected. Other people's data are not displayed.

  • This is the code which creates the widget and the model, fills it, and filters it to display information in the QTableView :

    self.model = QtSql.QSqlTableModel(self, self.db)
    self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
    self.ui.tbv_suivtemp.setModel(self.model)
    self.model.setTable("bdsuivis.t_suivprev_tablo")
    self.model.select() 
    
    self.model.setHeaderData(22, QtCore.Qt.Horizontal, "Annee")
    self.model.setHeaderData(21, QtCore.Qt.Horizontal, "Salarie")
    
    annee = self.ui.cbx_channee.itemText(self.ui.cbx_channee.currentIndex())
    
    if len(annee) == 0 and len(self.text_sal) == 0:
        self.model.setFilter("")
    else:
        filtre = "annee = '%s' AND salaries IN ('%s')" % (annee, self.text_sal)
        print filtre
        self.model.setFilter(filtre)
    
  • This is the code to create self.text_sal:

    def choisal(self):
        list_sal = []
        for item in xrange (len(self.ui.lst_salaries.selectedItems())):
            salarie = self.ui.lst_salaries.selectedItems()[item].text().replace("\'","\'\'")
            list_sal.append(salarie)
        self.text_sal = ", ".join((str(x) for x in list_sal)).replace(",","\',\'")
    
  • print filtre gives :

    annee = '2014' AND salaries IN ('Dupont Albert','Durant Hector')

Which represents the WHERE clause from a SQL query.

My problem is: the year is correctly filtered, but only for "Albert Dupont" - the data for "Hector Durant" is not displayed. If, as a user, I click for the first time on "Hector Durand", in my multiple choices list, then on "Albert Dupont", the data for "Hector Durand" in 2014 is displayed.

Is this normal behaviour for a QSqlTableModel filter? Is there a way to obtain data for the two persons?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
V Damoy
  • 190
  • 10
  • 1
    I cannot reproduce this - the filter works exactly as expected. Please provide a [mcve]. – ekhumoro Dec 05 '17 at 18:30
  • I've begun to create a MCV example, and found the solution : when I created text_sal with a join method, I added a space after the comma : ", ".join... and not ",".join... Now, the filter works correctly. Sorry for the noise. – V Damoy Dec 07 '17 at 14:43

0 Answers0