Nominate our 2022 Qt Champions!

Qt GUI merge C++ console app

  • Hi,
    In order to learn about Qt, I made a small GUI by Qt Designer 5.0.2 (via MSVC and Qt-VS-add-in). The GUI contains 3 lineEdits (2 for entry 1 for display result) and 1 pushButton to start the compute.
    Code are as follows:
    h.file @
    #ifndef TEST41_QT6_H
    #define TEST41_QT6_H

    #include <QtWidgets/QMainWindow>
    #include "ui_test41_qt6.h"

    class test41_qt6 : public QMainWindow

    test41_qt6(QWidget *parent = 0);

    Ui::test41_qt6Class ui;

    public slots: void numberChanged();

    #endif // TEST41_QT6_H

    cpp file @
    #include "test41_qt6.h"

    test41_qt6::test41_qt6(QWidget *parent)
    : QMainWindow(parent)
    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(numberChanged()));



    void test41_qt6::numberChanged()
    double nOne = ui.lineEdit->text().toDouble();
    double nTwo = ui.lineEdit_2->text().toDouble();
    double nAdd = nOne + nTwo;

    main.cpp file @
    #include "test41_qt6.h"
    #include <QtWidgets/QApplication>
    #include <array>

    using namespace std;

    int main(int argc, char *argv[])
    array<double,5> x = {1.0, 2.0, 3.0, 4.0, 5.0};

    QApplication a(argc, argv);
    test41_qt6 w;;
    return a.exec();

    In main.cpp I build an array. I want to send the value of nOne and nTwo (entered from GUI) to main.cpp and add them to x[0] (1st ele of x), then return the result to GUI and display it in lineEdit_3.

    Could anyone please tell me how to do this? Thanks in advance!

  • I suggest to start with some examples as they are shipped with Qt libs eg. the "dialog examples":
    This way you are probably much faster in understanding the way to handle the gui together with your application.

  • Thanks. Anyone can give more detailed help, please?

  • Lifetime Qt Champion


    As koahnig suggested, read the Qt examples and documentation. They are well written and explained and the dialog example shows how to do what you want to achieve.

    To get you started: don't pass the value back and forth through your main function.

  • Can anyone help me in concrete way, please? Is what I want to do too complicated or because nobody knows how to do it?

Log in to reply