Qt基础知识

2 篇文章 0 订阅

Qt:一般指qt框架

其工具主要有:
Qt Creator:是跨平台的集成开发环境(IDE)
Qt Designer:图形界面设计工具

Qt项目基本的文档结构

结构图片

.pro文件(项目配置文件)

#-------------------------------------------------
#
# Project created by QtCreator 2023-04-19T09:55:31
#
#-------------------------------------------------
#项目配置文件
QT       += core gui texttospeech#指定当前项目使用的类库(core库、gui界面库)

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets #运用桌面类库

TARGET = test01 #目标
TEMPLATE = app #模板,应用程序

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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 \
        widget.cpp
#头文件
HEADERS += \
        widget.h
#界面文件
FORMS += \
        widget.ui

.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget> //Qt框架类库提供的类,该类提供了gui界面的绘制,其他信号、事件的处理

namespace Ui {
class Widget; //定义一个不完整类
}

class Widget : public QWidget
{
    Q_OBJECT//编译宏元对象,告诉当前编译器当前类中能使用信号与槽,也就是识别signal与slot

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();//斜体为虚析构函数

private:
    Ui::Widget *ui;//使用名字空间中的Widget类,定义一个ui指针,用来操作gui界面
};

#endif // WIDGET_H

.cpp文件(界面组件控制)

#include "widget.h"
#include "ui_widget.h"//qmake根据pro文件和ui界面文件构建的头文件

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)//使用的是ui_Widget.h中的Ui名字空间
{
    ui->setupUi(this);//将gui界面与当前的类关联,默认设置gui界面的对象名、窗口标题、窗口大小,主要依赖ui文件
}

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

main.cpp(运行程序)

#include "widget.h"
#include <QApplication>
#include <QDebug>
#include <QTextToSpeech>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//提供gui界面所有事件、信号与槽的处理方法
    Widget w;
    w.show();//显示界面,根据w对象中的setupUi来绘制页面

    qDebug() << "Date";//自动换行
    qDebug("hello world");

    QTextToSpeech speech;
    speech.setVolume(1);
    speech.say("hello world");
    return a.exec();//事件循环的函数,不停的检测gui界面发生的事件
}

.ui文件(项目的界面设计)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>619</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

槽函数重载

//qoverload方法
 struct Foo {
          void overloadedFunction();
          void overloadedFunction(int, QString);
      };
      ... qOverload<>(&Foo::overloadedFunction)
      ... qOverload<int, QString>(&Foo::overloadedFunction)
//转换函数
 struct Foo {
          void overloadedFunction();
          void overloadedFunction(int, QString);
      };
	...static_cast<void (Foo::*)()>(&Foo::overloadedFunction)
	...static_cast<void (Foo::*)(int,Qstring)>(&Foo::overloadedFunction)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值