Home > Mobile >  Qt GUI C : Input values using GUI, then close GUI entirely and continue with main C code using in
Qt GUI C : Input values using GUI, then close GUI entirely and continue with main C code using in

Time:01-23

So I am learning Qt GUI for C (Linux Mint 21, Qt Creator 5). I am trying to write a simple program that will open a box with 3 fields for the user to enter values: a1, a2, and a3. Then there are two buttons: OK and Quit. See screenshot below for GUI box. Then when the user hits "Quit" the GUI and entire C code should terminate. I got this part working. When the user hits "OK", then the Qt GUI should save these values into variables, quit the GUI entirely, and pass the variables back to the main c code for use in ....this is where I am stuck:

Qt GUI Input Box

I want Qt GUI to completely shutdown and pass the control back to main c with the values/variables that the user entered. This is where I am stuck. Code is below:

main.cpp:

#include "mainwindow.h"
#include <QtCore>
#include <QApplication>
#include <QString>


int main(int argc, char *argv[])
{
    //=========================================================
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();

    // Qt GUI stops, main C   code continues....
    //=========================================================

    //float a1_temp = MainWindow::on_pushButton_clicked();
    //float a2_temp = MainWindow::on_pushButton_clicked();
    //float a3_temp = MainWindow::on_pushButton_clicked();

    //std::cout << "Qt passed back into main: " << a1_temp << std::endl;
    //std::cout << "Qt passed back into main: " << a2_temp << std::endl;
    //std::cout << "Qt passed back into main: " << a3_temp << std::endl;

}

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_2_clicked() // When QUIT is pressed, exit/terminate the whole program
{
    QApplication::quit();
}

void MainWindow::on_pushButton_clicked() // When OK is pressed, save values and return to main C  
{
    float a1 = ui->lineEdit->text().toFloat();
    float a2 = ui->lineEdit_2->text().toFloat();
    float a3 = ui->lineEdit_3->text().toFloat();

    // print values to verify of variables correctly saved the values
    std::cout << a1 << std::endl;
    std::cout << a2 << std::endl;
    std::cout << a3 << std::endl;

}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_pushButton_2_clicked();

    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

CodePudding user response:

Looks like your only problem is that you're calling return from main() when exec() returns:

return a.exec();

You could replace that with this:

int retVal = a.exec();

[.... Main C   code continues here ....]

float a1 = w.ui->lineEdit->text().toFloat();
[...]

return retVal;  // only at the end of main()

... and get the behavior you want.

  • Related