1日目の最適化
通常、QWidgetを直接使用してオブジェクトをインスタンス化するのではなく、その派生クラスを使用してオブジェクトを生成します。通常、main関数にあまり多くのコードを記述せず、ウィンドウの初期化、レイアウト、設定、および従属を行います。派生クラスのコンストラクタ。ウィンドウ設定など。
サンプルコードを添付してください:
widget.h
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
signals:
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent) : QWidget(parent)
{
new QPushButton("button",this);
}
main.cpp
//小问学QT
#include<QApplication>//QObject是QT中最基本的类
#include"widget.h"
int main(int argc,char** argv)
{
QApplication app(argc,argv);
Widget widget;
widget.show();
return app.exec();
}
QTメモリ管理メカニズム
例: