Home > Mobile >  Tell a function to wait until the user presses a button with Qt
Tell a function to wait until the user presses a button with Qt

Time:10-11

I have created a project and a basic app where there is a ui that pops up for users to enter data and then the data is uploaded to a firebase database. When I attempt to run the app the ui appears and i can enter in the data like in this image:

App Ui

However, the app is uploading to the data base as soon as it opes and so the database entry is all zeros. How can I make the app upload to the database and close after pressing submit. I am very new to c so any help would be appreciated :).

Here is my main.cpp:

#include "checkinapp.h"
#include "databasehandler.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    checkinapp w;
    w.show();
    DatabaseHandler dbhandler;
    return a.exec();
}

here is my checkinapp.h:

#ifndef CHECKINAPP_H
#define CHECKINAPP_H

#include <iostream>

#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>


QT_BEGIN_NAMESPACE
namespace Ui { class checkinapp; }
QT_END_NAMESPACE

class checkinapp : public QMainWindow
{
    Q_OBJECT

public:
    checkinapp(QWidget *parent = nullptr);
    ~checkinapp();

private slots:

    void on_happy_valueChanged(int value);

    void on_hungry_valueChanged(int value);

    void on_sleep_valueChanged(int value);

    void on_stress_valueChanged(int value);

    void on_male_toggled(bool checked);

    void on_female_toggled(bool checked);

    void on_other_toggled(bool checked);

    void on_help_toggled(bool checked);

    void on_pushButton_clicked();

private:
    Ui::checkinapp *ui;
};


#endif  // CHECKINAPP_H

checkinapp.cpp:

#include "checkinapp.h"
#include "ui_checkinapp.h"
#include "databasehandler.h"
#include "global_objects.hpp"
#include <QNetworkRequest>
#include <QDebug>
#include <QJsonDocument>
#include <QVariantMap>
#include <iostream>

using namespace std;

checkinapp::checkinapp(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::checkinapp)
{
    ui->setupUi(this);
}
checkinapp::~checkinapp()
{
    if(help == 1)
    {
        //delete ui;
    }
    if(help == 1)
    {
        cout << "help";
    }
}

void checkinapp::on_happy_valueChanged(int value)
{
    happy = value;
}

void checkinapp::on_hungry_valueChanged(int value)
{
    hungry = value;
}


void checkinapp::on_sleep_valueChanged(int value)
{
    tired = value;
}


void checkinapp::on_stress_valueChanged(int value)
{
    stressed = value;
}


void checkinapp::on_male_toggled(bool checked)
{
    if(checked == true)
    {
        gender = 0;
    }
}

void checkinapp::on_female_toggled(bool checked)
{
    if(checked == true)
    {
        gender = 1;
    }
}

void checkinapp::on_other_toggled(bool checked)
{
    if(checked == true)
    {
        gender = 2;
    }
}

void checkinapp::on_help_toggled(bool checked)
{
    if(checked == true)
    {
        help = 1;
    }
}

void checkinapp::on_pushButton_clicked()
{
    submitted = true;
        if(submitted==true)
        {
            cout <<submitted;
        }
    //delete ui;
}

databasehandler.h:

#ifndef DATABASEHANDLER_H
#define DATABASEHANDLER_H
#include <checkinapp.h>
#include <QObject>
#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class DatabaseHandler : public QObject
{
    Q_OBJECT
public:
    explicit DatabaseHandler(QObject *parent = nullptr);
    ~DatabaseHandler();

public slots:
    void networkReplyReadyRead();


signals:

private:
    QNetworkAccessManager * m_networkManager;
    QNetworkReply * m_networkReply;

};

#endif // DATABASEHANDLER_H

databasehandler.cpp:

#include "checkinapp.h"
#include "databasehandler.h"
#include "global_objects.hpp"
#include <QNetworkRequest>
#include <QDebug>
#include <QJsonDocument>
#include <QVariantMap>
#include <iostream>


DatabaseHandler::DatabaseHandler(QObject *parent) : QObject(parent)
{
    m_networkManager = new QNetworkAccessManager ( this );
    QVariantMap newUser;
    newUser[ "Stress" ] = QString::number(stressed);
    newUser[ "Sleep" ] = QString::number(tired);
    newUser[ "Hungry" ] = QString::number(hungry);
    newUser[ "Happy" ] = QString::number(happy);
    newUser[ "Grade" ] = QString::number(grade);
    newUser[ "Date" ] = "1/10/21";
    newUser[ "Gender" ] = QString::number(gender);
    newUser[ "Aid" ] = QString::number(help);
    QJsonDocument jsonDoc = QJsonDocument::fromVariant( newUser );
    QNetworkRequest newUserRequest( QUrl( "url/User.json"));
    newUserRequest.setHeader( QNetworkRequest::ContentTypeHeader, QString( "application/json" ));
    m_networkManager->post( newUserRequest, jsonDoc.toJson() );
}
DatabaseHandler::~DatabaseHandler()
{
    m_networkManager->deleteLater();
}
void DatabaseHandler::networkReplyReadyRead()
{
    //qDebug() << m_networkReply->readAll();
}

CodePudding user response:

To do that, according to your code, you should not handle you database connection in your main() function, but in a specific manager to open or close depending on your needs.

So following your code, you have some ideas here:

  • Remove DatabaseHandler dbhandler; from main() and create it in your checkinapp class, adding new methods to post values into the database using the values set in the GUI.
  • Keep the line DatabaseHandler dbhandler; but then you need to pass the object to the checkinapp class. Same than before, you will need new methods to populate the database.

Apart from that, I think you don't need to post in the DatabaseHandler constructor at the beginning:

m_networkManager->post( newUserRequest, jsonDoc.toJson() );
  •  Tags:  
  • c qt
  • Related