学习QT Example and Demo One by One 001——Application

本文详细解析了QT中MainWindow类的设计与实现。重点介绍了MainWindow类的结构,包括公共成员、槽函数及私有成员,并概述了各个部分的功能。

突然决定好好学习QT,我打算把QT Example and Demo中的代码都学习一遍。一什么样的形式记录我的学习过程呢,暂时还没有好的想法,先走一步看一步吧。今天从MainWindow中的Application开始。

 

先看看MainWindow.h文件中的内容

 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QPlainTextEdit;
QT_END_NAMESPACE

//! [0]
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();

protected:
    void closeEvent(QCloseEvent *event);

private slots:
    void newFile();
    void open();
    bool save();
    bool saveAs();
    void about();
    void documentWasModified();

private:
    void createActions();
    void createMenus();
    void createToolBars();
    void createStatusBar();
    void readSettings();
    void writeSettings();
    bool maybeSave();
    void loadFile(const QString &fileName);
    bool saveFile(const QString &fileName);
    void setCurrentFile(const QString &fileName);
    QString strippedName(const QString &fullFileName);

    QPlainTextEdit *textEdit;
    QString curFile;

    QMenu *fileMenu;
    QMenu *editMenu;
    QMenu *helpMenu;
    QToolBar *fileToolBar;
    QToolBar *editToolBar;
    QAction *newAct;
    QAction *openAct;
    QAction *saveAct;
    QAction *saveAsAct;
    QAction *exitAct;
    QAction *cutAct;
    QAction *copyAct;
    QAction *pasteAct;
    QAction *aboutAct;
    QAction *aboutQtAct;
};

从上面的代码可以看出,一般来说一个Mainwindow的类构造了如下内容:

1、public:声明了构造函数

2、private slots:声明了5个槽

3、private:声明了主窗口所包含的一些widget的指针;声明了完成本窗口功能的若干动作

另外还有几个函数留到实现MainWindow时在叙述。。

 

As an AI language model, I do not have the capability to provide a demo. However, I can explain how to create a simple Qt HTTP Web API demo. First, you need to include the necessary headers: ``` #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> ``` Then, you can create a `QNetworkAccessManager` object to send HTTP requests: ``` QNetworkAccessManager *manager = new QNetworkAccessManager(this); ``` You can then use the `get()` method to send a GET request to a Web API: ``` QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("https://jsonplaceholder.typicode.com/posts"))); connect(reply, &QNetworkReply::finished, [=]() { if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); // process the data received from the Web API } else { // handle the error } reply->deleteLater(); }); ``` In the above example, the `QByteArray` `data` contains the response data received from the Web API. You can then process this data as needed. You can also use the `post()` method to send a POST request with data: ``` QNetworkRequest request(QUrl("https://jsonplaceholder.typicode.com/posts")); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QJsonObject postData; postData["title"] = "foo"; postData["body"] = "bar"; postData["userId"] = 1; QJsonDocument doc(postData); QByteArray postDataBytes = doc.toJson(); QNetworkReply *reply = manager->post(request, postDataBytes); connect(reply, &QNetworkReply::finished, [=]() { if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); // process the data received from the Web API } else { // handle the error } reply->deleteLater(); }); ``` In the above example, a JSON object is created and serialized to a `QByteArray`. This data is then sent in the body of the POST request. This is just a basic example of how to use Qt to interact with a Web API. There are many other features and options available in Qt that can be used to customize and optimize the communication with the Web API.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值