Linking Qline edit with combo box in qt c++

浪子不回头ぞ 提交于 2019-12-12 05:27:22

问题


I want to load the value of book name to a text box when I select the book Id from the comboBox. First I wrote the code for loading Book Ids to comboBox from database and it loaded perfectly. This is the method.

void FictionSection::loadComboVal()
{
    DatabaseConnection mydb;
    QSqlQueryModel *modl = new QSqlQueryModel();
  dbConOpen();

    QSqlQuery *query = new QSqlQuery(mydb.db) ;
    query->prepare(" select material_id from fiction ");
  bool flag = query->exec();

  //assigning the values to a QTableView
  if(flag == true)
  {
      modl->setQuery(*query);
      ui->cmbxId->setModel(modl);

}
  mydb.dbConClose();
}

Then I wrote the code for assigning values to textBox once the book Id is selected from the comboBox. This is the code snippet:

// loading book names once user select the book id from combo box
void FictionSection::on_cmbxId_currentIndexChanged(int index)
{
    QString value = ui->cmbxId->currentText();
    int id;
    if(!value.isEmpty())
    {
     id = value.toInt();
    }
//Loading book table values to a table

dbC    onOpen();
       QSqlQuery query  ;
       QString ids = QString("values('") + QString::number(id);
       query.prepare(" select material_title from book where material_id = '"+ids+"'");
     bool flag = query.exec();

     //assigning the values to a QTableView
     if(flag == true)
     {
        ui->lneditFicNme->setText(query.value(1).toString());


     }
    dbConClose();

}

But then loaded values to comboBox are invisible and i can't get any value to textbox as I expected. But no compilation or run-time errors. Book Id is stored as an int and book name as varchar in database.

I changed the code as follows:

void FictionSection::on_cmbxId_currentIndexChanged(int index)
{
    QString value = ui->cmbxId->currentText();
    int id;
    if(!value.isEmpty())
    {
     id = value.toInt();
    }
//Loading book table values to a table

       dbConOpen();
       QSqlQuery query  ;
      // QString ids = QString("values('") + QString::number(id);
       query.prepare(" select material_title from book where material_id = ?");
       query.bindValue(0,id);
     bool flag = query.exec();

     //assigning the values to a QTableView
     if(flag == true)
     {
        ui->lneditFicNme->setText(query.value(1).toString());


     }
     else
     {

         QMessageBox :: critical(this,"Error",query.lastError().text());
     }
    dbConClose();

}

Then it gives an error as follows:

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlQuery::value: not positioned on a valid record
QSqlQuery::value: not positioned on a valid record

I edited the above code but still I couldn't get the expected outcome.

void FictionSection::on_cmbxId_currentIndexChanged(int index)
{
    DatabaseConnection con;
     con.dbConOpen();


    QString value = ui->cmbxId->currentText();
    int id;
    if(!value.isEmpty())
    {
     id = value.toInt();
    }
//Loading book table values to a table


       QSqlQuery query  ;

      // QString ids = QString("values('") + QString::number(id);
       query.prepare(" select material_title from book where material_id = :id");
       query.bindValue(":id",id);
     bool flag = query.exec();

     //assigning the values to a QTableView
     if(flag == true)
     {

          while(query.next())
          {

             ui->lneditFicNme->setText(query.value(1).toString());

          }
     }
     else
     {

         QMessageBox :: critical(this,"Error",query.lastError().text());
     }
    con.dbConClose();
}

Please help me to solve this. Thank you in advance


回答1:


Try to use QSqlQuery::next() method to position on valid record.



来源:https://stackoverflow.com/questions/34129395/linking-qline-edit-with-combo-box-in-qt-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!