MainWindow from code from the main.cpp in Qt

后端 未结 2 814
北海茫月
北海茫月 2021-01-26 08:53

Want to understand the difference in code between the MainWindow and the main.cpp. Specifically, how a chunk of code written exclusively in the m

2条回答
  •  时光取名叫无心
    2021-01-26 09:27

    You can put all of this code in constructor of your QMainWindow and retaining lambda functions as-is.

    Another more clean way would be transforming those lambda functions into private slots. Using this way, you should define networkManager as a class member of your QMainWindow class and also it should be allocated in heap memory not stack. To get this done, just define a QNetworkManager* class member and initialize it in your QMainWindow constructor.

    this->networkManager = new QNetworkManager(this);
    

    Once it's been initialized, you can use it in all slots of your QMainWindow class.

    A simple rule of thumb is: all shared variables among lambda functions and the main scope should be class members in this way.


    The code. (I tested it and works fine)

    main.cpp

    #include "mainwindow.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->lineEditGender->setReadOnly(true);
        ui->lineEditRegion->setReadOnly(true);
        ui->lineEditName->setReadOnly(true);
    
        networkManager = new QNetworkAccessManager(this);
    
        connect(networkManager, &QNetworkAccessManager::finished, this, &MainWindow::onNetworkManagerFinished);
        connect(ui->btnGetName, &QPushButton::clicked, this, &MainWindow::onBtnGetNameClicked);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::onNetworkManagerFinished(QNetworkReply *reply)
    {
        if(reply->error() != QNetworkReply::NoError){
            ui->lineEditName->setText("Error");
            ui->lineEditGender->setText("Error");
            ui->lineEditRegion->setText("Error");
    
            networkManager->clearAccessCache();
        } else {
            //parse the reply JSON and display result in the UI
            QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object();
            QString fullName= jsonObject["name"].toString();
            fullName.append(" ");
            fullName.append(jsonObject["surname"].toString());
            ui->lineEditName->setText(fullName);
            ui->lineEditGender->setText(jsonObject["gender"].toString());
            ui->lineEditRegion->setText(jsonObject["region"].toString());
        }
        ui->btnGetName->setEnabled(true);
        reply->deleteLater();
    }
    
    void MainWindow::onBtnGetNameClicked()
    {
        QUrlQuery query;
        query.addQueryItem("amount", "1");
        query.addQueryItem("region", "United States");
        QUrl url("http://uinames.com/api/");
        url.setQuery(query);
        QNetworkRequest networkRequest(url);
    
        //send GET request when the button is clicked
        networkManager->get(networkRequest);
        ui->btnGetName->setEnabled(false);
    
        ui->lineEditName->setText("Loading. . .");
        ui->lineEditGender->setText("Loading. . .");
        ui->lineEditRegion->setText("Loading. . .");
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void onNetworkManagerFinished(QNetworkReply* reply);
        void onBtnGetNameClicked();
    
    private:
        Ui::MainWindow *ui;
        QNetworkAccessManager *networkManager;
    };
    
    #endif // MAINWINDOW_H
    

提交回复
热议问题