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 searching/filtering the given text in QLineEdit with matching records highlited
But I could not do the task in QLineEdit that’s why I tried Qcombobox
What I have tried is:
from PyQt5.QtCore import QAbstractTableModel, QModelIndex, Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QComboBox, QAbstractItemView,
QCompleter, QTableView, QLabel)
import sys
class 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 None
class 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
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(data_)
combo = QComboBox(self)
combo.setGeometry(50,50, 250, 40)
combo.setEditable(True)
combo.setModel(model)
combo_view = QTableView(combo, selectionBehavior=QAbstractItemView.SelectRows)
combo_view.verticalHeader().setVisible(False)
combo.setView(combo_view)
combo.currentIndexChanged.connect(self.get_row)
completer_view = QTableView(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)
combo.setCompleter(completer)
def get_row(self, n):
self.label.setText(str(n))
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.show()
try:
sys.exit(app.exec_())
except SystemExit:
None
With this code
- Qcombobox show the data in tabular format and when i select a row with mouse, it returns the row number but does not fill the data in qcombobox (qcombobox remains empty)
- Qcompleter does not show any data (when typing in Qcombobox, no popup appears)
I want the Qcompleter match the text from all columns and highlight the record, and selecting the required row Qcombobox must be filled with the 1st column record e.g 101
If this could be done in QLineEdit (That would be actually helpful) because Qcombobox could be slow when the dataset has more than 3 million rows
The sample result image is as below:
enter image description here
Aqsa Farm House is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.