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

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.

下面四个文件的基本架构都是由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:

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

其中,MainWindow.ui是xml形式的ui 文件,可以由qt creator打开。

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 loop

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。

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

Mstering QT5 chapter1的更多相关文章

  1. QT5利用chromium内核与HTML页面交互

    在QT5.4之前,做QT开发浏览器只能选择QWebkit,但是有过使用的都会发现,这个webkit不是出奇的慢,简直是慢的令人发指,Release模式下还行,debug下你就无语了,但是webkit毕 ...

  2. qt5中信号和槽的新语法

    qt5中的连接 有下列几种方式可以连接到信号上 旧语法 qt5将继续支持旧的语法去连接,在QObject对象上定义信号和槽函数,及任何继承QObjec的对象(包含QWidget). connect(s ...

  3. Ubuntu在wps-office等qt5程序下不能切换中文fcitx输入法的问题

    经检查,是缺了fcitx-qt的包.比如qt5的程序,需要一个叫fcitx-libs-qt5的包. 如果您在基于qt的程序下不能使用基于fcitx的中文输入法,请检查以下包是否已安装: sudo ap ...

  4. qt5中文代码编码编译问题

    qt中文代码用vs2010编译问题解决 总结说就是qt5默认UTF8不支持微软默认的ANSI(GB2312/GBK).解决办法是把中文字符串全部用 QString::fromLocal8Bit() 封 ...

  5. VS2010+Qt5.4.0 环境搭建(离线安装)

    原创作者:http://blog.csdn.net/solomon1558/article/details/44084969 前言 因项目需要Qt开发GUI,我根据网上资料及自己的经验整理了搭建vs2 ...

  6. qt5.5程序打包发布以及依赖【转】

    玩qt5也有一段时间了,惭愧的是一直没有好好的发布过程序,因为写的都是小程序没啥需要用到发布,而且qt也说不上很熟悉,本来打算到基本掌握qt之后再来研究研究怎么打包程序,最近晚上的空闲时间多了,闲着也 ...

  7. Qt5.5.1编译出来的程序出现libgcc_s_dw2-1.dll的解决方案

    问题如图: 输入"myudp2016.exe 1  " 后出现 这是因为没有在系统环境变量path里加上相关路径,我们添加如下路径: 比如说WIN7系统-开始-计算机-右键-属性- ...

  8. ubuntu 16.04 + N驱动安装 +CUDA+Qt5 + opencv

    Nvidia driver installation(after download XX.run installation file) 1. ctrl+Alt+F1   //go to virtual ...

  9. Qt5中的信号槽

    Qt4中的信号槽 Qt4中的信号槽是通过SIGNAL,SLOT两个宏,将参数转换成字符串.Qt编译前,会从源码的头文件中提取由signal和slot声明的信号和槽的函数, 将其组成一张信号和槽对应的字 ...

随机推荐

  1. linux系列(二十三):df命令

    1.命令格式 df [选项] [文件] 2.命令功能 显示指定磁盘文件的可用空间.如果没有文件名被指定,则所有当前被挂载的文件系统的可用空间将被显示.默认情况下,磁盘空间将以 1KB 为单位进行显示, ...

  2. 【转】gcc的__builtin_函数介绍

    转自:http://blog.csdn.net/jasonchen_gbd/article/details/44948523 GCC提供了一系列的builtin函数,可以实现一些简单快捷的功能来方便程 ...

  3. 手写实现RPC框架(不带注册中心和带注册中心两种)

    实现自己的RPC框架如果不需要自定义协议的话那就要基于Socket+序列化. ProcessorHandler:主要是用来处理客户端的请求. package dgb.nospring.myrpc; i ...

  4. Python 自学笔记(六)

    PK小游戏 1.要有玩家敌人:那就是需要定义两个角色的属性变量 2.相互攻击:需要两个角色都有血量和攻击的变量(也就是四个变量) 3.攻击减少血量:比如玩家血量=敌人攻击力-玩家当前血量 4.最终胜负 ...

  5. 如果前面的IO操作出问题了,按照我们代码的意思,不就try catch 了吗,这样的话线程就没关闭了,就会造成线程泄露。 那怎么解决这个问题呢,其实也简单,把关闭线程的方法写到finally里就可以了。

    https://mp.weixin.qq.com/s/WaNVT2bZFGHNO_mb5nK6vw 连HDFS源码大神都会犯的错之线程泄露(1) 西瓜老师 西瓜老师爱大数据 1月11日  

  6. android: Canvas的drawArc()方法的几个误区

    绘制圆环很多时候会用到Canvas的drawArc方法, drawArc()方法的说明很简单: public void drawArc (RectF oval, float startAngle, f ...

  7. 解决com.android.support版本冲突问题

    原文:https://www.jianshu.com/p/0fe985a7e17e 项目中不同Module的support包版本冲突怎么办? 只需要将以下代码复制到每个模块的build.gradle( ...

  8. osgViewer::Viewer::Windows

    osg自带窗口去掉边框 #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #incl ...

  9. Linux 下kafka集群搭建

    主机的IP地址: 主机IP地址 zookeeper kafka10.19.85.149 myid=1 broker.id=110.19.15.103 myid=2 broker.id=210.19.1 ...

  10. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_01-用户认证需求分析

    1.1 用户认证与授权 截至目前,项目已经完成了在线学习功能,用户通过在线学习页面点播视频进行学习.如何去记录学生的学习过程 呢?要想掌握学生的学习情况就需要知道用户的身份信息,记录哪个用户在什么时间 ...