Mstering QT5 chapter1
涉及到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的更多相关文章
- QT5利用chromium内核与HTML页面交互
在QT5.4之前,做QT开发浏览器只能选择QWebkit,但是有过使用的都会发现,这个webkit不是出奇的慢,简直是慢的令人发指,Release模式下还行,debug下你就无语了,但是webkit毕 ...
- qt5中信号和槽的新语法
qt5中的连接 有下列几种方式可以连接到信号上 旧语法 qt5将继续支持旧的语法去连接,在QObject对象上定义信号和槽函数,及任何继承QObjec的对象(包含QWidget). connect(s ...
- Ubuntu在wps-office等qt5程序下不能切换中文fcitx输入法的问题
经检查,是缺了fcitx-qt的包.比如qt5的程序,需要一个叫fcitx-libs-qt5的包. 如果您在基于qt的程序下不能使用基于fcitx的中文输入法,请检查以下包是否已安装: sudo ap ...
- qt5中文代码编码编译问题
qt中文代码用vs2010编译问题解决 总结说就是qt5默认UTF8不支持微软默认的ANSI(GB2312/GBK).解决办法是把中文字符串全部用 QString::fromLocal8Bit() 封 ...
- VS2010+Qt5.4.0 环境搭建(离线安装)
原创作者:http://blog.csdn.net/solomon1558/article/details/44084969 前言 因项目需要Qt开发GUI,我根据网上资料及自己的经验整理了搭建vs2 ...
- qt5.5程序打包发布以及依赖【转】
玩qt5也有一段时间了,惭愧的是一直没有好好的发布过程序,因为写的都是小程序没啥需要用到发布,而且qt也说不上很熟悉,本来打算到基本掌握qt之后再来研究研究怎么打包程序,最近晚上的空闲时间多了,闲着也 ...
- Qt5.5.1编译出来的程序出现libgcc_s_dw2-1.dll的解决方案
问题如图: 输入"myudp2016.exe 1 " 后出现 这是因为没有在系统环境变量path里加上相关路径,我们添加如下路径: 比如说WIN7系统-开始-计算机-右键-属性- ...
- ubuntu 16.04 + N驱动安装 +CUDA+Qt5 + opencv
Nvidia driver installation(after download XX.run installation file) 1. ctrl+Alt+F1 //go to virtual ...
- Qt5中的信号槽
Qt4中的信号槽 Qt4中的信号槽是通过SIGNAL,SLOT两个宏,将参数转换成字符串.Qt编译前,会从源码的头文件中提取由signal和slot声明的信号和槽的函数, 将其组成一张信号和槽对应的字 ...
随机推荐
- Queue Pair in RDMA (zz)
Queue Pair in RDMA 首页分类标签留言关于订阅2018-03-21 | 分类 Network | 标签 RDMA 一个CA(Channel Adapter)可以包含多个QP,QP相当 ...
- EAST结构分析+pytorch源码实现
目录 EAST结构分析+pytorch源码实现 @ 一. U-Net的前车之鉴 1.1 FCN网络结构 1.2 U-NET网络 1.3 CTPN网络 二. EAST结构分析 2.1 结构简述 2.2 ...
- QTTabBar 使用
QTTabBar 激活 修改语言
- Selenium操作Chrome模拟手机浏览器
目录 使用指定设备 使用自定义设备 在使用Chrome浏览网页时,我们可以使用Chrome开发者工具模拟手机浏览器,在使用Selenium操作Chrome时同样也可以模拟手机浏览器.主要有以下两种用途 ...
- Java学习日记——基础篇(二)基本语法
变量 变量和常量是程序处理的两种基本数据对象,变量是程序的基本组成单位 变量的目的就是确定目标并提供存放空间 public class Hello { public static void main( ...
- POI2010 Bridges
好题\(Q\omega Q\) 我们考虑这个东西要求最大值最小,显然一眼二分答案对吧. 问题在于如何\(check\),我们二分答案之后把问题转换成了混合图如何求欧拉回路. 考虑欧拉回路的性质,每一个 ...
- mybatis 集合in的用法
<!-- 查询 设备List 的 某天,并且小于当前小时的,耗电 记录 --> <select id="findByDeviceIdInAndBetweenRecor ...
- Centos7 开启swap分区
阿里云购买的机器,默认不会开启swap分区,如有需要,需自行开启. 阿里当前的做法是: 1.不创建swap分区,由镜像决定 2.将vm.swappiness设定为0,即永不使用swap分区 开启swa ...
- Jenkins自动化打包(Gitlab)并上传蒲公英
整个过程详见:https://www.jianshu.com/p/91e8f571fc2b 以下是遇到的问题及解决过程 一.安装homebrew(网速很慢很慢……被墙了) /usr/bin/ruby ...
- VM 15 永久激活密钥
VMware Workstation 15 永久激活密钥 YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8 UG5J2-0ME12-M89WY-NPWXX-WQH88 UA5 ...