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文件。

相关文章
相关标签/搜索