C++ 从表视图中选择行时,将int值指定给组合框

C++ 从表视图中选择行时,将int值指定给组合框,c++,qt,combobox,tableview,C++,Qt,Combobox,Tableview,单击表视图中的某一行后,我希望将特定的材质Id指定给组合框 void FictionSection::on_tblFiction_clicked(const QModelIndex &index) { int indicator = ui->tblFiction->model()->data(index).toInt(); QSqlQuery query; query.prepare(" select fic_bk_id,material_id,

单击表视图中的某一行后,我希望将特定的材质Id指定给组合框

void FictionSection::on_tblFiction_clicked(const QModelIndex &index)
{
   int indicator = ui->tblFiction->model()->data(index).toInt();

    QSqlQuery query;
    query.prepare(" select fic_bk_id,material_id,no_of_cpy,shelf_num,edition from fiction "
                  "where material_id= :id or no_of_cpy=:copy or shelf_num=:shelf or edition=:edition");
    query.bindValue(":id",indicator);
    query.bindValue(":copy",indicator);
    query.bindValue(":shelf",indicator);
    query.bindValue(":edition",indicator);

    //Assigning table values to fields
    if(query.exec())
    {
        while(query.next())
        {
            ui->lneditMngFicId->setText(query.value(0).toString());
             ui->cmboxMngId->setCurrentText(query.value(1).toString());

            ui->lneditMngCpy->setText(query.value(2).toString());
            ui->spinbxMngShlf->setValue(query.value(3).toInt());
            ui->spinbxMngEdtn->setValue(query.value(4).toInt());

        }

    }
    else
    {
         QMessageBox :: critical(this,"Error","Couldn't load the values");
    }
}

尽管其他字段在我单击任何行组合框后都会得到值,但不会得到所需的值。物料id作为int存储在数据库中。为了解决这个问题,我做了很多努力。请帮我解决这个问题。提前感谢

是因为您的查询中有与物料id相关的空格:

material_id= :id

您是否验证了查询是否达到了预期效果ie是否确实在material_id中获得了有效的int?其他需要验证的Qt内容(遵循QComboBox::currentText的文档):“如果组合框可编辑,则当前文本为行编辑显示的值。否则,如果组合框为空或未设置当前项,则为当前项的值或空字符串。”该框是否可编辑?如果没有,那么我认为您需要先填充它,然后再填充setCurrentIndex(int-index)