Reputation: 87
I am not expert in PyQt5, may be i should say i am too weak and a totally newbie in PyQt5 So I need help. I am sharing the minimal working code to get help in regards to PyQt5 QCompleter.
The Custom QCompleter given in my code matches the QlineEdit's text from the given column number e.g column number 0 or 1, 2, 3 or so on... which i have to mentioned through the app.
I actually want QCompleter to match records across all columns of QAbstract Table Model so the limitation of column number will not be required any more.
Here is the code:
from PyQt5.QtWidgets import QApplication, QCompleter,
QAbstractItemView, QTableView, QMainWindow, QLabel, QLineEdit
from PyQt5.QtCore import QModelIndex, QAbstractTableModel, Qt
import sys, datetime
class CustomCompleter(QCompleter):
def __init__(self, model, label, view, obj, col=0):
super().__init__()
self.label = label
w, h = 600, 480
# self.model = model
self.setCaseSensitivity(False)
self.setFilterMode(Qt.MatchContains)
self.setModel(model)
self.setCompletionRole(Qt.DisplayRole)
self.setPopup(view)
self.setModelSorting(QCompleter.CaseSensitivelySortedModel)
# self.setCompletionMode(QCompleter.PopupCompletion)
self.popup().setMinimumWidth(w)
self.popup().setMinimumHeight(h)
self.setCompletionColumn(col)
self.setMaxVisibleItems(15)
obj.setCompleter(self)
def pathFromIndex(self, index):
index = self.model().index(index.row(), 0, QModelIndex())
self.index = index.row()
self.label.setNum(self.index)
sibling_index = index.sibling(index.row(), 0)
return super().pathFromIndex(sibling_index)
class TableModel(QAbstractTableModel):
def __init__(self, data, cols):
super().__init__()
self._data = data
self.columns = cols
def rowCount(self, parent=QModelIndex()):
return len(self._data)
def columnCount(self, parent=QModelIndex()):
return len(self._data[0]) if self._data else 0
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self._data[index.row()][index.column()]
return None
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.columns[section]
# return 'Column {}'.format(section + 1)
return super().headerData(section, orientation, role)
class TableView(QTableView):
def __init__(self, obj, model):
super().__init__()
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.verticalHeader().hide()
self.setModel(model)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.label_return = QLabel(parent = self)
self.label_return.setGeometry(20,200, 250, 50)
w, h = 900, 480
self.setMinimumSize(w, h)
self.lineEdit = QLineEdit(self)
self.lineEdit.setGeometry(50,80, 250, 40)
self.lineEdit.returnPressed.connect(self.get_row)
self._data = ((1975, 25000.0, datetime.date(2025, 1, 16), 3, 2, 200, 160.0, 1.0, 0.0, 3960000, 'Hello How are you', 'Sale', 19, 1),
(2025, 3000.0, datetime.date(2025, 1, 16), 3, 2, 300, 200.0, 1.0, 0.0, 594000, '', 'Sale', 20, 1))
column_names = ['Bill', 'Mtr', 'Date', 'Pur', 'Sale', 'Than', 'Rate', 'Disc', 'Dis', 'Amt', 'Dtl', 'Type', 'Days', 'QID']
self.model = TableModel(self._data, column_names)
self.completer_view = TableView(self.lineEdit, self.model)
column_number = 0 # This is what i don't want the limitation of column number. I want to match the QlineEdit's Text from all columns at once and return the rows containing matching record from any of given columns
CustomCompleter(self.model, self.label_return, self.completer_view, self.lineEdit, column_number)
def get_row(label, data):
index = label.text()
if index:
v_id = data[int(index)][0]
else:
label.clear()
def clear_search(self):
self.label.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.show()
try:
sys.exit(app.exec_())
except SystemExit:
None
When i type something in QlineEdit, the Qcompleter pops up a table showing all rows who have the matching records in 1st column of the QAbstractTableModel. If i change the variable named column_number from 0 to 1 or 2, then the Qcompleter show matching records from the given column number.
Now i want qlineEdit's text to get searched from all columns of QAbstractTablemodel instead searching it any specific given column.
Please someone guide me to sort the issue.
Upvotes: 0
Views: 31