QT5之基本对话框

对话框

对话框Name
标准文件对话框
标准颜色对话框
标准字体对话框
标准输入对话框(标注字符串 条目对话框)
各种消息对话框
Question对话框
Information对话框
Warning对话框
Critical对话框
About对话框

各对话框对应的静态函数

在这里插入图片描述

标准文件对话框

详细说明getOpenFileName()函数的作用以及各个参数
在这里插入图片描述

实现

头文件
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>

// 标注文件对话框
private:
    // 文件对话框需要成员 button lintedit layout
    QPushButton *fileBtn;
    QLineEdit *fileLineEdit;
    QGridLayout *mainLayout;
private slots:
    void showFile();
类cpp文件
#include "qlineedit.h"
#include "qpushbutton.h"
#include "qgridlayout.h"
#include "qstring.h"
#include "qfiledialog.h"

// 标注文件对话框
    fileBtn = new QPushButton(); // 控件对象的初始化
    fileBtn->setText(tr("文件标准对话框实例"));
    fileLineEdit = new QLineEdit(); //用来显示选择的文件名
    // 布局管理
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn,0,0);  //后面两个参数应该是row column
    mainLayout->addWidget(fileLineEdit,0,1);
    // 事件关联
    connect(fileBtn,SIGNAL(clicked()),this,SLOT(showFile()));
}

void Dialog::showFile()
{
    // 声明的路径,声明可以打开的文件格式
    QString s = QFileDialog::getOpenFileName(this,"open file dialog","/",
                                             "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}

在这里插入图片描述
在这里插入图片描述

标注颜色对话框

函数getColor的形式:
在这里插入图片描述

实现

头文件
#include <QFrame>

// 标注颜色对话框
private:
    QPushButton *colorBtn;
    QFrame *colorFrame;
private slots:
    void showColor();

类文件
#include "qframe.h"
#include "qcolordialog.h"
// 标注颜色对话框
    colorBtn = new QPushButton();
    colorBtn->setText(tr("颜色标注对话框实例"));
    colorFrame = new QFrame;
    //colorFrame对象是根据用户选择的不同颜色更新不同的背景
    colorFrame->setFrameShape(QFrame::Box); // 设置背景格式
    colorFrame->setAutoFillBackground(true); // 将自动填充背景设为True
    // 布局管理
    mainLayout->addWidget(colorBtn,1,0);
    mainLayout->addWidget(colorFrame,1,1);
    connect(colorBtn,SIGNAL(clicked()),this,SLOT(showColor()));

void Dialog::showColor()
{
    QColor c = QColorDialog::getColor(Qt::blue); // 设置默认是蓝色
    // 判断获取的颜色是否有效,则把frame对象背景改为该颜色
    if(c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

标注字体对话框

getFont函数:,更改你文本框中的字体类型和大小
在这里插入图片描述

实现

头文件
#include <QLineEdit>
#include <QPushButton>
// 标注字体对话框
private:
    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;

private slots:
    void showFont();

类文件
#include "qfontdialog.h"
#include "qlineedit.h"
#include "qpushbutton.h"

// 标注字体对话框
    fontBtn = new QPushButton();
    fontBtn->setText(tr("字体标注对话框实例"));
    fontLineEdit = new QLineEdit();
    fontLineEdit->setText(tr("Welcome!"));
    mainLayout->addWidget(fontBtn,2,0);
    mainLayout->addWidget(fontLineEdit,2,1);

    connect(fontBtn,SIGNAL(clicked(bool)),this,SLOT(showFont()));

void Dialog::showFont()
{
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(f);
    }
}

标注输入对话框

在项目文件中添加C++ class文件,基类归属Dialog,类名随意
在这里插入图片描述

在运行时报错error: undefined reference to `vtable for InputDlg’
可以通过重新qmake 或者 将新建的文件移除再添加进去

实现(只要实现了标准输入框类之后,字符串和条目都是在该类下实现)

inputDlg头文件
#ifndef INPUTDLG_H
#define INPUTDLG_H

#include <QDialog>
class InputDlg : public QDialog
{
    Q_OBJECT
public:
    InputDlg(QWidget *parent = 0);
};

#endif // INPUTDLG_H

类文件
#include "inputdlg.h"
#include <QInputDialog>
InputDlg::InputDlg(QWidget *parent)
    : QDialog(parent)
{
}

dialog头文件
#include "inputdlg.h"
private:
    QPushButton *inputBtn;
    // 添加标注输入对话框的实例类
    InputDlg *inputDlg;
private slots:
    void showInputDlg();
类文件
#include "inputdlg.h"
// 标注输入对话框
    inputBtn = new QPushButton();
    inputBtn->setText(tr("标注输入对话框"));
    mainLayout->addWidget(inputBtn,3,0);
    connect(inputBtn,SIGNAL(clicked(bool)),this,SLOT(showInputDlg()));
void Dialog::showInputDlg()
{
    inputDlg = new InputDlg(this);
    inputDlg->show();
}

在这里插入图片描述
点击之后出现的效果

标注字符串输入对话框

在这里插入图片描述

实现

要求是在标准输入框下单击“修改姓名”按钮后出现对话框,可在该对话框内修改姓名

inputDlg头文件
#ifndef INPUTDLG_H
#define INPUTDLG_H

#include <QDialog>

#include "qlabel.h"
#include "inputdlg.h"
#include "qpushbutton.h"
#include "qgridlayout.h"

class InputDlg : public QDialog
{
    Q_OBJECT
public:
    InputDlg(QWidget *parent = 0);

private:
    QGridLayout *mainLayout;
    QPushButton *nameBtn;
    QLabel *nameLabel1;
    QLabel *nameLabel2;

private slots:
    void ChangName();
};

#endif // INPUTDLG_H

类文件
#include "inputdlg.h"


#include <QInputDialog>

#include "qlabel.h"
#include "qstring.h"
#include "qgridlayout.h"

InputDlg::InputDlg(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("标准输入对话框实例"));
    mainLayout = new QGridLayout(this); // 布局作为子对象,然后添加小组件
    // 标注字符串输入框
    nameLabel1 = new QLabel();
    nameLabel1->setText(tr("姓名"));
    nameLabel2 = new QLabel();
    nameLabel2->setText((tr("周贺")));
    nameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);//标签的风格
    nameBtn = new QPushButton;
    nameBtn->setText(tr("修改姓名"));
    // 布局
    mainLayout->addWidget(nameLabel1,0,0);
    mainLayout->addWidget(nameLabel2,0,1);
    mainLayout->addWidget(nameBtn,0,2);
    connect(nameBtn,SIGNAL(clicked(bool)),this,SLOT(ChangName()));

}

void InputDlg::ChangName()
{
    bool ok;
    QString text = QInputDialog::getText(this,tr("标准字符串输入对话框"),
         tr("请输入姓名:"), QLineEdit::Normal,nameLabel2->text(),&ok);
    if(ok&&!text.isEmpty())
        nameLabel2->setText(text);
}

在这里插入图片描述
在这里插入图片描述
效果:对话框的lauout中加入标准输入对象(inputDlg),然后在其中加入子layout然后布局字符串输入(QInputDialog)

标准条目对话框

在这里插入图片描述

实现

目的:单击“修改性别”按钮后出现对话框,可在该对话框内选择性别

    QLabel *sexLabel1;
    QLabel *sexLabel2;
    QPushButton *sexBtn;
    void ChangSex();

//标准条目
    sexLabel1 = new QLabel;
    sexLabel2 = new QLabel;
    sexLabel1->setText(tr("修改性别"));
    sexLabel2->setText(tr("男"));
    sexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    sexBtn = new QPushButton();
    sexBtn->setText(tr("修改性别"));
    //
    mainLayout->addWidget(sexLabel1,1,0);
    mainLayout->addWidget(sexLabel2,1,1);
    mainLayout->addWidget(sexBtn,1,2);
    connect(sexBtn,SIGNAL(clicked(bool)),this,SLOT(ChangSex()));

void InputDlg::ChangSex()
{
    // 列表批量化输入
    QStringList sexItems;
    sexItems << tr("男") << tr("女");
    bool ok;
    QString sexItem = QInputDialog::getItem(this, tr("标准条目选择对话框"),
            tr("请选择性别:"), sexItems, 0, false, &ok);
    if(ok&&!sexItems.isEmpty())
        sexLabel2->setText(sexItem);
}

在这里插入图片描述

标准INT和DOUBLE型对话框

代码主要放置 里面的change函数
在这里插入图片描述

void InputDlg::ChangeAge()
{
    bool ok;
    int age = QInputDialog::getInt(this, tr("标准int类型输入对话框"),
     tr("请输入年龄:"), ageLabel2->text().toInt(&ok), 0, 100, 1, &ok);
    if (ok)
        ageLabel2->setText(QString(tr("%1")).arg(age));
}

单击“修改年龄”按钮后出现对话框,可在该对话框内修改年龄

在这里插入图片描述

void InputDlg::ChangeScore()
{
    bool ok;
    double score = QInputDialog::getDouble(this, tr("标准double类型输入对话框"),
     tr("请输入成绩:"),scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);
    if (ok)
        scoreLabel2->setText(QString(tr("%1")).arg(score));
}

消息对话框

所有的消息框的内容函数 都是源于 QMessageBox 类
创建继承于QDialog的MsgBoxDlg类

#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include <QDialog>// 添加


class MsgBoxDlg : public QDialog
{
    Q_OBJECT // 添加
public:
    MsgBoxDlg(QWidget *parent = 0);// 添加
};

#endif // MSGBOXDLG_H

#include "msgboxdlg.h"

MsgBoxDlg::MsgBoxDlg(QWidget *parent)
    : QDialog(parent)   // 添加
{

}

在dialog中的改动

头文件  
#include "msgboxdlg.h"
private:
    QPushButton *MsgBtn;
    MsgBoxDlg *msgDlg;

private slots:
    void showMsgDlg();

类文件
// 消息对话框
    MsgBtn = new QPushButton;
    MsgBtn->setText(tr("标准消息对话框实例"));
    mainLayout->addWidget(MsgBtn,3,1);
    connect(MsgBtn,SIGNAL(clicked()),this,SLOT(showMsgDlg()));

void Dialog::showMsgDlg()
{
    msgDlg = new MsgBoxDlg();
    msgDlg->show();
}

Question消息对话框

在这里插入图片描述

实现
头文件
#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include <QDialog>// 添加

#include <QLabel>
#include <QPushButton>
#include <QGridLayout>

class MsgBoxDlg : public QDialog
{
    Q_OBJECT // 添加
public:
    MsgBoxDlg(QWidget *parent = 0);// 添加

private:
    QLabel *label;
    QPushButton *questionBtn;
    QGridLayout *mainLayout;
private slots:
    void showQuestionMsg();
};

#endif // MSGBOXDLG_H

类文件
#include "msgboxdlg.h"

#include <QMessageBox>

MsgBoxDlg::MsgBoxDlg(QWidget *parent)
    : QDialog(parent)   // 添加
{
    setWindowTitle(tr("标准消息对话框实例"));
    mainLayout = new QGridLayout(this);

    label = new QLabel();
    label->setText(tr("请选择一种消息框"));
    // 按钮设定
    questionBtn = new QPushButton();
    questionBtn->setText(tr("QuestionMsg"));
    // 布局设定
    mainLayout->addWidget(label,0,0,1,2);
    mainLayout->addWidget(questionBtn,1,0);
    // 事件关联
    connect(questionBtn,SIGNAL(clicked(bool)),this,SLOT(showQuestionMsg()));

}
void MsgBoxDlg::showQuestionMsg()
{
    label->setText(tr("Question Message Box"));
        switch(QMessageBox::question(this,tr("Question消息框"),
              tr("您现在已经修改完成,是否要结束程序?"),
              QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))
        {
        case QMessageBox::Ok:
            label->setText("Question button/Ok");
            break;
        case QMessageBox::Cancel:
            label->setText("Question button/Cancel");
            break;
        default:
            break;
        }
        return;
}

在这里插入图片描述

选择了不同结果会进行更改

Information消息框

在这里插入图片描述

// label是之前同意定义的,一旦点击某个消息框之后,内容就会更改
informationBtn =new QPushButton;
informationBtn->setText(tr("InformationMsg"));
mainLayout->addWidget(informationBtn,1,1);
connect(informationBtn,SIGNAL(clicked()),this,SLOT(showInformationMsg()));


void MsgBoxDlg::showInformationMsg()
{
    label->setText(tr("Information Message Box"));
    QMessageBox::information(this,tr("Information消息框"),tr("这是Information消息框测试,欢迎您!"));
    return;
}

在这里插入图片描述

Warning消息框

在这里插入图片描述

void MsgBoxDlg::showWarningMsg()
{
    label->setText(tr("Warning Message Box"));
    switch(QMessageBox::warning(this,tr("Warning消息框"),
         tr("您修改的内容还未保存,是否要保存对文档的修改?"),
         QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,
         QMessageBox::Save))
    {
    case QMessageBox::Save:
        label->setText(tr("Warning button/Save"));
        break;
    case QMessageBox::Discard:
        label->setText(tr("Warning button/Discard"));
        break;
    case QMessageBox::Cancel:
        label->setText(tr("Warning button/Cancel"));
        break;
    default:
        break;
    }
    return;
}

在这里插入图片描述

CRITICAL对话框

在这里插入图片描述

void MsgBoxDlg::showCriticalMsg()
{
    label->setText(tr("Critical Message Box"));
    QMessageBox::critical(this,tr("Critical消息框"),tr("这是一个Critical消息框测试!"));
    return;
}

在这里插入图片描述

ABOUT

在这里插入图片描述

void MsgBoxDlg::showAboutMsg()
{
    label->setText(tr("About Message Box"));
    QMessageBox::about(this,tr("About消息框"),tr("这是一个About消息框测试!"));
    return;
}

在这里插入图片描述

ABOUTQT

在这里插入图片描述

void MsgBoxDlg::showAboutQtMsg()
{
    label->setText(tr("About Qt Message Box"));
    QMessageBox::aboutQt(this,tr("About Qt消息框"));
    return;
}

在这里插入图片描述

HomeWork

1. 使用文件对话框,获取用户选择的文件夹路径,并存储到QString变量中

2. 使用字符串输入对话框,获取用户输入的文本内容,存入到Qstring变量中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值