F
F
Fedor2016-02-05 17:59:21
Qt
Fedor, 2016-02-05 17:59:21

QT: how to update the database after query execution?

There was a problem that after entering data into tables, before closing the program, they are not displayed in other tables.
For example, there is a reference table in which I enter data and write it to the database (yes, they are recorded in the database, I checked this), then I open a form that should use this data for itself, but they are not displayed there, that is, after saving there is no data update.
A little about the structure of the program:
The following variables for windows are declared in the mainwindow.h file:

genusPlantD* genPlantD;
    researcherD* researchD;
    openProjectD* openPrD;
    addEditProject* aeProj; //добавление/редактирование проекта

Accordingly, when a menu item is clicked, the following code is executed:
void MainWindow::ht_genusPlants()
{
    genPlantD = new genusPlantD();
    genPlantD->show();
}

in this place I add reference information and save everything (an entry appears in the database).
When I call, for example, adding/editing a project (addEditProject* aeProj; ) or in a window openProjectD* openPrD; I do editing the project, then new data does not appear in the combobox.
Now, when creating a project (addEditProject* aeProj; ), when you click on the save button in it, the following code is executed:
void db::PlantStore(QString name, QString descr, QString gps, QString typeP, QString genusP, QString age, QString cipher, QString variety, QString whoAdded, QDate dateAdded)
{
    qDebug() << "db::PlantStore .....";
    QSqlQuery* sq = new QSqlQuery(sdb->getDB());
    strQuery = "INSERT INTO plants (title, description, gps, type_plant_id, genus_plant_id,"
               "age, cipher, variety, who_added_id, date_added) VALUES ('"+name+"','"+descr+"','"+
                gps+"','"+typeP+"','"+genusP+"','"+age+"','"+cipher+"','"+variety+
                "','"+whoAdded+"','"+dateAdded.toString("yyyy-MM-dd")+"')";
    sq->exec(strQuery);
}

and in order for the window openProjectD* openPrD; new project data displayed, from addEditProject* aeProj; I send a signal to the main window:
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
..... 
    openPrD = new openProjectD(); //для открытия проекта
    aeProj = new addEditProject();
.......
   connect(aeProj,SIGNAL(sendOnlyCreateProjectSignal()),
           this, SLOT(afterCreateNewProject()));
}

which performs the slot:
void MainWindow::afterCreateNewProject()
{
    openPrD = new openProjectD();
}

and after that in openProjectD* openPrD; I see the data that was written by the SQL query.
If for each data change to create a new instance on a signal, this is not good ...
But it seems that this is such a bicycle that I'm wrong and there is a way to do it more gracefully, for example, by reconnecting to the database.
What do you advise?

PS> I enter the data in the combobox as follows:
QMultiMap<QString, QString> db::researchList_Term()
{
    QString id, fio;
    QSqlQuery *p_Query = new QSqlQuery("SELECT id, fio FROM researcher", sdb->getDB());
    QSqlRecord record;
    QMultiMap<QString,QString> rMap;
    while(p_Query->next()){
        record=p_Query->record();
        id=record.value("id").toString();
        fio=record.value("fio").toString();
        rMap.insert(id, fio);
    }
    return rMap;
}

and add to ComboBox:
void addEditProject::reportResearcher()
{
    researchMap = myDB->researchList_Term();
    QMultiMap<QString, QString>::iterator it = researchMap.begin();
    for (;it != researchMap.end(); ++it) {
        ui->researcher_cb->addItem(it.value());
        ui->researcher2_cb->addItem(it.key());
    }
}

After that, we make the second combobox hidden, and in the Designer exposes signals and slots for changing the values ​​of the combobox.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question