gpt4 book ai didi

c++ - 如何更改 QT 中信号和插槽的时序?

转载 作者:行者123 更新时间:2023-12-03 07:02:00 25 4
gpt4 key购买 nike

我正在创建一个管理学生信息的程序。目前我有一个tableview,我有一个添加按钮,当我点击添加按钮时,会弹出一个新对话框,提示用户添加一个新学生。我的意图是为我的对话框创建一个信号和插槽连接,以便每当按下 ok 按钮时,我的 tableview 都会刷新(我正在使用 SQLITE)。但是,现在的问题是,在我调用 refreshwindow 函数后,数据库似乎正在更新,所以当我调用 refreshwindow 函数时,数据库还没有更新。我不确定这是否是问题,但这是我的想法。
下面是一些代码:
当我点击添加按钮

void viewStudents::on_addStudent_clicked()
{
studentWindow = new studentManagement(username,this);
QObject::connect(studentWindow,SIGNAL(accepted()),this, SLOT(refreshwindow()));
studentWindow->show();
}
我的刷新窗口功能
void viewStudents::refreshwindow()
{
QSqlQueryModel *modal = new QSqlQueryModel();
QSqlDatabase tempdb = QSqlDatabase::addDatabase("QSQLITE");
tempdb.setDatabaseName("accounts.db");
if(tempdb.open()){
QSqlQuery tempquery;
tempquery.exec("SELECT firstname, lastname, DOB, Day_of_lessons, Start_date, Price_per_lesson, Length_of_lessons from studentList WHERE teacher = '"+username+"';");
modal->setQuery(tempquery);
ui->tableView->setModel(modal);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();
tempdb.close();
}
else{
QMessageBox::warning(this,"Error","Something unexpected has happened.");
}
}
我添加学生的对话框
studentManagement::studentManagement(QString username, QWidget *parent) :
QDialog(parent),
ui(new Ui::studentManagement)
{
this->username = username;
ui->setupUi(this);
QFont information_font = ui->informationLabel->font();
information_font.setPointSize(14);
information_font.setBold(true);
ui->informationLabel->setFont(information_font);
ui->startdate->setMinimumDate(QDate::currentDate());
ui->dayOfLessonsBox->addItem("Monday");
ui->dayOfLessonsBox->addItem("Tuesday");
ui->dayOfLessonsBox->addItem("Wednesday");
ui->dayOfLessonsBox->addItem("Thursday");
ui->dayOfLessonsBox->addItem("Friday");
ui->dayOfLessonsBox->addItem("Saturday");
ui->dayOfLessonsBox->addItem("Sunday");
}

studentManagement::~studentManagement()
{
delete ui;
}

void studentManagement::on_buttonBox_accepted()
{
QString firstname = ui->firstname->text();
QString lastname = ui->lastname->text();
QString DOB = ui->dateofbirth->text();
QString dayOfLessons = ui->dayOfLessonsBox->currentText();
QString startdate = ui->startdate->text();
QString pricing = ui->pricing->text();
QString lengthoflessons = ui->lengthoflessons->text();
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("accounts.db");
if(!mydb.open())QMessageBox::warning(this,"File Not Found Error", "The database file cannot be find.");
else{
QSqlQuery query;
if(query.exec("INSERT INTO studentList VALUES('"+firstname+"', '"+lastname+"', '"+DOB+"', '"+dayOfLessons+"', '"+startdate+"', '"+pricing+"', '"+lengthoflessons+"', '"+username+"');")){
mydb.close();
}
}
}
如果有人可以帮助我一点或者给我一些关于在哪里看的建议,我将非常感激!

最佳答案

第一你应该使用你的connect构造函数中的语句或每次on_addStudent_clicked()正在调用一个新的连接,它不会形成一个错误,但它是完全没有必要的。
其次 ,当我们向列表中添加记录时,我们希望确保立即刷新列表,因此它需要在 insert 之后陈述。
第三 , Qt 非常智能地找到您想要创建的隐式连接,如果您正在创建具有 on_something_someevent() 的一般形式的函数然后它将自动查找名为 QObject 的对象/类(类型为 something )并连接其信号someeventthison_something_someevent()作为插槽。结果写了一个像 thisobject::on_something_someevent(){} 这样的函数会自动渲染成:

 QObject::connect(something, SIGNAL(someevent()), this, SLOT(on_something_someevent()));
这非常方便,但它可能会尝试创建不需要的 connect或至少引发警告或错误。
最后 ,不要忘记使用 public slot: 定义您的插槽在你的头文件中。公共(public)部分是必需的,否则您将收到访问冲突错误,因为 Qt 框架无法调用您的插槽函数(从另一个对象)。
这是我认为可以完成这项工作的代码的更正形式(我没有你的 .ui 或 .h 文件,也没有你的两个类的完整源代码和你的 main() 函数,所以请将此视为我希望传达要点的部分来源)
void viewStudents::refreshwindow()
{
QSqlQueryModel modal = QSqlQueryModel::QSqlQueryModel();
QSqlDatabase tempdb = QSqlDatabase::addDatabase("QSQLITE");
tempdb.setDatabaseName("accounts.db");
if (tempdb.open()) {
QSqlQuery tempquery;
tempquery.exec("SELECT firstname, lastname, DOB, Day_of_lessons, Start_date, Price_per_lesson, Length_of_lessons from studentList WHERE teacher = '" + username + "';");
modal->setQuery(tempquery);
ui->tableView->setModel(modal);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();
tempdb.close();
}
else {
QMessageBox::warning(this, "Error", "Something unexpected has happened.");
}
}

studentManagement::studentManagement(QString username, QWidget *parent) :
QDialog(parent),
ui(new Ui::studentManagement)
{
this->username = username;
ui->setupUi(this);
QFont information_font = ui->informationLabel->font();
information_font.setPointSize(14);
information_font.setBold(true);
ui->informationLabel->setFont(information_font);
ui->startdate->setMinimumDate(QDate::currentDate());
ui->dayOfLessonsBox->addItem("Monday");
ui->dayOfLessonsBox->addItem("Tuesday");
ui->dayOfLessonsBox->addItem("Wednesday");
ui->dayOfLessonsBox->addItem("Thursday");
ui->dayOfLessonsBox->addItem("Friday");
ui->dayOfLessonsBox->addItem("Saturday");
ui->dayOfLessonsBox->addItem("Sunday");

// This connect statment could be quit unnecessary as Qt will create it automatically
// Since you have created the function name as it is expected by Qt (on_class_event)
QObject::connect(addStudent, SIGNAL(clicked()), this, SLOT(on_addStudent_clicked()));
}

studentManagement::~studentManagement()
{
delete ui;
}

void studentManagement::on_addStudent_clicked()
{
QString firstname = ui->firstname->text();
QString lastname = ui->lastname->text();
QString DOB = ui->dateofbirth->text();
QString dayOfLessons = ui->dayOfLessonsBox->currentText();
QString startdate = ui->startdate->text();
QString pricing = ui->pricing->text();
QString lengthoflessons = ui->lengthoflessons->text();
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("accounts.db");
if (!mydb.open())
{
QMessageBox::warning(this, "File Not Found Error", "The database file cannot be find.");
}
else
{
QSqlQuery query;
if (query.exec("INSERT INTO studentList VALUES('" + firstname + "', '" + lastname + "', '" + DOB + "', '" + dayOfLessons + "', '" + startdate + "', '" + pricing + "', '" + lengthoflessons + "', '" + username + "');"))
mydb.close();
}
studentWindow = new studentManagement(username, this);
studentWindow->show();

viewStudents::refreshwindow();
// OR
// define refreshwindow as a public slot and emit a signal from here
emit(viewStudents::refreshwindow());
}

关于c++ - 如何更改 QT 中信号和插槽的时序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64812616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com