如何在更改数据库时更新组合框项目?
How to update comboBox items when the database is being changed?
我用数据库中的项目填充组合框。当我尝试添加新项目、删除所有项目并再次添加它们时,如果正在更改数据库,我会看到这些错误:
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:\Users107\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();”)?
如果您多次调用 foodListConstructor()
,您将多次调用 addDatabase()
。根据 documentation:
这完全没问题
Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.
所以看起来您只是看到了 Qt 内部输出的调试消息。这不是真正的错误,因此请忽略它。虽然,一遍又一遍地添加相同的数据库没有意义。我建议只添加一次,然后在需要时使用 QSqlDatabase::database()
访问它。
QSqlQuery
错误是不言自明的。当查询不在活动记录上时,您尝试读取字段值。同样,这看起来像是另一条内部调试消息,而不是真正的错误。根据 documentation:
An invalid QVariant
is returned if field index does not exist, if the query is inactive, or if the query is positioned on an invalid record.
为什么您从不活动的 SQL 记录中得到无效的 QVariant
?我不能说。你得自己想办法。
但它解释了最后的 terminate
消息,这是一个真正的错误。您在 无效 QVariant
:
上调用 toString()
Calling QVariant::toString() on an unsupported variant returns an empty string.
在某个地方,std::stoi()
被调用时使用了一个不代表整数值的字符串(即,可能是您要添加到 comboBox
的空字符串),所以它抛出您未捕获的 std::invalid_argument
异常,该异常从您的应用程序的主要功能中转义,导致 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();
}
我用数据库中的项目填充组合框。当我尝试添加新项目、删除所有项目并再次添加它们时,如果正在更改数据库,我会看到这些错误:
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:\Users107\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();”)?
如果您多次调用 foodListConstructor()
,您将多次调用 addDatabase()
。根据 documentation:
Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.
所以看起来您只是看到了 Qt 内部输出的调试消息。这不是真正的错误,因此请忽略它。虽然,一遍又一遍地添加相同的数据库没有意义。我建议只添加一次,然后在需要时使用 QSqlDatabase::database()
访问它。
QSqlQuery
错误是不言自明的。当查询不在活动记录上时,您尝试读取字段值。同样,这看起来像是另一条内部调试消息,而不是真正的错误。根据 documentation:
An invalid
QVariant
is returned if field index does not exist, if the query is inactive, or if the query is positioned on an invalid record.
为什么您从不活动的 SQL 记录中得到无效的 QVariant
?我不能说。你得自己想办法。
但它解释了最后的 terminate
消息,这是一个真正的错误。您在 无效 QVariant
:
toString()
Calling QVariant::toString() on an unsupported variant returns an empty string.
在某个地方,std::stoi()
被调用时使用了一个不代表整数值的字符串(即,可能是您要添加到 comboBox
的空字符串),所以它抛出您未捕获的 std::invalid_argument
异常,该异常从您的应用程序的主要功能中转义,导致 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();
}