My problems with my first example



  • I want to write a desktop calculator using Qt. I designed the first step of the form this way:

    alt text

    I added only one signal & slot using Designer. The Calc.h, Calc.pp and main.cpp are as follows respectively:

    #ifndef CALC_H
    #define CALC_H
    
    #include <QDialog>
    
    namespace Ui {
    class Calc;
    }
    
    class Calc : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Calc(QWidget *parent = 0);
        ~Calc();
    
    private:
        Ui::Calc *ui;
    };
    
    #endif // CALC_H
    
    #include "calc.h"
    #include "ui_calc.h"
    
    Calc::Calc(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Calc)
    {
        ui->setupUi(this);
    }
    
    Calc::~Calc()
    {
        delete ui;
    

    }

    #include <QApplication>
    #include "calc.h"
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        Calc* dialog = new Calc;
        dialog -> show();
        return app.exec();
    }
    

    Now what codes do I need to add into Calc.h & Calc.cpp to make it worked please?



  • What would you do if this is an CLI program(command line only, no gui)?

    They may looks like these

    double a = 0, b = 0;
    char operation = '+';
    std::cin>>a;
    std::cin>>operation;
    std::cin>>b;
    switch(operation){
      case '+':
         std::cout<<(a+b)<<std::endl;
      break;
      //....and so on
    }
    

    How would you make this work if it is gui app?

    you can do it as following(pseudo codes)

    1 : show the result of your input button

    connect(ui->oneButton, &QPushButton::clicked, [this]()
    {
        ui->lineEdit->setText(ui->lineEit()->text() + "1");
    });
    

    2 : store the result of lineEdit when user click on the add button

    connect(ui->addButton, &QPushButton::clicked, [this]()
    {
        firstNum = ui->lineEdit->text().toDouble();
    });
    

    3 : show the result of your input button

    4 : show the result of summation when user click on "=" button

    connect(ui->resultButton, &QPushButton::clicked, [this]()
    {
        auto const curNum = ui->lineEdit->text().toDouble();
        ui->lineEdit->setText(QString::number(firstNum + curNum));
    });
    

    These are the simplest way I could think of. There are many things to improve, like support other number, operation, able
    to parse long equation(ex : 3 + 4 - 5 * 6 / 33.1), error handles etc.

    Something related to resource managament.

    This will cause memory leak

    Calc* dialog = new Calc;
    dialog -> show();
    

    Use it like this(best solution in most cases)

    Calc dialog;
    dialog.show();
    

    or(not as good as first solution)

    std::unique_ptr<Calc> dialog(new Calc); //c++14 support make_unique
    dialog -> show();
    

    I strongly suggest you study the concept of RAII and understand the concept of parent--child relationship of Qt, they could
    make your life much more easier to live. After you know how they work, you will find out c++ is more easier to manage resource than those languages with gc(like java, c#, js, python etc).

    ps : I prefer to create the ui by handcrafting codes when I studied Qt codes, because this help me know better about Qt.
    At that time I even do not know what is widget, dialog, mainWindow, what they use for.



  • @tham parent--child relationship of Qt. I am trying to understand better this and apply like him. But this is not different of other languages. Friendship and inheritance basically.



  • @Jeronimo said in My problems with my first example:

    @tham parent--child relationship of Qt. I am trying to understand better this and apply like him. But this is not different of other languages. Friendship and inheritance basically.

    Looks like I did not express it clearly, I should said "Object trees & OwnerShip". It is not about the concept of oop but the memory management solution of Qt.

    Links :

    Object trees & OwnerShip
    Parent child relationship in Qt


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.