Qt工程的建立

來源:互聯網
上載者:User
一、Qt工程的建立(單一個情況:嵌入式開發時建立的gui)1.開啟Qtcreator,creator 一個new project2.選擇qt c++ project 中的qt project gui app3.起個名字,選擇路徑(最好不要有中文路徑)並next3.到選擇基類的時候(base class),選擇qwidget,因為要建一個視窗4.接著next和finish就可以了5.在右上方附近的filter tree中把hide選項的勾去掉,然後編譯一下,會顯示出自動產生的.h檔案二、程式碼分析(為便於分析,從UI中托入一些label或者button)

#ifndef WIDGET_H
#define WIDGET_H

#include
<QWidget>

namespace Ui //聲明,下面的類中用到了這個類,有什麼用

{
    class Widget;
}

class Widget :
public QWidget //有什麼用

{
    Q_OBJECT

public:
    explicit Widget(QWidget
*parent = 0);
    ~Widget();

private:
    Ui::Widget
*ui; //私人一個
};

#endif

這是標準的標頭檔!(除了自動產生的.h檔案,這也是預設唯一個標頭檔)

#include
<QtGui/QApplication>
#include
"widget.h"

int main(int argc,
char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();//為什麼能調用show

    return a.exec();//返回的是什麼
}

標準的main.cpp

#include
"widget.h"
#include
"ui_widget.h"

Widget::Widget(QWidget
*parent):QWidget(parent)

                                   ,ui(new
Ui::Widget)

//傳兩個參數,第一個是繼承的基類需要的參數,第二個是自己需要參數

有什麼用?


{
    ui->setupUi(this);//這個函數是什麼,this是誰的?有什麼用
}

Widget::~Widget()
{
    delete ui;
}

標準的另一個.cpp檔案

以上這三個都是建立好工程以後自動產生的,不需要編譯

還有一個系統編譯產生.h檔案

/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created: Mon Nov 22 20:46:49 2010
** by: Qt User Interface Compiler version 4.7.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include
<QtCore/QVariant>
#include
<QtGui/QAction>
#include
<QtGui/QApplication>
#include
<QtGui/QButtonGroup>
#include
<QtGui/QHeaderView>
#include
<QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:

    void setupUi(QWidget
*Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(400, 300);

        retranslateUi(Widget);

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget
*Widget)
    {
        Widget->setWindowTitle(QApplication::translate("Widget",
"Widget", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui //

{
    class Widget:
public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.