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声明的信号和槽的函数, 将其组成一张信号和槽对应的字 ...
随机推荐
- /etc/crontab
- Ubuntu下Django+uWSGI+nginx部署
本文采用uwsgi+nginx来部署django 这种方式是将nginx作为服务端前端,将接受web所有的请求,统一管理,Nginx把所有的静态请求自己处理,然后把所有非静态请求通过uwsgi传递给D ...
- Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
[POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...
- Ansible 模式
一.Ansible 命令 1.Ansible 命令执行的方式有两种:Ad-Hoc.Ansible-playbooks,这两种方式没有本质的区别,Ad-Hoc用于临时执行命令:Ansible-playb ...
- elasticsearch启动错误
requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled java.lang.UnsupportedOpe ...
- springMVC_配置文件搭建基础环境
SpringMVC与Struts的区别. 一.基础jar包 二.①DispatcherServlet,handelMapping,webAction(colltroller),ModelAndView ...
- 按比例吃CPU
前几天测试软件在多核上的性能,需要按照比例吃各个CPU,查了查资料,撸了下面一小段代码: #include <unistd.h> #include <stdlib.h> #in ...
- node.js 简易聊天室
效果图 首先开启server.js 打开服务器端,然后打开client.js,注册用户.打开一个注册一个用户,打开三个,注册三个用户. 接下来,小王发布一个广播消息,小李和小刘都收到了此消息. 接下来 ...
- python学习之模块:xlsxwriter
1.安装xlsxwriter模块 pip install xlsxwriter 2.使用 import xlsxwriter workbook = xlsxwriter.Workbook('hello ...
- kvm网卡配置
https://blog.51cto.com/quliren/2046001 https://blog.51cto.com/quliren/2045555 https://blog.csdn.net/ ...