Qt窗口——对话框

news2024/9/21 21:01:57

文章目录

    • 对话框
    • 自定义对话框
    • 对话框分类
    • 消息对话框QMessageBox
      • 使用示例
      • 自定义按钮
      • 快速构造对话框
    • 颜色对话框QColorDialog
    • 文件对话框QFileDialog
    • 字体对话框QFontDialog
    • 输入对话框QInputDialog

对话框

对话框可以理解成一个弹窗,用于短期任务或者简洁的用户交互

Qt内置对话框:

  • QFiledialog文件对话框
  • QColorDialog颜色对话框
  • QFontDialog字体对话框
  • QInputDialog输入对话框
  • QMessageBox消息框

继承QDialog:
在这里插入图片描述
在这里插入图片描述

实际开发都是创建额外的类,让额外的类继承QDialog

继承QMainWindow:

往ui文件里面添加一个按钮,设置槽,点击按钮就会弹出对话框:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    QDialog* dialog = new QDialog(this);
    dialog->setWindowTitle("弹窗");
    dialog->resize(300,400);
    dialog->show();
}

GIF 2024-9-21 12-33-50

内存泄漏问题:

点击一次按钮,就弹出一个对话框,即每次点击都会创建新的QDialog对象。

如果无数次点击这个按钮,就会产生无数次这个对象,进而引发内存泄漏。

如果将delete操作直接放入:

void MainWindow::on_pushButton_clicked()
{
    QDialog* dialog = new QDialog(this);
    dialog->setWindowTitle("弹窗");
    dialog->resize(300,400);
    dialog->show();
    delete dialog;
}

这样弹窗一闪而过,正确的做法是将delete和关闭按钮的信号关联起来。

Qt给QDialog设置了属性,通过设置属性,完成上述效果

void MainWindow::on_pushButton_clicked()
{
    QDialog* dialog = new QDialog(this);
    dialog->setWindowTitle("弹窗");
    dialog->resize(300,400);
    dialog->show();
    
    dialog->setAttribute(Qt::WA_DeleteOnClose);
}

自定义对话框

要想自定义对话框,就要继承QDialog创建类

纯代码:

创建一个C++类,继承自QDialog

image-20240921125947229

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QWidget>
#include<QDialog>
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget* parent);
    
    void handle();
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include<QPushButton>
#include<QLabel>
#include<QVBoxLayout>
Dialog::Dialog(QWidget* parent) : QDialog(parent)
{
    //创建控件加入dialog当中
    QVBoxLayout* layout = new QVBoxLayout();
    this->setLayout(layout);
    QLabel* label = new QLabel("对话框");
    QPushButton* button = new QPushButton("关闭");
    layout->addWidget(label);
    layout->addWidget(button);
    
    connect(button, &QPushButton::clicked, this, &Dialog::handle);
}

void Dialog::handle()
{
    this->close();
}
#include "dialog.h"
#include<QPushButton>
#include<QLabel>
#include<QVBoxLayout>
Dialog::Dialog(QWidget* parent) : QDialog(parent)
{
    //创建控件加入dialog当中
    QVBoxLayout* layout = new QVBoxLayout();
    this->setLayout(layout);
    QLabel* label = new QLabel("对话框");
    QPushButton* button = new QPushButton("关闭");
    layout->addWidget(label);
    layout->addWidget(button);
    
    connect(button, &QPushButton::clicked, this, &Dialog::handle);
}

void Dialog::handle()
{
    this->close();
}

mianwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<dialog.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    Dialog* dialog = new Dialog(this);
    
    dialog->resize(400, 300);
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();
}

GIF 2024-9-21 13-26-36

图形化:

创建新的ui文件:

image-20240921132926374

image-20240921135018533

对话框分类

  • 模态对话框: 表示弹出对话框的时候,此时用户无法操作父窗口,必须完成对话框内部的操作,关闭之后,才可进行操作父窗口
    一般用于关键的场合,用户必须做出决策
  • 非模态: 弹出对话框之后,用户可以操作父窗口
    上面写的对话框,都是非模态

QDialog::exec()表示模态

QDialog::show()表示非模态

消息对话框QMessageBox

弹出一个对话框,给用户显示消息,并让用户进行一个简单的选择

使用示例

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    //创建
    QMessageBox* messageBox = new QMessageBox();
    messageBox->setWindowTitle("对话框标题");
    messageBox->setText("对话框文本");
    messageBox->setIcon(QMessageBox::Warning);
    messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Save | QMessageBox::Cancel);
    messageBox->exec();
    //模态对话框一直处于阻塞状态,知道对话框关闭,使用可以直接在后面delete
    delete messageBox;
}

QMessageBox内置的图标:

image-20240921140634754

QMessageBox内置按钮:

image-20240921140702394

用户点击按钮的时候,可以通过exec的返回值(按钮枚举的值)来知道用户点击的是哪个按钮,从而执行了哪些操作

image-20240921140903918

自定义按钮

void MainWindow::on_pushButton_clicked()
{
    //创建
    QMessageBox* messageBox = new QMessageBox();
    messageBox->setWindowTitle("对话框标题");
    messageBox->setText("对话框文本");
    messageBox->setIcon(QMessageBox::Warning);
    //messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Save | QMessageBox::Cancel);
    
    QPushButton* button = new QPushButton("按钮", messageBox);
    messageBox->addButton(button, QMessageBox::AcceptRole);
    
    messageBox->exec();

    delete messageBox;
}

image-20240921142835383

快速构造对话框

QMessageBox提供了静态函数,可以快速构造对话框

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMessageBox>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    int ret = QMessageBox::warning(this, "标题", "文本", QMessageBox::Ok | QMessageBox::Cancel);
    if(ret == QMessageBox::Ok)
    {
        qDebug() << "Ok";   
    }
    else if(ret == QMessageBox::Cancel)
    {
        qDebug() << "Cancel";
    }
}

颜色对话框QColorDialog

QColorDialog颜色对话框的功能是允许用户选择颜色,相当于调色板,继承自QDialog

常用方法:

  • QColorDialog(QWidget* parent = nullptr)创建对象的同时设置父对象

  • QColorDialog(const QColor &initial, QWidget* parent = nullptr)创建对象的同时通过QColor对象设置默认颜色和父对象

  • void setCurrentColor(const QColor &color)设置当前颜色对话框

  • QColor getColor(const QColor &initial = Qt::white, QWidget *parent = nullptr, const QString &title = QString(), QColorDialog::ColorDialogOptions options = ColorDialogOptions())打开颜色对话框,返回一个QColor对象

    是一个静态函数,不必创建对话框对象就可以直接使用

    参数说明:
    initial设置默认颜色
    parent设置父对象
    title设置对话框标题
    options设置选项

  • void open(QObject *receiver, const char* member)打开颜色对话框

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QColorDialog>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    QColor color = QColorDialog::getColor(QColor(0, 255, 0), this, "选择颜色");
    qDebug() << color;
}

image-20240921150052826

这里的a表示alpha不透明度

后面的就表示RGB的值,范围是[0, 1]

1对应整数255;0对应整数0

修改窗口颜色:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QColorDialog>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    QColor color = QColorDialog::getColor(QColor(0, 255, 0), this, "选择颜色");
    qDebug() << color;
    //QSS方式设置
//    QString style = "background-color: rgb(" +
//            QString::number(color.red()) + ", "
//            + QString::number(color.green()) + ", "
//            + QString::number(color.blue()) + ");";
    char style[1024] = { 0 };
    sprintf(style, "background-color: rgb(%d, %d, %d)", color.red(), color.green(), color.blue());
    this->setStyleSheet(style);
}

GIF 2024-9-21 15-07-00

文件对话框QFileDialog

文件对话框用于应用程序需要打开一个外部文件或者将当前文件存储到指定的外部文件

常用方法:

  • 打开文件(一次打开一个文件)

    static QString getOpenFileName(QWidget *parent = nullptr,
                                   const QString &caption = QString(),
                                   const QString &dir = QString(),
                                   const QString &filter = QString(),
                                   QString *selectedFilter = nullptr,
                                   Options options = Options());
    
  • 打开多个文件(一次打开多个)

    static QStringList getOpenFileNames(QWidget *parent = nullptr,
                                        const QString &caption = QString(),
                                        const QString &dir = QString(),
                                        const QString &filter = QString(),
                                        QString *selectedFilter = nullptr,
                                        Options options = Options());
    
  • 保存文件

    static QString getSaveFileName(QWidget *parent = nullptr,
                                   const QString &caption = QString(),
                                   const QString &dir = QString(),
                                   const QString &filter = QString(),
                                   QString *selectedFilter = nullptr,
                                   Options options = Options());
    

    参数说明:

    1. parent父亲
    2. caption对话框标题
    3. dir默认打开路径
    4. filter文件过滤器
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFileDialog>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this);
    qDebug() << filePath;
}

void MainWindow::on_pushButton_2_clicked()
{
    QString filePath = QFileDialog::getSaveFileName(this);
    qDebug() << filePath;
}

此处的打开/保存的功能都是需要额外实现的,并不是说按下就打开/保存了

GIF 2024-9-21 15-17-51

字体对话框QFontDialog

QFontDialog用于让用户选择字体的属性

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFontDialog>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    bool ok = false;
    QFont font = QFontDialog::getFont(&ok);
    qDebug() << "ok = " << ok;
    qDebug() << font;
    
    //选择的字体相关属性设置到按钮上
    ui->pushButton->setFont(font);
}

GIF 2024-9-21 15-28-32

输入对话框QInputDialog

QInputDialog允许用户输入一个具体的数据(整数、浮点数、字符串)

常用方法:

  • 双精度浮动型输入数据对话框

    static double getDouble(QWidget *parent, const QString &title, const QString &label, double value,
                            double minValue, double maxValue, int decimals, bool *ok, Qt::WindowFlags flags,
                            double step);
    
  • 整数输入数据对话框

    static int getInt(QWidget *parent, const QString &title, const QString &label, int value = 0,
                      int minValue = -2147483647, int maxValue = 2147483647,
                      int step = 1, bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
    
  • 选择条目输入型数据框

    static QString getItem(QWidget *parent, const QString &title, const QString &label,
                           const QStringList &items, int current = 0, bool editable = true,
                           bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(),
                           Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
    

    参数说明:

    1. parent父亲
    2. title对话框标题
    3. label对话框标签
    4. items可选择条目
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QInputDialog>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    int ret = QInputDialog::getInt(this, "整数输入对话框", "输入整数: ");
    qDebug() << ret;
}

void MainWindow::on_pushButton_2_clicked()
{
    QStringList items;
    items.push_back("123");
    items.push_back("456");
    items.push_back("789");
    QString item = QInputDialog::getItem(this, "条目输入对话框", "输入条目:", items);
    qDebug() << item;
}

GIF 2024-9-21 15-40-17

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AIoT智能工控板

在当今竞争激烈的商业环境中&#xff0c;企业需要强大的科技力量来助力腾飞&#xff0c;AIoT智能工控板就是这样的力量源泉。 其领先的芯片架构设计&#xff0c;使得主板的性能得到了极大的提升。无论是数据的处理速度、图形的渲染能力&#xff0c;还是多任务的并行处理能力&a…

【Linux笔记】虚拟机内Linux内容复制到宿主机的Window文件夹(文件)中

一、共享文件夹 I、Windows宿主机上创建一个文件夹 目录&#xff1a;D:\Centos_iso\shared_files II、在VMware中设置共享文件夹 1、打开VMware Workstation 2、选择需要设置的Linux虚拟机&#xff0c;点击“编辑虚拟机设置”。 3、在“选项”标签页中&#xff0c;选择“共…

【BetterBench博士】2024年中国研究生数学建模竞赛 E题:高速公路应急车道紧急启用模型 问题分析

2024年中国研究生数学建模竞赛 E题&#xff1a;高速公路应急车道紧急启用模型 问题分析 更新进展 【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析 【BetterBench博士】2024年中国研究生数学建模竞赛 E题&#xff1a;高速公路应急车道紧急启用…

Remix在SPA模式下,出现ErrorBoundary错误页加载Ant Design组件报错,不能加载样式的问题

Remix是一个既能做服务端渲染&#xff0c;又能做单页应用的框架&#xff0c;如果想做单页应用&#xff0c;又想学服务端渲染&#xff0c;使用Remix可以降低学习成本。最近&#xff0c;在学习Remix的过程中&#xff0c;遇到了在SPA模式下与Ant Design整合的问题。 我用Remix官网…

简单多状态dp第三弹 leetcode -买卖股票的最佳时机问题

309. 买卖股票的最佳时机含冷冻期 买卖股票的最佳时机含冷冻期 分析: 使用动态规划解决 状态表示: 由于有「买入」「可交易」「冷冻期」三个状态&#xff0c;因此我们可以选择用三个数组&#xff0c;其中&#xff1a; ▪ dp[i][0] 表示&#xff1a;第 i 天结束后&#xff0c…

探索AI编程新时代:GitHub Copilot如何重塑开发者工作效率

在当今技术瞬息万变的时代&#xff0c;软件开发者们每天都在努力寻找更高效的编程方法。面对繁忙的工作日程和不断增加的项目压力&#xff0c;如何在编码过程中大幅提升效率成为了一个备受关注的话题。在众多工具中&#xff0c;GitHub Copilot以其强大的AI驱动能力脱颖而出&…

菜鸟也能轻松上手的Java环境配置方法

初学者学习Java这么编程语言&#xff0c;第一个难题往往是Java环境的配置&#xff0c;今天与大家详细地聊一聊&#xff0c;以便大家能独立完成配置方法和过程。 首先&#xff0c;找到“JDK”&#xff0c;点击“archive”&#xff1a; 向下滑&#xff0c;在“previous java rel…

HTTPS:构建安全通信的基石

HTTPS&#xff08;Hypertext Transfer Protocol Secure&#xff09;&#xff0c;作为互联网上安全通信的基石&#xff0c;通过在HTTP基础上引入SSL/TLS协议层&#xff0c;实现了数据传输的加密&#xff0c;确保了信息的机密性、完整性和真实性。这一过程涉及多个精细设计的步骤…

【EasyBlog】基于React+AntD+NextJS+NestJS+MySQL打造的开源博客系统

Github项目地址&#xff1a;https://github.com/fecommunity/easy-blog&#xff0c; 欢迎Star。 Easy-Blog Easy-Blog 是一套集成文章发表、页面创建、知识库管理、博客后台管理等功能于一体的博客系统。 首页-浅色主题 首页-暗黑主题 文章阅读 后台管理 ✨ 特性 &#…

公司网站改版时,需要注意哪些细节?

在公司网站改版时&#xff0c;需要注意的细节非常多&#xff0c;这些细节将直接影响到网站的用户体验、SEO效果以及整体品牌形象。以下是一些关键的注意事项&#xff1a; 明确改版目标&#xff1a; 在改版前&#xff0c;要明确改版的目标是什么&#xff0c;比如提升用户体验、增…

【AcWing】873. 欧拉函数

#include<iostream> using namespace std;int main(){int n;cin>>n;while(n--){int x;cin>>x;int resx;for(int i2;i<x/i;i){if(x%i0){//resres*(1-1/i);整数1/i等于0&#xff0c;算不对且会溢出//以下几种都能ac//resres/i*(i-1);i*(1-1/i)i-1&#xff0…

通过标签实现有序:优化你的 FastAPI 生成的 TypeScript 客户端

在软件开发的世界里&#xff0c;API 客户端代码的质量直接影响着应用程序的性能和可维护性。随着项目规模的扩大&#xff0c;自动化生成的代码往往变得臃肿且难以管理。但幸运的是&#xff0c;通过一系列的优化策略&#xff0c;我们可以显著提升这些代码的优雅与效能。在本文中…

计算机网络(八) —— Udp协议

目录 一&#xff0c;再谈端口号 1.1 端口号 1.2 netsta命令 二&#xff0c;UDP协议 2.1 关于UDP 2.2 Udp协议格式 2.3 Udp协议特点 2.4 Udp的缓冲区 一&#xff0c;再谈端口号 http协议本质是“请求 - 响应”形式的协议&#xff0c;但是应用层需要先将数据交给传输层&…

2024/9/21 408 20题

a b 58-130-180-199-42-15&#xff1a;c d a 184-182-187-176-19941 c d a a c b d c a c b c c c

12V转100V低压升高压DC/DC电源GRB12-100D-100mA-Uz(0-3V)

特点 ● 效率高达75%以上 ● 1*2英寸标准封装 ● 单电压输出 ● 超高性价比 ● 电压控制输出,输出电压随控制电压的变化而线性变压 ● 工作温度: -40℃~75℃ ● 阻燃封装&#xff0c;满足UL94-V0 要求 ● 温度特性好 ● 可直接焊在PCB 上 应用 GRB 系列模块电源是一…

深度学习笔记17_TensorFlow实现咖啡豆识别

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 | 接辅导、项目定制 一、我的环境 1.语言环境&#xff1a;Python 3.9 2.编译器&#xff1a;Pycharm 3.深度学习环境&#xff1a;TensorFlow 2.10.0 二、GPU设置…

linux操作系统的基本命令

1.linux下的文件系统 在linux操作目录下没有像window操作系统下盘符的概念,只有一个根目录/,所有文件目录都在它的下面 linux的目录结构: 在Linux系统中: 文件都从跟目录开始的,用/表示文件名称区分大小写路径都是以/俩进行分隔(windown用\分隔)以.开头的文件为隐藏文件 Li…

Java反序列化利用链篇 | CC6链分析(通用版CC链)

文章目录 CC6和CC1之间的区别CC6的调用链构造CC6的payload完成TiedMapEntry.getValue()完成TiedMapEntry.hashCode()完成HashMap.hash()及HashMap.readObject()解决hash()方法提前触发的问题 系列篇其他文章&#xff0c;推荐顺序观看~ Java反序列化利用链篇 | JdbcRowSetImpl利…

LeetCode[中等] 215. 数组中的第 K 个最大元素

给定整数数组 nums 和整数 k&#xff0c;请返回数组中第 k 个最大的元素。 请注意&#xff0c;你需要找的是数组排序后的第 k 个最大的元素&#xff0c;而不是第 k 个不同的元素。 你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。 思路&#xff1a;基于快排改进的快速…

【AI算法岗面试八股面经【超全整理】——深度学习】

AI算法岗面试八股面经【超全整理】 概率论【AI算法岗面试八股面经【超全整理】——概率论】信息论【AI算法岗面试八股面经【超全整理】——信息论】机器学习【AI算法岗面试八股面经【超全整理】——机器学习】深度学习CVNLP 目录 1、激活函数2、Softmax函数及求导3、优化器 1、…