第四章 Qt5基本对话框——实例:标准基本对话框

       一、简介和运行结果

        本实例包括“文本标准对话框”、“颜色标准对话框”、“字体标准对话框”、“表输入对话框”、“标准消息对话框”、“自定义对话框”。

第四章 Qt5基本对话框——实例:标准基本对话框_第1张图片

第四章 Qt5基本对话框——实例:标准基本对话框_第2张图片

二、代码实现

        具体细节看书了解。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "inputdlg.h"
#include "mesboxdlg.h"

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private slots:
    void showFile();//标准文件对话框--槽函数
    void showColor();//颜色标准对话框--槽函数
    void showFont();//标准字体对话框--槽函数
    void showInput();//标准输入对话框--槽函数
    void showMesDlg();//标准信息对话框--槽函数
    void showCustomDlg();//自定义对话框--槽函数
private:
    //标准文件对话框的部件
    QPushButton * fileBtn;
    QLineEdit * fileLineEdit;
    //颜色标准对话框的部件
    QPushButton * colorBtn;
    QFrame * colorFrame;
    //标准字体对话框部件
    QPushButton * fontBtn;
    QLineEdit * fontLineEdit;
    //标准输入对话框部件
    QPushButton * inputBtn;
    InputDlg * inputDlg;
    //标准消息对话框实例部件
    QPushButton * MesBtn;
    MesBoxDlg * mesDlg;
    //自定义消息框部件
    QPushButton * CustomBtn;
    QLabel * label;
    //整体布局
    QGridLayout * mainLayout;


};

#endif // DIALOG_H

 dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //1、标准文件对话框
    fileBtn = new QPushButton;
    fileBtn->setText(tr("文件标准对话框实例"));
    fileLineEdit = new QLineEdit;
    connect(fileBtn,SIGNAL(clicked()),this,SLOT(showFile()));//连接信号
    //2、颜色标准对话框
    colorBtn = new QPushButton;
    colorBtn->setText(tr("颜色标准对话框实例"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);//框架的形状--盒子
    colorFrame->setAutoFillBackground(true);//背景颜色
    connect(colorBtn,SIGNAL(clicked()),this,SLOT(showColor()));
    //3、标准字体对话框
    fontBtn = new QPushButton;
    fontBtn->setText(tr("标准字体对话框"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));
    connect(fontBtn,SIGNAL(clicked()),this,SLOT(showFont()));
    //4、标准输入对话框
    inputBtn = new QPushButton;
    inputBtn->setText(tr("标准输入对话框"));
    connect(inputBtn,SIGNAL(clicked()),this,SLOT(showInput()));
    //5、标准信息对话框
    MesBtn = new QPushButton;
    MesBtn->setText(tr("标准信息对话框"));
    connect(MesBtn,SIGNAL(clicked()),this,SLOT(showMesDlg()));
    //6、自定义对话框
    CustomBtn = new QPushButton;
    CustomBtn->setText(tr("用户自定义消息对话框实例"));
    label = new QLabel;
    label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    connect(CustomBtn,SIGNAL(clicked()),this,SLOT(showCustomDlg()));
    //布局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn,0,0);
    mainLayout->addWidget(fileLineEdit,0,1);
    mainLayout->addWidget(colorBtn,1,0);
    mainLayout->addWidget(colorFrame,1,1);
    mainLayout->addWidget(fontBtn,2,0);
    mainLayout->addWidget(fontLineEdit,2,1);
    mainLayout->addWidget(inputBtn,3,0);
    mainLayout->addWidget(MesBtn,3,1);
    mainLayout->addWidget(CustomBtn,4,0);
    mainLayout->addWidget(label,4,1);
}

//1、标准文件对话框--槽函数
void Dialog::showFile()
{
    QString s = QFileDialog::getOpenFileName(this,"open file dialog","/",
                                             "All files(*.*);;"
                                             "C++ files(*.cpp);;"
                                             "C file(*.c);;"
                                             "Head files(*.h)");
    fileLineEdit->setText(s);
}

//2、颜色标准对话框--槽函数
void Dialog::showColor()
{
    QColor c = QColorDialog::getColor(Qt::blue);
    if(c.isValid()){//QColor::isValid()函数可以判断用户选择的颜色是否有效
        colorFrame->setPalette(QPalette(c));
    }
}

//3、标准字体对话框--槽函数
void Dialog::showFont()
{
    bool ok;
    QFont f = QFontDialog::getFont(&ok);
    if(ok){
        fontLineEdit->setFont(f);
    }
}

//标准输入对话框--槽函数
void Dialog::showInput()
{
    inputDlg = new InputDlg;
    inputDlg->show();
}

//标准信息对话框--槽函数
void Dialog::showMesDlg()
{
    mesDlg = new MesBoxDlg();
    mesDlg->show();
}

void Dialog::showCustomDlg()
{
    label->setText(tr("Custom Message Box"));
    QMessageBox customMsgBox;
    //QLabel ico = ("Qt.png");
    customMsgBox.setWindowTitle(tr("自定义消息框"));
    QPushButton * yesBtn = customMsgBox.addButton(tr("YES"),QMessageBox::ActionRole);
    QPushButton * noBtn = customMsgBox.addButton(tr("NO"),QMessageBox::ActionRole);
    QPushButton * cancelBtn = customMsgBox.addButton(QMessageBox::Cancel);
    //customMsgBox.setIconPixmap(QPixmap("Qt.png"));//没有出现,不知道为什么
    customMsgBox.setText(tr("这是一个用户自定义消息框"));


    customMsgBox.exec();
    if(customMsgBox.clickedButton() == yesBtn)
        label->setText("Custom Message Box/Yes");
    if(customMsgBox.clickedButton() == noBtn)
        label->setText("Custom Message Box/No");
    if(customMsgBox.clickedButton() == cancelBtn)
        label->setText( "Custom Message Box/Cancel");
    return;
}

Dialog::~Dialog()
{

}

子窗口--消息框mesboxdlg.h

#ifndef MESBOXDLG_H
#define MESBOXDLG_H

#include 
#include 
#include 
#include 
#include 

class MesBoxDlg : public QDialog
{
    Q_OBJECT
public:
    MesBoxDlg(QWidget * parent = 0);
    ~MesBoxDlg();
private slots:
    void showQuestionMsg();
    void showInformationMsg();
    void showWarningMsg();
    void showCriticalMsg();
    void showAboutMsg();
    void showAboutQtMsg();
private:
    QLabel * label;
    QPushButton * questionBtn;
    QPushButton * informationBtn;
    QPushButton * warningBtn;
    QPushButton * criticalBtn;
    QPushButton * aboutBtn;
    QPushButton * aboutQtBtn;
    QGridLayout * mainLayout;

};

#endif // MESBOXDLG_H

子窗口--消息框mesboxdlg.cpp

#include "mesboxdlg.h"

MesBoxDlg::MesBoxDlg(QWidget * parent):QDialog(parent)
{
    setWindowTitle(tr("标准消息对话框实例:"));
    label = new QLabel;
    label->setText(tr("请选择一种消息框"));
    questionBtn = new QPushButton;
    questionBtn->setText(tr("QuestionMsg"));
    informationBtn = new QPushButton;
    informationBtn->setText(tr("InformationMsg"));
    warningBtn = new QPushButton;
    warningBtn->setText(tr("WarningMsg"));
    criticalBtn = new QPushButton;
    criticalBtn->setText(tr("CriticalMsg"));
    aboutBtn = new QPushButton;
    aboutBtn->setText(tr("AboutMsg"));
    aboutQtBtn = new QPushButton;
    aboutQtBtn->setText(tr("AboutQtMsg"));
    //布局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(label,0,0);
    mainLayout->addWidget(questionBtn,1,0);
    mainLayout->addWidget(informationBtn,1,1);
    mainLayout->addWidget(warningBtn,2,0);
    mainLayout->addWidget(criticalBtn,2,1);
    mainLayout->addWidget(aboutBtn,3,0);
    mainLayout->addWidget(aboutQtBtn,3,1);
    //连接信号
    connect(questionBtn,SIGNAL(clicked()),this,SLOT(showQuestionMsg()));
    connect(informationBtn,SIGNAL(clicked()),this,SLOT(showInformationMsg()));
    connect(warningBtn,SIGNAL(clicked()),this,SLOT(showWarningMsg()));
    connect(criticalBtn,SIGNAL(clicked()),this,SLOT(showCriticalMsg()));
    connect(aboutBtn,SIGNAL(clicked()),this,SLOT(showAboutMsg()));
    connect(aboutQtBtn,SIGNAL(clicked()),this,SLOT(showAboutQtMsg()));
}

MesBoxDlg::~MesBoxDlg()
{

}

void MesBoxDlg::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("Quesstion button/Cancel");
        break;
    default:
        break;
    }
    return;
}

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

void MesBoxDlg::showWarningMsg()
{
    label->setText(tr("Warning Message Box"));
    switch (QMessageBox::warning(this,tr("Waring消息框"),
                                 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;
}

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

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

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

子窗口--输入对话框inputdlg.h

#ifndef INPUTDLG_H
#define INPUTDLG_H

#include 
#include 
#include 
#include 
#include 

class InputDlg : public QDialog
{
    Q_OBJECT
public:
    InputDlg(QWidget* parent = 0);
    ~InputDlg();
private slots:
    void changeName();
    void changeSex();
    void changeAge();
    void changeScore();
private:
    QLabel * nameLabel1;
    QLabel * nameLabel2;
    QLabel * sexLabel1;
    QLabel * sexLabel2;
    QLabel * ageLabel1;
    QLabel * ageLabel2;
    QLabel * scoreLabel1;
    QLabel * scoreLabel2;
    QPushButton * nameBtn;
    QPushButton * sexBtn;
    QPushButton * ageBtn;
    QPushButton * scoreBtn;
    QGridLayout * mainLayout;
};

#endif // INPUTDLG_H

子窗口--输入对话框inputdlg.cpp

#include "inputdlg.h"

InputDlg::InputDlg(QWidget * parent):QDialog(parent)
{
    setWindowTitle(tr("标准输入对话框"));
    nameLabel1 = new QLabel;
    nameLabel1->setText(tr("姓名:"));
    nameLabel2 = new QLabel;
    nameLabel2->setText(tr("杨五郎"));
    nameLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    nameBtn = new QPushButton;
    nameBtn->setText(tr("修改姓名"));
    sexLabel1 = new QLabel;
    sexLabel1->setText(tr("性别:"));
    sexLabel2 = new QLabel;
    sexLabel2->setText(tr("男"));
    sexLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    sexBtn = new QPushButton;
    sexBtn->setText(tr("修改性别"));
    ageLabel1 = new QLabel;
    ageLabel1->setText(tr("年龄:"));
    ageLabel2 = new QLabel;
    ageLabel2->setText(tr("20"));
    ageLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    ageBtn = new QPushButton;
    ageBtn->setText(tr("修改年龄"));
    scoreLabel1 = new QLabel;
    scoreLabel1->setText(tr("成绩:"));
    scoreLabel2 = new QLabel;
    scoreLabel2->setText(tr("80"));
    scoreLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    scoreBtn = new QPushButton;
    scoreBtn->setText(tr("修改成绩"));
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(nameLabel1,0,0);
    mainLayout->addWidget(nameLabel2,0,1);
    mainLayout->addWidget(nameBtn,0,2);
    mainLayout->addWidget(sexLabel1,1,0);
    mainLayout->addWidget(sexLabel2,1,1);
    mainLayout->addWidget(sexBtn,1,2);
    mainLayout->addWidget(ageLabel1,2,0);
    mainLayout->addWidget(ageLabel2,2,1);
    mainLayout->addWidget(ageBtn,2,2);
    mainLayout->addWidget(scoreLabel1,3,0);
    mainLayout->addWidget(scoreLabel2,3,1);
    mainLayout->addWidget(scoreBtn,3,2);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    connect(nameBtn,SIGNAL(clicked()),this,SLOT(changeName()));
    connect(sexBtn,SIGNAL(clicked()),this,SLOT(changeSex()));
    connect(ageBtn,SIGNAL(clicked()),this,SLOT(changeAge()));
    connect(scoreBtn,SIGNAL(clicked()),this,SLOT(changeScore()));
}

InputDlg::~InputDlg()
{

}

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

void InputDlg::changeSex()
{
    QStringList SexItems;
    SexItems<setText(SexItem);
}

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

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

你可能感兴趣的:(Qt5开发及实例,qt,开发语言)