我用資料庫中的專案填充組合框。當我嘗試添加新專案、擦除所有專案并再次添加它們時,如果正在更改 db,我會看到以下錯誤:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlQuery::value: not positioned on a valid record created
terminate called after throwing an instance of 'std::invalid_argument' what(): stoi
21:41:04: Debugging of C:\Users\79107\Downloads\build-food_calculator-Desktop_Qt_6_2_2_MinGW_64_bit-Debug\debug\food_calculator.exe has finished with exit code 3.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
MainWindow::foodListConstructor();//function, that fills the comboBox
}
void MainWindow::foodListConstructor()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("food_list.db");
db.open();
QSqlQuery query("SELECT food_name FROM food", db);
if(query.isActive())
{
while(query.next())
{
ui->comboBox->addItem(query.value(0).toString());
}
}
}
void MainWindow::on_action_3_triggered()
{
AddFood af(this);// in this new window a user writes what he wants to add
af.setModal(true);
af.exec();
this->ui->comboBox->clear();
this->ui->comboBox->addItem("test");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("food_list.db");
db.open();
QSqlQuery query1("SELECT food_name FROM food", db);
if(query1.isActive())
{
while(query1.next())
{
ui->comboBox->addItem(query1.value(0).toString());
}
}
如何使它作業而不是重復專案(如果我洗掉“this->ui->comboBox->clear();”,就會發生這種情況)?
uj5u.com熱心網友回復:
如果您foodListConstructor()多次呼叫,您將addDatabase()多次呼叫。根據檔案,這非常好:
使用驅動程式型別和連接名稱 connectionName 將資料庫添加到資料庫連接串列中。如果已經存在名為 connectionName 的資料庫連接,則洗掉該連接。
因此,您似乎只是看到 Qt 在內部輸出除錯訊息。這不是一個真正的錯誤,所以只需忽略它。盡管如此,一遍又一遍地添加相同的資料庫是沒有意義的。我建議只添加一次,然后QSqlDatabase::database()在需要時使用它來訪問它。
該QSqlQuery錯誤是不言自明的。當查詢不在活動記錄上時,您嘗試讀取欄位值。同樣,這看起來像是另一個內部除錯訊息,而不是真正的錯誤。根據檔案:
無效的
QVariant,如果現場索引不存在則回傳,如果查詢無效,或者如果查詢被定位在一個無效的記錄。
為什么QVariant從不活動的 SQL 記錄中得到無效的資訊?我不能說的。你必須自己弄清楚。
但它解釋了最終terminate訊息,這是一個真正的錯誤。您正在呼叫toString()一個無效的 QVariant:
在不受支持的變體上呼叫 QVariant::toString() 會回傳一個空字串。
在此程序中的某個地方,std::stoi()被一個不代表整數值的字串呼叫(即,可能是您添加到 的空字串comboBox),因此它拋出一個std::invalid_argument您沒有捕捉到的例外應用程式的 main 函式,導致 C 運行時呼叫std::terminate()以退出應用程式。
在哪里std::stoi()被呼叫?你需要搜索你的代碼。你有一些事件處理程式連接到comboBox?
話雖如此,請嘗試更像這樣的事情:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("food_list.db");
foodListConstructor();
}
void MainWindow::foodListConstructor()
{
ui->comboBox->clear();
QSqlDatabase db = QSqlDatabase::database("QSQLITE");
QSqlQuery query("SELECT food_name FROM food", db);
while (query.next())
{
QVariant v = query.value(0);
if (v.isValid())
ui->comboBox->addItem(v.toString());
}
}
void MainWindow::on_action_3_triggered()
{
AddFood af(this);
af.setModal(true);
af.exec();
foodListConstructor();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/384600.html
