简体   繁体   中英

How can I change the timing for signals and slot in QT?

I am creating a program that manages student information. Currently I have a tableview and I have an add button, when I click on the add button, a new dialog pops up, prompting users to add a new student. My intentions are that I create a signal and slot connection to my dialog so that whenever the ok button is pressed, my tableview would refresh (I am using SQLITE). However, the problem right now is that it seems to me that the database is being updated after I call my refreshwindow function, so when I call my refreshwindow function, the database hasn't been updated yet. I am not sure if this is the problem forsure but this is what I think.

Below are some codes: When I click on the add button

void viewStudents::on_addStudent_clicked()
{
    studentWindow = new studentManagement(username,this);
    QObject::connect(studentWindow,SIGNAL(accepted()),this, SLOT(refreshwindow()));
    studentWindow->show();
}

my refreshwindow function

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.");
    }
}

my dialog for adding students

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

If someone could help me out a little or give me some suggestions on where to look I would really appreciate it!

First of all you should have used your connect statement in constructors or every time on_addStudent_clicked() is being called a new connect will be executed which does not formulate a bug but it is completely unnecessary.

Secondly , when we are adding a record to a list we want to make sure list is being refreshed right away, So it need to be right after the insert statement.

Thirdly , Qt is very much intelligent to find the implicit connects that you want to create, if you are creating a function with the general form of on_something_someevent() then it will automatically look for object/class (of type QObject ) named something and connects its signal which is someevent to this and on_something_someevent() as the slot. As a result writing a function like thisobject::on_something_someevent(){} will automatically rendered into:

 QObject::connect(something, SIGNAL(someevent()), this, SLOT(on_something_someevent()));

which is very convenient, but it could tries to create an unwanted connect or at least raise a warning or error.

Finally , do not forget to define your slot with public slot: in your header file. Public part is necessary or you would get access violation error since Qt framework can not call your slot function (from another object).

Here it is the corrected form of your code that I think could do the job (I didn't have your.ui or.h files neither the full source of your both classes and your main() function, so please consider this as a partial source which I hope that conveys the gist)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM