I have a large dataset with 3 million rows and more than 5 columns, (increasing data day by day)
I want to get the row data by typing the text in Qcombobox(editable) with matching records highlited
What I have tried is:
from PyQt5.QtCore import QAbstractTableModel, QModelIndex, Qtfrom PyQt5.QtWidgets import (QApplication, QMainWindow, QComboBox, QAbstractItemView,QCompleter, QTableView, QLabel)import sysclass CustomCompleter(QCompleter): def pathFromIndex(self, index): sibling_index = index.sibling(index.row(), 0) return super().pathFromIndex(sibling_index)class TableModel(QAbstractTableModel): def __init__(self, data): super().__init__() self._data = data def rowCount(self, parent=QModelIndex()): return len(self._data) def columnCount(self, parent=QModelIndex()): return len(self._data[0]) def data(self, index, role=Qt.DisplayRole): if role == Qt.DisplayRole: return self._data[index.row()][index.column()] return Noneclass MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__() self.label = QLabel(parent = self) self.label.setGeometry(20,200, 250, 50) w, h = 600, 480 self.setMinimumSize(w, h) # using MySQLdb # cur = db() # q = 'select id, name_purchase, acchead, name_sale, quantity, edate, quality from dataTableB;' # cur.execute(q) # data_ = cur.fetchall() # sample data for minimal reproducable code self.data_ = ( (101, 'Robert James', 30, 'Robert Clark', '01-02-2024'), (102, 'Jack William', 25, 'Hazel Mile', '12-03-2024'), (103, 'Ivy Alexander', 30, 'Ellis', '01-04-2024'), (101, 'John Mile', 30, 'Lily', '21-04-2024'), (103, 'Edward George', 30, 'Arthur', '25-04-2024') ) model = TableModel(self.data_) self.combo = QComboBox(self) self.combo.setGeometry(50,50, 250, 40) self.combo.setEditable(True) self.combo.setModel(model) combo_view = QTableView(self.combo, selectionBehavior=QAbstractItemView.SelectRows) combo_view.verticalHeader().setVisible(False) self.combo.setView(combo_view) self.combo.currentIndexChanged.connect(self.get_row) completer_view = QTableView(self.combo, selectionBehavior=QAbstractItemView.SelectRows) completer_view.verticalHeader().hide() completer = CustomCompleter() completer.setCaseSensitivity(False) completer.setFilterMode(Qt.MatchContains) completer.setModel(model) completer.setCompletionColumn(0) completer.setPopup(completer_view) # completer.setModelSorting(QCompleter.CaseSensitivelySortedModel) # completer.setCompletionMode(QCompleter.PopupCompletion) # completer.popup().setMinimumWidth(w) # completer.popup().setMinimumHeight(h) self.combo.setCompleter(completer) def get_row(self, n): self.label.setText(str(n)) v_id = self.data_[n][0] self.combo.setCurrentText(str(v_id))if __name__ == '__main__': app = QApplication(sys.argv) main = MainWindow() main.show() try: sys.exit(app.exec_()) except SystemExit: NoneWith this codeQcombobox show the data in tabular format but Qcompleter does not show any data (when typing in Qcombobox, no popup appears)
I want the Qcompleter to match the text from all columns and highlight the record.
The sample result image is here:Click me to see The Required Result