QSortFilterProxyModel 似乎限制了 QTableView 中可以显示的数据量,但仅限于应用过滤器时。看起来限制是数据大小而不是记录数量,因为我可以在此处的示例中放入比我看到行为的实际应用程序更多的记录。
要复制,请按照以下步骤操作(在 Linux 中,安装了 Sqlite)。下面提供了代码。
- 使用提供的代码创建一个新项目。
- 在包含可执行文件的目录中,键入:sqlite3 testInsert.db
- 在 sqlite 提示符中,键入:create table testTable(id integer Primary key autoincrement, name text);
- 运行应用程序并查看记录 5000 testString 显示在 TableView 中。
- 将 numRecords 变量增加到 511。(也许您的具体数字会有所不同??)
- 在 sqlite 提示符中,键入:drop table testTable
- 重做第 3 步
- 运行看看,这次记录5000没有出现
- 重做步骤 6(删除表),然后重做步骤 3(创建表)。也许有一种更简单的方法来清理 table ,但这对我有用。
- 在代码底部附近,调用了模型的 SetFilterRegExp 方法。将该行注释掉。
- 再次运行。在结果中,向下滚动到底部,您会看到记录 5000 确实出现了。
示例代码:
#include <QApplication>
#include <QDebug>
#include <QTableView>
#include <QtSql>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./testInsert.db");
db.open();
QSqlRelationalTableModel *model = new QSqlRelationalTableModel(NULL, db);
model->setTable("testTable");
// put a bunch of dummy records into the database
int numRecords = 510;
for(int i=0; i<numRecords; i++)
{
QSqlRecord newRecord;
QSqlField name("NAME", QVariant::String);
name.setValue("unmatchedString");
newRecord.append(name);
model->insertRecord(-1, newRecord);
}
// put in the record we want to see
QSqlRecord record;
QSqlField id("ID", QVariant::Int);
QSqlField name("NAME", QVariant::String);
id.setValue(5000);
name.setValue("testString");
record.append(id);
record.append(name);
model->insertRecord(-1, record);
model->select();
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
proxyModel->setSourceModel(model);
proxyModel->setFilterKeyColumn(1);
QRegExp filterRegExp = QRegExp("^testString$");
proxyModel->setFilterRegExp(filterRegExp); // <<-- this is the line to comment out
QTableView tableView;
tableView.setModel(proxyModel);
tableView.show();
return a.exec();
}
更新:也在另一台运行 Windows 的计算机上复制。我还注意到,在我提供的示例中,在应该显示单个记录但没有显示的情况下,我实际上可以通过调整主窗口的大小来使其显示。在我第一次发现此问题的实际应用程序中,调整大小不会导致出现丢失的记录。
最佳答案
我能够重现该问题。我可以显示该行的唯一方法是添加以下行:
proxyModel->invalidate();
调用tableView.show()
之后。然而,据我所知,这应该是没有必要的。
关于c++ - QSortFilterProxyModel 在应用过滤器时限制记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027654/