Qt开发第一讲

一、Qt项目里面有什么?

对各个文件的解释:

Empty.pro文件

QT       += core gui   # 要引入的Qt模块,后面学习到一些内容的时候可能会修改这里
#这个文件相当于Linux里面的makefile文件。makefile其实是一个非常古老的技术了。
#qmake搭配.pro起到作用和makefile是类似的。
#Qt creator 把这个过程中编译的细节都封装好了,不需要过多的关注,只需要点击运行按钮,就直接编译运行了
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11 #编译选项

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \          
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui
#描述了当前项目中,参与构建的文件都有啥(编译器要编译哪些文件)
#这个地方不需要手动修改,Qt Creator帮我们自动维护好

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

# 项目的工程文件,也是qmake构建时候的重要依据。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//要想使用这个类,就需要包含对应的头文件。Qt
//的设定,使用Qt中内置的类,包含的头文件的名字就是和类名一致的
#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
//创建项目时,选择的父类。
class MainWindow : public QMainWindow
{
    Q_OBJECT  //是一个Qt内置的宏,宏本质上是文本替换
    //Qt中有一个非常核心的机制,信号和槽,如果某个类想使用信号和槽,就需要引入
    //Q_OBJECT这个宏

public:
    //Qt中引入了对象树机制,创建的Qt的对象,就可以把这个对象给挂到对象树上,往树上挂的时候
    //就需要指定父节点。
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui; //和form file密切相关。
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"

#include <QApplication>
//入口函数,命令行参数的个数,argv命令行参数。
int main(int argc, char *argv[])
{
    //编写一个Qt的图形化界面程序,一定需要有QApplication对象!
    QApplication a(argc, argv);
    //创建一个控件对象,并显示出来。它的父类是QWidget,都是QWidget提供的。
    MainWindow w;
    //show方法让空间显示出来
    w.show();
    //让空间隐藏
    //w.hide();
    return a.exec(); //让程序执行起来。在linux中我们学过exec*系列函数,把可执行文件中的代码和数据,替换到当前进程中
    //但在这里,只是名字恰好一样
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
//构造和析构的实现
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow) //把form file生成的界面和当mainwindow关联起来
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>  //xml格式的文件,和html相似。
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

二、Qt Hello World 程序

两种方式实现hello world

1、通过图形化的方式,在界面上创建出一个空间,显示hello world

Qt Designer右上角,通过树形结构,显示出了当前界面上都有哪些控件。

2、通过纯代码的方式,通过编写代码,在界面上创建控件,显示hello world

#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QLabel* label = new QLabel(this);
    label->setText("hello world");
}

Widget::~Widget()
{
    delete ui;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值