获取 id 字段作为 QSqlTableModel 中记录的整数



>我正在使用QSqlTableModel *modelcompleter,然后使用QCompleter将数据加载到qLineEdit *search

    connect(ui->btnSearch, SIGNAL(clicked()), SLOT(search()));
    modelcompleter->setTable("cust");
    modelcompleter->setEditStrategy(QSqlTableModel::OnManualSubmit);
    modelcompleter->select();
    QCompleter *searchCompleter = new QCompleter(modelcompleter);
    searchCompleter->setCompletionColumn(1);
    searchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    ui->search->setCompleter(searchCompleter);

我正在使用qlineEdit在我想选择的项目上按回车键:

 connect(ui->search, SIGNAL(returnPressed()), SLOT(search()));

函数

void search(){
cust_name = ui->search->text();
    modelsearch->setTable("cust");
    modelsearch->setEditStrategy(QSqlTableModel::OnManualSubmit);
    modelsearch->setFilter(QString("cust_name like '%%1%'").arg(cust_name));
    modelsearch->select();
    qDebug() << modelsearch->record(0).value("id").toInt(); }

我不明白为什么,但是模型搜索目前设置为我按回车键时仅获得 1 条记录(QCompleter 确保我提交完整的"cust_name")。QDebug() 输出 0。有没有其他方法可以获取与搜索查询'cust_name'匹配的'cust_name' id

qDebug() << modelsearch->record(0).value("id").toInt();

取而代之的是这个。我用了

QSqlQueryModel *query = new QSqlQueryModel;
query->setQuery(QString("SELECT id from cust WHERE cust_name like '%%1%'").arg(cust_name));
qDebug() << query->record(0).value("id").toInt();

它现在正在获取记录的 ID!

相关内容

  • 没有找到相关文章

最新更新