Home > database >  Qt_Creator close gui and run main.cpp
Qt_Creator close gui and run main.cpp

Time:10-12

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

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

The app gets stuck on w.show(). How can i make the submit button end w.show() and run the next line DatabaseHandler dbhandler

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:

Okay, I think you have some confusion. w.show() makes the window appear. That's it. Execution continues all the way to your a.exec().

What you need to do is have your window tell your DatabaseHandler when it's time to grab values and do an update. The Qt way is to set up a signal. I find those to kind of be a pain, so I use dependency injection. That is, I'd create the handler earlier in main but NOT have the constructor do all that. Make a method. Then pass a reference to the handler in the constructor of the window.

Then when the button is clicked, call a method on the handler to do its job. After that, you can close the app if you want.

  •  Tags:  
  • c qt
  • Related