Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
QT+C++中QLayes编辑与组合框的链接_C++_Qt_Combobox_Qlineedit - Fatal编程技术网

QT+C++中QLayes编辑与组合框的链接

QT+C++中QLayes编辑与组合框的链接,c++,qt,combobox,qlineedit,C++,Qt,Combobox,Qlineedit,当我从组合框中选择图书Id时,我想将图书名称的值加载到文本框中。首先,我编写了从数据库将图书ID加载到comboBox的代码,它加载得非常完美。这就是方法 void FictionSection::loadComboVal() { DatabaseConnection mydb; QSqlQueryModel *modl = new QSqlQueryModel(); dbConOpen(); QSqlQuery *query = new QSqlQuery(myd

当我从组合框中选择图书Id时,我想将图书名称的值加载到文本框中。首先,我编写了从数据库将图书ID加载到comboBox的代码,它加载得非常完美。这就是方法

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();
}
然后,我编写了代码,用于在从组合框中选择图书Id后将值分配给textBox。这是代码片段:

// 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();

}
但随后加载到comboBox的值是不可见的,我无法像预期的那样将任何值加载到textbox。但没有编译或运行时错误。在数据库中,图书Id存储为int,图书名称存储为varchar

我将代码更改如下:

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();

}
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
然后给出如下错误:

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();

}
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
我编辑了上面的代码,但仍然无法得到预期的结果

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();
}

请帮我解决这个问题。提前感谢您

尝试使用QSqlQuery::next方法在有效记录上定位。

缺少值的右括号。。如果旗子!=您应该检查数据库错误。我还记得在向数据库添加值时使用值。。选择时不需要。但是,如果这个查询对您有效,那么就可以了。调试出最终的查询是个好主意。我不知道如何正确地关闭它。当它关闭时,会出现一个错误啊,好的,你看。问题从较低的层次开始。检查数据库上的每个操作是否存在错误。。包括“打开”和“关闭”以归结为错误。不,这并不是说未定位在有效记录中。但它显示了重复的数据库等,甚至没有显示组合框的值。我后来更改了代码,并编辑了上面的问题。您可以检查一下吗?您应该使用QSqlQuery::value方法的索引0。在您的情况下,还可以使用query.valuematerial\u title.toString;。它也不起作用。qlineEdit中没有显示任何内容