Mstering QT5 chapter1

涉及到c++ 14新特性: lambda,autovariables.c++

A basic .pro file generally contains:
1) Qt modules used (core, gui, and so on)
2) Target name (todo, todo.exe, and so on)
3) Project template (app, lib, and so on)
4) Sources, headers, and forms架構

-----------------------------------------------------   例 子-----------------------------------------------------------------------
In Qt Creator, you can create a new Qt project via File | New File or Project | Application| Qt Widgets Application.app

下面四個文件的基本架構都是由qt creator在建立工程時自動生成的!函數

1) pro文件:工具

For GCC and CLANG compilers, you must add CONFIG += c++14 to the .pro file to enable C++14 on a Qt project, as shown in the following code:oop

QT += core gui
CONFIG += c++14
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = todo
TEMPLATE = app
SOURCES += main.cpp \
MainWindow.cpp
HEADERS += MainWindow.h \
FORMS += MainWindow.ui \           //qt5裏面沒有.ui
ui

其中,MainWindow.ui是xml形式的ui 文件,能夠由qt creator打開。this

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

As usual, the main.cpp file , by default, perform two actions:
Instantiate and show your main window
Instantiate a QApplication and execute the blocking main event loopspa

Qt tip:
編譯快捷鍵:Ctrl + B (for Windows/Linux) or Command + B (for Mac)
Debug模式下運行快捷鍵: F5 (for Windows / Linux) or Command +R (for Mac) to run your application in debug mode。debug

3)mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;  //用於聲明在Ui命名空間中存在一個與界面對應的MainWindows類,跟下面定義的同名類是不一樣的。   ?
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
     //
C++關鍵字explici,聲明爲explicit的構造函數不能在隱式轉換中使用。
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void addTask();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

私有成員變量ui屬於類 Ui::MainWindow, which is defined in the ui_MainWindow.h file generated by Qt. It's the C++ transcription of the UI design file MainWindow.ui, 若是你用qt designer添加了一些控件,例如按鈕,從新編譯後,會看到ui_MainWindow.h中會增長相應的button的定義,從而也能夠直接在該文件中經過代碼來增長控件!!
The ui member variable will allow you to interact with your UI components (QLabel, QPushButton, and so on) from C++。

4)mainwindow.cpp
#include "mainwindow.h"
#include "
ui_mainwindow.h//該文件是根據mainwindow.ui文件自動生成的
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)  //經過初始化成員列表來給成員變量ui賦值。
{
    //
setupUi function is to initialize all widgets used by the MainWindow.ui design file,例如 menuBar = new QMenuBar(MainWindow);
    ui->setupUi(this);
   //connect的使用,pushButton 是按鈕類型的指針,其在ui_mainwindow.h中被定義:pushButton = new QPushButton(centralWidget);
   //信號接收者: QApplication::instance(). It is the QApplication object created in main.cpp.
   //槽函數:&QApplication::quit,this is a pointer to one of the receiver's member slot functions. In
   //this example, we use the built-in quit() slot from Qapplication, which will exit the application. quit是
QCoreApplication類的靜態函數。
    connect(ui->pushButton, &QPushButton::clicked,
                QApplication::instance(), &QApplication::quit);

    //使用本身在類中定義的槽函數:
   connect(ui->pushButton_2, &QPushButton::clicked,
    this, &MainWindow::addTask);
}

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


void MainWindow::addTask()
{
    qDebug() << "User clicked on the button!";
}

ui_mainwindow.h 中定義了命名空間Ui。能夠查看代碼。

---------------------------------------------------  例 子 end-----------------------------------------------------------------------

新建一個類,來用這個類hold our data. 新建的這個類有本身的ui文件,從而能夠與mainWindow區分開來。qt creator提供了一個自動工具

來新建一個C++類以及與它關聯的ui文件。

相關文章
相關標籤/搜索