Mstering QT5 chapter1

本文介绍如何在Qt Creator中设置C++14,并演示了基本的Qt项目配置过程,包括.pro文件、源代码文件、头文件和界面文件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

涉及到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文件。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值