Qt之标准对话框(QMessageBox、QFileDialog)

举报
人才程序员 发表于 2023/07/26 11:34:03 2023/07/26
【摘要】 @TOC 前言Qt为开发者提供了一些可复用的对话框,他对我们的开发是很重要的。下面我们就来学习提示:以下是本篇文章正文内容,下面案例可供参考 如何学习标准对话框其实在Qt中的对话框遵守相同的原则:DialogType dialog(this);dialog.Setproperties(...);//设置属性if(dialog.exec() == DialogType::value){ //处...

@TOC


前言

Qt为开发者提供了一些可复用的对话框,他对我们的开发是很重要的。下面我们就来学习


提示:以下是本篇文章正文内容,下面案例可供参考

如何学习标准对话框

其实在Qt中的对话框遵守相同的原则:

DialogType dialog(this);
dialog.Setproperties(...);//设置属性

if(dialog.exec() == DialogType::value)
{
	//处理具体的事情
}

其实是很简单的,很快就能学会。

QMessageBox消息对话框

这就相当于是一个QMessageBox
在这里插入图片描述

应用

1、为用户提示重要信息
2、强制用户操作选择

属性

设置窗口标题

setWindowTitle(QString);

设置对话框中的字符

setText(QString s);

设置图标

setIcon(Icon);

Qt中有预定义的图标使用,如下:

QMessageBox::NoIcon
0
the message box does not have any icon.
QMessageBox::Question
4
an icon indicating that the message is asking a question.
QMessageBox::Information
1
an icon indicating that the message is nothing out of the ordinary.
QMessageBox::Warning
2
an icon indicating that the message is a warning, but can be dealt with.
QMessageBox::Critical
3
an icon indicating that the message represents a critical problem.

设置按钮:
如下图,设置的是最下面的三个按钮,也可以设置2个1个等等…
在这里插入图片描述

最后:

if(messagebox.exec() == QMessageBox::Ok)
{
}

提示:QMessageBox::Ok只有在上面这一步设置了,我们才能写。
点击Ok后,我们就会进入if

实操

因为代码比较少,所以我直接在main.cpp中写了,大家可以在类中写

#include "form.h"
#include <QApplication>
#include "QMessageBox"
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMessageBox msg;

    msg.setWindowTitle("Window Title");
    msg.setText("This is a message dialog!");
    msg.setIcon(QMessageBox::Information);
    //会有三个按钮
    msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);

    if( msg.exec() == QMessageBox::Ok )
    {
        qDebug() << "Ok button is clicked!";
    }

    return a.exec();
}


QFileDialog文件对话框

应用

1、打开文件
2、保存文件

属性

设置模式

setAcceptMode();


QFileDialog::AcceptOpen//打开模式
0
QFileDialog::AcceptSave//保存模式
1

设置打开/保存文件是否为1个/多个/其他

setFileMode()


QFileDialog::AnyFile
0
The name of a file, whether it exists or not.
QFileDialog::ExistingFile
1
The name of a single existing file.
QFileDialog::Directory
2
The name of a directory. Both files and directories are displayed. However, the native Windows file dialog does not support displaying files in the directory chooser.
QFileDialog::ExistingFiles
3
The names of zero or more existing files.

取文件中的数据

selectedFiles()

设置只打开哪些后缀的文件:

setNameFilter()

实操

	QFileDialog dlg;

    dlg.setAcceptMode(QFileDialog::AcceptOpen);
    dlg.setNameFilter("Text(*.txt)");
    dlg.setFileMode(QFileDialog::ExistingFiles);

    if( dlg.exec() == QFileDialog::Accepted )
    {
    	//selectFiles()返回值为QStringList,意为QString的链表
    	//使用他的使用需要包含QStringList的头文件
        QStringList fs = dlg.selectedFiles();

        for(int i=0; i<fs.count(); i++)
        {
            qDebug() << fs[i];
        }
    }
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。