0

So I have made a basic app that allows users to enter in data and then upon pressing submit the data is submitted to a firebase and the app closes. However for some reason the app is closing without submitting the data to the firebase.

The code for the submit button is as follows:

void checkinapp::on_pushButton_clicked()
{
    checkinapp::post();
    checkinapp::exit();
}

post() and exit() are defined as follows:

void post()
    {
        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(Url( "url/User.json"));
        newUserRequest.setHeader( QNetworkRequest::ContentTypeHeader, QString( "application/json" ));
        m_networkManager->post( newUserRequest, jsonDoc.toJson() );
        submitted = true;
    }

void exit()
    {
        if(submitted==true)
        {
            QApplication::exit();
        }

    }

When I comment out checkinapp::exit() the app submits to the database but doesn't close. however when it's not commented out it closes the app without uploading to the database.

Edit: Here is my class:

class checkinapp : public QMainWindow
{
    Q_OBJECT

public:
    checkinapp(QWidget *parent = nullptr);
    ~checkinapp();
    void databasehandler(QWidget *parent = nullptr);
    int stressed;
    int happy;
    int hungry;
    int tired;
    int gender;
    bool help;
    int grade;
    bool submitted;
    //bool QNetworkReply::isFinished() const;
    void post()
    {
        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.jason"));
        newUserRequest.setHeader( QNetworkRequest::ContentTypeHeader, QString( "application/json" ));

        connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
        this,SLOT(myOnFinishSlot(QNetworkReply*)));

        m_networkManager->post( newUserRequest, jsonDoc.toJson() );
        //submitted = true;
    }
    void myOnFinishSlot()
    {
        exit();
    }
    void exit()
    {
        QApplication::quit();
    }

public slots:
    //void networkReplyReadyRead();
    void myOnFinishSlot(QNetworkReply* x);
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;
    QNetworkAccessManager * m_networkManager;
    QNetworkReply * m_networkReply;
};
  • 4
    `QNetworkAccessManager::post` is asynchronous operation, instead of instantly closing the app after calling it, you should wait for `QNetworkAccessManager::finished` signal, preferably with some checking if it succeeded, and then close the app in signal handler. https://doc.qt.io/qt-5/qnetworkaccessmanager.html#post – Kaldrr Oct 11 '21 at 09:19
  • 1
    I removed the firebase tag as this is a FAQ for Qt: [QNetworkReply wait for finished](https://stackoverflow.com/questions/5486090/qnetworkreply-wait-for-finished) – Botje Oct 11 '21 at 09:22
  • 1
    Be careful of exposing the hostnames of in-development APIs to SO. – Botje Oct 11 '21 at 09:23
  • Thanks @Kaldrr, did you want to copy and paste that into an answer so i can accept it as the answer. – SilverSerpent03 Oct 11 '21 at 09:41
  • And thank you again @Botje, sorry for the inconvenience. – SilverSerpent03 Oct 11 '21 at 09:41

1 Answers1

1

If I were you I would do this:

void checkinapp::on_pushButton_clicked()
{
    checkinapp::post();
    //checkinapp::exit();
}

then in the post method:

void post()
{
    m_networkManager = new QNetworkAccessManager ( this );
    ...
    HERE connect the Manager to your custom slot, something like
    connect(mm_networkManager, SIGNAL(finished(QNetworkReply*)),
    this,SLOT(myOnFinishSlot(QNetworkReply*)));
    m_networkManager->post( newUserRequest, jsonDoc.toJson() );
    //submitted = true;
}

and then after the response is received close all by calling exit:

void myOnFinishSlot(QNetworkReply* x)
{
    ....
    do some stuff
    exit();
}

how to define the slot:

in the header file of the checkinapp do

class checkinapp : public QObject
{
    Q_OBJECT

public:
    //your public data

public slots:
    void myOnFinishSlot(QNetworkReply* x);

signals:
    //your signals if any

private:
    //your private data
};
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Thanks, @ΦXocę 웃 Пepeúpa ツ, I tried this but im getting the following errors: ```qt.core.qobject.connect: QObject::connect: No such slot checkinapp::myOnFinishSlot(QNetworkReply*) in ../AppCheckIn/checkinapp.h:55``` ```qt.core.qobject.connect: QObject::connect: (receiver name: 'checkinapp')``` – SilverSerpent03 Oct 11 '21 at 10:48
  • that error is because you need to define the slot – ΦXocę 웃 Пepeúpa ツ Oct 11 '21 at 11:11
  • Thanks @ΦXocę 웃 Пepeúpa ツ, this worked but now im getting Undefined symbols for architecture x86_64: "checkinapp::myOnFinishedSlot(QNetworkReply*)", referenced from: "checkinapp::qt_static_metacall(QObject*,QMetaObject::Call,int,void**) in moc_checkinapp.o – SilverSerpent03 Oct 11 '21 at 11:41