I want to find the row-index with searching word using with QSortFilterProxyModel
and QtCore.QRegExp
.
I want to create a list with "mobile" and "email" columns only from the row which is finding from the variable of QRegExp
.
Below is example code:
from PyQt5 import QtCore, QtSql
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("users.db")
if db.open():
query = QtSql.QSqlQuery()
query.exec_("""CREATE TABLE IF NOT EXISTS user_name(name TEXT, age TEXT, mobile TEXT, mail TEXT)""")
model = QtSql.QSqlTableModel()
model.setTable("table")
model.select()
proxy = QtCore.QSortFilterProxyModel()
proxy.setSourceModel(model)
query.exec_("INSERT into users VALUES ('name1', 29, 123, 'Mail1.com')")
query.exec_("INSERT into users VALUES ('name2', 30, 456, 'Mail2.com')")
query.exec_("INSERT into users VALUES ('name3', 31, 789, 'Mail3.com')")
search_name = "name2"
search = QtCore.QRegExp(search_name)
proxy.setFilterRegExp(search)
list_a = []
I want to print the row.index
and insert the value of "mobile" and "email" of the row into list_a
.
How is it possible or is there any other solution for Request?
It is not necessary to create a model to filter elements based on a regex since the Qt sqlite driver allows to use the
regex
function enabling it throughQSQLITE_ENABLE_REGEXP
usingsetConnectOptions()
method:If you still want to use QSqlTableModel then you can also use the setFilter method:
If you still want to use QSqlTableModel + QSQSortFilterProxyModel then apart from the filter you have to map the position of the rows: