是否可以使用带有搜索名称的 QRegExp 查找索引行和值?

Is it posible to find index-row and values with QRegExp with search name?

我想使用 QSortFilterProxyModelQtCore.QRegExp 查找带有搜索词的行索引。 我想创建一个仅包含从 QRegExp 变量中找到的行的“移动”和“电子邮件”列的列表。 下面是示例代码:

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 = []

我想打印 row.index 并将该行的“mobile”和“email”的值插入 list_a。 怎么可能或者有没有其他的请求解决方案?

没有必要创建模型来基于正则表达式过滤元素,因为 Qt sqlite 驱动程序允许使用 regex 函数通过 QSQLITE_ENABLE_REGEXP 使用 setConnectOptions() 方法启用它:

import sys

from PyQt5 import QtSql

db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("users.db")
db.setConnectOptions("QSQLITE_ENABLE_REGEXP=1")

if not db.open():
    sys.exit(-1)

query = QtSql.QSqlQuery()
query.exec_(
    """CREATE TABLE IF NOT EXISTS user_name(name TEXT, age TEXT, mobile TEXT, mail TEXT)"""
)

query.exec_("INSERT into user_name VALUES ('name1', 29, 123, 'Mail1.com')")
query.exec_("INSERT into user_name VALUES ('name2', 30, 456, 'Mail2.com')")
query.exec_("INSERT into user_name VALUES ('name3', 31, 789, 'Mail3.com')")

query_filter = QtSql.QSqlQuery()
query_filter.prepare("SELECT rowid, * FROM user_name WHERE regexp(?, name) ")
query_filter.addBindValue("name1")

if query_filter.exec_():
    record = query_filter.record()
    while query_filter.next():
        print("=======")
        for i in range(record.count()):
            print(record.fieldName(i), query_filter.value(i))
else:
    print(query_filter.lastError().text())

如果你仍然想使用 QSqlTableModel 那么你也可以使用 setFilter 方法:

import sys

from PyQt5 import QtSql

db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("users.db")
db.setConnectOptions("QSQLITE_ENABLE_REGEXP=1")

if not db.open():
    sys.exit(-1)

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("user_name")
model.select()

query.exec_("INSERT into user_name VALUES ('name1', 29, 123, 'Mail1.com')")
query.exec_("INSERT into user_name VALUES ('name2', 30, 456, 'Mail2.com')")
query.exec_("INSERT into user_name VALUES ('name3', 31, 789, 'Mail3.com')")

model.setFilter("regexp('%s', name)" % ("name1"))

model.select()
for i in range(model.rowCount()):
    r = model.record(i)
    print("=====")
    for j in range(r.count()):
        print(r.fieldName(j), r.value(j))

如果您仍想使用 QSqlTableModel + QSQSortFilterProxyModel,那么除了过滤器之外,您还必须映射行的位置:

import sys

from PyQt5 import QtCore, QtSql

db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("users.db")

if not db.open():
    sys.exit(-1)

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("user_name")
model.select()

proxy = QtCore.QSortFilterProxyModel()
proxy.setSourceModel(model)

query.exec_("INSERT into user_name VALUES ('name1', 29, 123, 'Mail1.com')")
query.exec_("INSERT into user_name VALUES ('name2', 30, 456, 'Mail2.com')")
query.exec_("INSERT into user_name VALUES ('name3', 31, 789, 'Mail3.com')")

model.select()

search_name = "name2"

search = QtCore.QRegExp(search_name)
proxy.setFilterRegExp(search)

for i in range(proxy.rowCount()):
    r = model.record()
    print("=====")
    print("row", proxy.mapToSource(proxy.index(i, 0)).row())
    for j in range(r.count()):
        index = proxy.index(i, j)
        print(r.fieldName(j), index.data())