QT入门介绍

简单使用

1、创建工程项目

在这里插入图片描述

创建类的基类: 带菜单栏的窗口、空白窗口、对话框窗口

2、QT中文乱码解决

在这里插入图片描述

3、按钮属性

#include <QPushButton>

QPushButton *button = new QPushButton;
// button->show();
button->setParent(this);  // 设置按钮的父对象为窗口 这样按钮就在窗口上显示
button->move(100,100);
button->setFixedSize(40,20);
button->setText("点击");

在这里插入图片描述

4、信号与槽、自定义信号和槽、信号连接信号

student.h

public slots:
    void treat();
};

student.cpp

#include <QDebug>

void Student::treat(){
    qDebug()<<"打印内容";
}

teacher.h

signals:
    void hungury();

teacher.cpp


widget.h

#include "teacher.h"
#include "student.h"

public:
    
    void ClassOver();
    Teacher *tea;
    Student * stu;

widget.cpp

#include "widget.h"
#include <QPushButton>
#include "teacher.h"
#include "student.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("第一个窗口");
    QPushButton *button = new QPushButton;
    // button->show();
    button->setParent(this);
    button->move(100,100);
    button->setFixedSize(40,20);
    button->setText("点击");

    // 信号与槽
    // connect(button,&QPushButton::clicked,this,&Widget::close);
    // connect(button,&QPushButton::clicked,this,&Widget::ClassOver);
		// 第二种调用方式

    this->tea = new Teacher(this);
    this->stu = new Student(this);
    connect(tea,&Teacher::hungury,stu,&Student::treat);
    // ClassOver();   // 第一种调用方式

    connect(button,&QPushButton::clicked,tea,&Teacher::hungury);
		// 第三种调用方式
}
void Widget::ClassOver(){
    emit tea->hungury();
}

Widget::~Widget()
{

}

5、重载自定义信号与槽

student.h

public slots:
    void treat();
    void treat(QString food);
};

student.cpp

#include <QDebug>

void Student::treat(){
    qDebug()<<"打印内容";
}
void Student::treat(QString food){
    // qDebug()<<"打印内容" << food;     // 打印内容 "食物"

    // QString --> QByteArray --> char *
    qDebug().nospace()<<"打印内容" << food.toUtf8().data();   // 打印内容食物

}

teacher.h

signals:
    void hungury();
    void hungury(QString food);

teacher.cpp


widget.h

#include "teacher.h"
#include "student.h"

public:
    
    void ClassOver();
    Teacher *tea;
    Student * stu;

widget.cpp

#include "widget.h"
#include <QPushButton>
#include "teacher.h"
#include "student.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("第一个窗口");
    QPushButton *button = new QPushButton;
    // button->show();
    button->setParent(this);
    button->move(100,100);
    button->setFixedSize(40,20);
    button->setText("点击");

    // 信号与槽

    this->tea = new Teacher(this);
    this->stu = new Student(this);

    void (Teacher::*teachesignal)(QString) = &Teacher::hungury;
    void (Student::*studentslot)(QString) = &Student::treat;

    //connect(tea,&Teacher::hungury,stu,&Student::treat);
    connect(tea,teachesignal,stu,studentslot);

    ClassOver();

    // connect(button,&QPushButton::clicked,tea,&Teacher::hungury);
}
void Widget::ClassOver(){
    emit tea->hungury("食物");
}

Widget::~Widget()
{

}

6、Lambda表达式

#include "widget.h"
#include <QPushButton>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("第一个窗口");
    QPushButton *button = new QPushButton;

    button->setParent(this);
    button->move(100,100);
    button->setFixedSize(40,20);
    button->setText("点击");

    connect(button,&QPushButton::clicked,this,[=] () {
        qDebug() <<"按钮被按下";
    });

}

Widget::~Widget()
{

}

7、菜单栏的创建

stu.h

public slots:
    void treat();
};

stu.cpp

void Stu::treat(){
    qDebug()<<"点击菜单项--打印内容";
}

mainwindow.h

#include "stu.h"

public:
    

    void ClassOver();
    Stu * stu;
};

mainwindow.cpp

#include "mainwindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include "stu.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(500,300);

    // 创建菜单栏
    // QMenuBar *menubar = new QMenuBar(this);
    QMenuBar *menubar = menuBar();    // 成员函数方式创建菜单栏

    this->setMenuBar(menubar);
    // 创建菜单
    QMenu *menu1 = new QMenu("文件");
    QMenu *menu2 = new QMenu("编辑");
    QMenu *menu3 = new QMenu("构建");
    menubar->addMenu(menu1);
    menubar->addMenu(menu2);
    menubar->addMenu(menu3);
    // 创建菜单项
    QAction *act1 = new QAction("打开文件");
    QAction *act2 = new QAction("另存为");
    QAction *act3 = new QAction("打印");
    menu1->addAction(act1);
    menu1->addAction(act2);
    menu1->addAction(act3);

    this->stu = new Stu(this);
    connect(act3,&QAction::triggered,this,&MainWindow::ClassOver);
    // connect(act3,&QAction::triggered,this,&MainWindow::close);
}
void MainWindow::ClassOver(){
    emit stu->treat();
}
MainWindow::~MainWindow()
{

}

8、工具栏的创建

mainwindow.cpp

#include <QToolBar>

		// 创建工具栏
    QToolBar *toolbar = new QToolBar(this);
    this->addToolBar(toolbar);
    QAction *act4 = new QAction("工具");
    toolbar->addAction(act1);
    toolbar->addAction(act2);
    toolbar->addAction(act3);
    toolbar->addAction(act4);
    // 指定工具栏停靠区域
    //toolbar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

    // 设置工具栏不可移动
    toolbar->setMovable(false);

    // 设置工具栏的浮动状态
    // toolbar->setFloatable(false);

9、状态栏的创建

mainwindow.cpp

#include <QStatusBar>
#include <QLabel>

		// 创建状态栏
    QStatusBar *statusbar = statusBar();
    // QStatusBar *statusbar = new QStatusBar(this);
    this->setStatusBar(statusbar);
    // 增加临时信息
    statusbar->showMessage("加载中..",2000);
    // 增加正式信息(一般位于状态栏左侧)
    // QLabel *label1 = new QLabel("打开文件", this);
    // statusbar->addWidget(label1);
    // 增加永久信息(一般位于状态栏右侧)
    QLabel *label2 = new QLabel("www.baidu.com", this);
    statusbar->addPermanentWidget(label2);

10、铆接部件

#include<QDockWidget>

		// 创建铆接部件
    QDockWidget *dockwidget = new QDockWidget("第一个",this);
    this->addDockWidget(Qt::TopDockWidgetArea,dockwidget);
    QDockWidget *dockwidget2 = new QDockWidget("第二个",this);
    this->addDockWidget(Qt::TopDockWidgetArea,dockwidget2);

11、核心部件(中心部件)

#include <QTextEdit>

		// 创建记事本作为核心部件
    QTextEdit *edit = new QTextEdit("文本编辑器",this);
    this->setCentralWidget(edit);

12、资源文件

QT资源系统是一个跨平台的资源机制,用于将程序运行时所需要的资源以二进制的形式存储于可执行文件内部

新增图片资源文件,设置别名,给菜单项增加图片

		
		// 给菜单项增加图片
    QPixmap pix;
    // 选择图片
    pix.load(":/open");
    act1->setIcon(QIcon(pix));

    QPixmap pix1;
    pix1.load(":/open1");
    act2->setIcon(QIcon(pix1));

13、背景图设置

    this->setFixedSize(800,600);

    this->setAutoFillBackground(true);  // 设置自动填充背景
    // 创建图片控件
    // QPixmap pix21;
    // pix21.load(":/open3");

		// 创建图片控件 另外一种方式
    QPixmap pix21 = QPixmap(":/open3").scaled(this->size());

    QPalette palette;
    palette.setBrush(QPalette::Background, QBrush(pix21));
    this->setPalette(palette);

14、QDialog对话框

  • 模态对话框
  • 非模态对话框
#include <QDialog>

//    QDialog dialog;
//    dialog.setWindowTitle(tr("hello dialog!"));
//    dialog.exec();   // 模态对话框

    QDialog *dialog = new QDialog;   // 对话框创建要在堆创建
    dialog->setAttribute(Qt::WA_DeleteOnClose);  // 设置对话框关闭,自动销毁对话框
    dialog->setWindowTitle(tr("hello dialog!"));
    dialog->show();  // 非模态对话框

15、ui的简单使用

ui->setupUi(this);
connect(ui->actionf,&QAction::triggered,this, &MainWindow::close);

16、标准对话框

文件对话框

帮助手册—搜索qfiledialog—static public members—getopenfilename

在这里插入图片描述


mainwindow.h

// ui里面-按钮右键选择-转到槽  会自动创建如下slots
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

mainwindow.cpp

#include <QFileDialog>
#include <QDebug>

void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("打开文件"),"./",tr("Images (*.png *.xpm *.jpg);;Text (*.txt)"));
    if(!fileName.isEmpty())
        ui->plainTextEdit->appendPlainText(fileName);  // 如果文件名称不为空  就设置文件名称加到文本框内

}

void MainWindow::on_pushButton_2_clicked()
{
    QStringList files = QFileDialog::getOpenFileNames(
                              this,
                              "打开文件",
                              "./",
                              "Images (*.png *.xpm *.jpg)");
    for(int i=0;i<files.count();i++)
    {
        qDebug()<<files.at(i);
    }
}

颜色对话框


// ui里面-新建一个按钮-右键选择-转到槽

#include <QColorDialog>

void MainWindow::on_pushButton_3_clicked()
{
    // 生成颜色对话框,选择颜色,设置编辑器字体颜色 
    QPalette pal = ui->plainTextEdit->palette();  // 获取现有palette 调色板
    QColor initColor =pal.color(QPalette::Text);  // 现有文字颜色
    QColor color = QColorDialog::getColor(initColor,this,"选择颜色");
    // 判断选择的颜色是否有效,如果有效设置编辑器字体颜色
    if(color.isValid()){
        pal.setColor(QPalette::Text,color);
        ui->plainTextEdit->setPalette(pal);
    }
}

选择字体对话框

// ui里面-新建一个按钮-右键选择-转到槽

#include <QFontDialog>

void MainWindow::on_pushButton_4_clicked()
{
    bool ok;
     // QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);

    QFont initFont = ui->plainTextEdit->font(); // 获取原有文本框字体
     QFont font = QFontDialog::getFont(&ok, initFont, this);
     if (ok) {
        ui->plainTextEdit->setFont(font);
     }

}

消息对话框

// ui里面-新建一个按钮-右键选择-转到槽

#include <QMessageBox>

void MainWindow::on_pushButton_5_clicked()
{
    // QMessageBox::critical(this, "错误消息", "程序出现异常,请联系管理员!");
    // QMessageBox::warning(this,"警告消息", "警告错误,服务器内部错误!");
    // QMessageBox::information(this,"消息对话框", "保存成功,请继续...",QMessageBox::Ok,QMessageBox::NoButton);
    QMessageBox::StandardButton result;
    result=  QMessageBox::question(this, "选择消息框", "文件已修改,是否保存",QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
    if (result == QMessageBox::Yes)
        qDebug() <<"在保存";
    else if(result == QMessageBox::No)
        qDebug() <<"不保存";
    else
        qDebug() <<"取消操作";

}

输入对话框

// ui里面-新建一个按钮-右键选择-转到槽

#include <QInputDialog>

void MainWindow::on_pushButton_6_clicked()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("输入文字对话框"),
                                       tr("请输入密码:"), QLineEdit::Normal,
                                       "123456", &ok);
    if (ok && !text.isEmpty())
      ui->plainTextEdit->appendPlainText(text);
}

17、布局、登录页面切主页面、主页面返回登录页面

form.h

signals:
    void back(void);

form.cpp

// connect(ui->pushButton,&QPushButton::clicked,this, );
    connect(ui->pushButton,SIGNAL(clicked(bool)),this, SIGNAL(back()));

widget.h

#include "form.h"

private:
    

    Form *new_window;
};

widget.cpp

 		this->new_window = new Form();

    connect(ui->pushButton,&QPushButton::clicked,this, [=](){
            this->hide();
            this->new_window->show();
    });

    connect(this->new_window, &Form::back,this, [=](){

        this->new_window->hide();
        this->show();
    });

在这里插入图片描述

form.ui

widget.ui

18、QLabel标签

设置文本

#include <QLabel>

		QLabel *label = new QLabel(this);
    label->setText("标签");

设置html

  	QLabel *label = new QLabel(this);
    label->setText("<h1><a href=\"https://www.baidu.com\">百度</a></h1>");
    label->setOpenExternalLinks(true);  // 设置用户点击链接后自动打开链接

设置图片

  	QPixmap pix;
    pix.load(":/open");
    ui->label_img->setPixmap(pix);

设置动画

#include <QMovie>

    QMovie *movie = new QMovie(":/gif");
    ui->label_2->setMovie(movie);
    movie->start();

19、QLineEdit

#include <QLineEdit>
#include <QDebug>

		QLineEdit *edit = new QLineEdit(this);
    edit->setText("这是代码创建的编辑框");
    QString str = edit->text();
    qDebug() <<str;
  	QLineEdit *edit1 = new QLineEdit(this);
    edit1->setText("这是代码创建的编辑框");
    QString str1 = edit1->text();
    edit1->setEchoMode(QLineEdit::Password);  // 密码模式
    qDebug() <<str1;

20、自定义控件

重复性使用的窗口或者窗口中的模块,封装成自定义控件。

仅创建一次,使用时提升。

举例:

form.ui

在这里插入图片描述

form.cpp

联动上面两个组件的信号与槽代码示例

  	connect(ui->spinBox,SIGNAL(valueChanged(int)),
            ui->horizontalSlider,SLOT(setValue(int)));
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
            ui->spinBox,SLOT(setValue(int)));

widget.ui

将容器Widget提升为Form

在这里插入图片描述

然后在widget.ui里面加一个按钮组件,设置:点击按钮可以控制进度条到一半的位置。

form.h

public slots:
    void setNum();

form.cpp

void Form::setNum(){
    ui->horizontalSlider->setValue(50);
}

widget.cpp

connect(ui->pushButton, SIGNAL(clicked(bool)), ui->widget,SLOT(setNum()));

21、栈容器的使用案例

widget.ui

  • 加三个按钮,加一个Stacked Widget栈容器。
  • 容器内三个page,提升为Form1,Form2,Form3

widget.cpp

    ui->stackedWidget->setCurrentIndex(0);

    connect(ui->pushButton, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->pushButton_2, &QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->pushButton_3,&QPushButton::clicked, this, [=](){
        ui->stackedWidget->setCurrentIndex(2);
    });

在这里插入图片描述

qt 是一个跨平台的的C++图形用户界面应用程序框架
QTCreator
下载地址:https://download.qt.io/new_archive/qt/5.8/5.8.0/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/429705.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【go从入门到精通】go环境安装和第一个经典程序

go下载和环境变量配置 下载地址 Go官网下载地址&#xff1a;https://golang.org/dl/All releases - The Go Programming Languagehttps://golang.org/dl/ 然后根据自己的系统环境来选择不同的安装包下载&#xff0c;下面我分别针对不同环境进行说明&#xff08;大家可以根据自…

Rabbitmq消息丢失-生产者消息丢失(一)

说明&#xff1a;消息生产者在将数据发送到Mq的时候&#xff0c;可能由于网络等原因造成数据投递失败。 消息丢失大致分三种&#xff1a;这里说的是生产者消息丢失&#xff01; 分析原因&#xff1a; 1.有没有一种可能&#xff0c;我刚发送消息&#xff0c;消息还没有到交换…

超全Chat GPT论文修改指令

文献综述指令润色修改指令论文选题指令论文大指令研究理论指令论文致谢指令参考文献指令论文润色整体逻辑论文整体优化提问指令 1&#xff0e;文献综述指令 请你帮我写一份关于&#xff08;研究主题&#xff09;的文献综述。我的论文选题方向是 XXXX &#xff0c;我已经找到了…

JS逆向进阶篇【去哪儿旅行登录】【下篇-逆向Bella参数JS加密逻辑Python生成】

目录&#xff1a; 每篇前言&#xff1a;引子——本篇目的1、 代码混淆和还原&#xff08;1&#xff09;单独替换&#xff1a;&#xff08;2&#xff09;整个js文件替换&#xff1a; 2、算法入口分析3、 深入分析&#xff08;0&#xff09;整体分析&#xff1a;&#xff08;1&am…

前后端分离项目Docker部署指南(上)

目录 前言 一.搭建局域网 1.搭建net-ry局域网&#xff0c;用于部署若依项目 2.注意点 二.安装redis 创建目录 将容器进行挂载 ​编辑 测试是否安装成功 ​编辑 三. 安装MySQL 创建文件夹 上传配置文件并且修改 .启动MySQL容器服务 充许远程连接 四.部署后端 使用…

linux 交叉编译curl(+openssl)

一、交叉编译openssl 参考博客&#xff1a;点击跳转 二、交叉编译curl 1、源码下载 地址&#xff1a;点击跳转 2、配置 CPPFLAGS"-I/home/gui/gui/openssl/build_arm/include" LDFLAGS"-L/home/gui/gui/openssl/build_arm/lib" LIBS"-ldl" \ …

Android之Handler原理解析与问题分享

一、Handler运行原理剖析 1.关系剖析图 如果把整个Handler交互看做一个工厂&#xff0c;Thread就是动力MessageQueue是履带Looper是转轴Loooper的loop方法就是开关&#xff0c;当调用loop方法时整个工厂开始循环工作&#xff0c;处理来自send和post提交到MessageQueue的消息&a…

使用Javassist 在android运行时生成类

序言 最近在写框架&#xff0c;有一个需求就是动态的生成一个类&#xff0c;然后查阅了相关文献&#xff0c;发现在android中动态生成一个类还挺麻烦。因次把一些内容分享出来&#xff0c;帮助大家少走弯路。 方案一 DexMaker DexMaker 是一个针对 Android 平台的库&#xf…

游戏引擎用什么语言开发上层应用

现在主流的游戏引擎包括&#xff1a; 1、Unity3D&#xff0c;C#语言&#xff0c;优点在于支持几乎所有平台 丹麦创立的一家公司&#xff0c;现已被微软收购。在中国市场占有率最高&#xff0c;也是社群很强大&#xff0c;一般解决方案也能在网上找到&#xff0c;教程丰富。物理…

.md转pdf

1、使用vscode安装Markdown PDF Markdown PDF 打开预览转pdf,同目录下自动生成pdf文件

稀碎从零算法笔记Day5-LeetCode:多数元素

题型&#xff1a;数组、计数、排序、STL函数、查找众数 链接&#xff1a;169. 多数元素 - 力扣&#xff08;LeetCode&#xff09; 来源&#xff1a;LeetCode 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 题目描述 给定一个大小为 n …

30分钟做200多张报表的金蝶云星空BI方案来了

曾经一张报表都要做好久&#xff0c;但现在&#xff0c;200多张的BI数据分析报表只需30分钟就能完成&#xff01;BI智能数据分析的高效性在这一刻具象化了。奥威-金蝶云星空BI方案&#xff0c;一套注册、下载、执行&#xff0c;即见效果的标准化BI数据分析方案。 30分钟&#…

【S32DS报错】-7-程序进入HardFault_Handler,无法正常运行

【S32K3_MCAL从入门到精通】合集&#xff1a; S32K3_MCAL从入门到精通https://blog.csdn.net/qfmzhu/category_12519033.html 问题背景&#xff1a; 在S32DS IDE中使用PEmicro&#xff08;Multilink ACP&#xff0c;Multilink Universal&#xff0c;Multilink FX&#xff09…

智能驾驶规划控制理论学习06-基于优化的规划方法之数值优化基础

目录 一、优化概念 1、一般优化问题 2、全局最优和局部最优 二、无约束优化 1、无约束优化概述 2、梯度方法 通用框架 线性搜索 回溯搜索 3、梯度下降 基本思想 实现流程 ​4、牛顿法 基本思想 实现流程 5、高斯牛顿法 6、LM法&#xff08;Le…

java数据结构与算法刷题-----LeetCode637. 二叉树的层平均值

java数据结构与算法刷题目录&#xff08;剑指Offer、LeetCode、ACM&#xff09;-----主目录-----持续更新(进不去说明我没写完)&#xff1a;https://blog.csdn.net/grd_java/article/details/123063846 文章目录 1. 广度优先2. 深度优先 解题思路&#xff1a;时间复杂度O(n)&am…

网络基础(二)

目录 再谈"协议" 序列化 JSON 网络版计算器 HTTP协议 认识URL urlencode和urldecode HTTP协议格式 telnet指令 stat函数 struct stat类型 stringstream类型 wget指令 HTTP的方法 HTTP的状态码 传输层 再谈端口号 端口号范围划分 认识知名端口号(W…

深度学习_16_权重衰退调整过拟合

所谓过拟合即模型复杂度较高&#xff0c;但用于训练数据集过于简单&#xff0c;最后导致模型将过多无用渣质作为学习对象 这个在上篇 深度学习_15_过拟合&欠拟合 已经详细介绍&#xff0c;以下便不再赘述。 上篇提到要想解决过拟合现象可以试着降低模型复杂度&#xff0c…

Python web框架fastapi中间件与CORS详细教学

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;Fastapi 景天的主页&#xff1a;景天科技苑 文章目录 fastapi中间件与CORS1、中间件1.创建中间件方法2.中间件里面添加响应头…

抖音视频评论批量采集软件|视频下载工具

《轻松搞定&#xff01;视频评论批量采集软件&#xff0c;助您高效工作》 在短视频这个充满活力和创意的平台上&#xff0c;了解用户评论是了解市场和观众心声的重要途径之一。为了帮助您快速获取大量视频评论数据&#xff0c;我们推出了一款操作便捷、功能强大的软件&#xff…

第一弹:Flutter安装和配置

目标&#xff1a; 1&#xff09;配置Flutter开发环境 2&#xff09;创建第一个Flutter Demo项目 Flutter中文开发者网站&#xff1a; https://flutter.cn/ 一、配置Flutter开发环境 Flutter开发环境已经提供集成IDE开发环境&#xff0c;因此需要配置开发环境的时候&#xf…
最新文章