【问题标题】:QSortFilterProxyModel limits number of records when filter is applied应用过滤器时 QSortFilterProxyModel 限制记录数
【发布时间】:2014-03-02 00:44:09
【问题描述】:

QSortFilterProxyModel 似乎限制了可以在 QTableView 中显示的数据量,但仅限于应用过滤器时。似乎限制是数据大小而不是记录数,因为与我看到行为的实际应用程序相比,我可以在此处的示例中放入更多记录。

要复制,请按照以下步骤操作(在 Linux 中,安装 Sqlite)。代码如下。

  1. 使用提供的代码创建一个新项目。
  2. 在包含可执行文件的目录中,键入:sqlite3 testInsert.db
  3. 在 sqlite 提示符下,输入:create table testTable(id integer primary key autoincrement, name text);
  4. 运行应用程序并看到记录 5000 testString 出现在表视图中。
  5. 将 numRecords 变量增加到 511。(也许您的具体数字会有所不同??)
  6. 在 sqlite 提示符中,输入:drop table testTable
  7. 重做第 3 步
  8. 运行看看这次记录5000有没有没有出现
  9. 重做第 6 步(删除表),然后重做第 3 步(创建表)。可能有一种更简单的方法来清理桌子,但这对我有用。
  10. 在代码的底部附近,有一个对模型的 SetFilterRegExp 方法的调用。注释掉那一行。
  11. 再次运行。在结果中,向下滚动到底部并看到记录 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 的机器上复制。我还注意到,在我提供的示例中,在应该显示单个记录但没有显示的情况下,我实际上可以通过调整主窗口的大小来显示它。在我第一次发现此问题的实际应用程序中,调整大小不会导致出现丢失的记录。

【问题讨论】:

    标签: c++ qt sqlite


    【解决方案1】:

    我能够重现该问题。我可以让行显示的唯一方法是添加行:

    proxyModel->invalidate();
    

    在调用tableView.show() 之后。然而,据我所知,这不应该是必要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2019-11-20
      • 2020-05-13
      • 2012-10-10
      • 1970-01-01
      • 2016-09-11
      • 2010-10-27
      相关资源
      最近更新 更多