2
QSortFilterProxyModel似乎限制了可以在QTableView中显示的数据量,但仅限于应用过滤器时。似乎限制是数据大小而不是记录数量,因为我可以在这里的示例中填充更多的记录,而不是我看到行为的实际应用程序。QSortFilterProxyModel限制应用过滤器时的记录数
重复以下步骤(在Linux中,安装Sqlite)。代码如下。
- 使用提供的代码创建一个新项目。
- 在与可执行,类型的目录:sqlite3的testInsert.db
- 在源码提示,类型:创建表TestTable的(ID整数主键自动增量,名称文本);
- 运行该应用程序并查看该记录5000,testString,出现在表视图中。
- 递增的变量numRecords〜511(也许你的具体数目将变化??)
- 在源码提示,类型:DROP TABLE TestTable的
- 重做步骤3
- 运行和看到,这一次记录5000确实不是出现
- 重做第6步(放置表),然后第3步(创建表)。可能有一种更简单的方法来清理桌子,但这对我来说很有用。
- 靠近代码底部,有一个对模型的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另一台机器上。我还注意到,在我提供的例子中,在单个记录应该显示但不是的情况下,我实际上可以通过调整主窗口的大小来显示它。在我第一次发现此问题的实际应用程序中,调整大小不会导致缺少的记录出现。