QT 对话框实例

QT基本对话框:

标准文件对话框(QFileDialog)、标准颜色对话框(QColorDialog)、标准字体对话框(QFontDialog)、标准输入对话框(QInputDialog)、标准消息对话框(QMessageBox 包括:Question、Information、Warning、Critical、About、AboutQt等消息框)等等。

相关联
类说明
静态函数
函数说明
QFileDialog类 标准文件对话框
getOpenFileName 获得用户选择的文件名
getSaveFileName 获得用户保存的文件名
getExistingDirectory 获得用户选择的已存在的目录名
getOpenFileNames 获得用户选择的文件名列表
QColorDialog类 标准颜色对话框 getColor 获得用户选择的颜色值
QFontDialog类 标准字体对话框 getFont 获得用户选择的字体
QInputDialog 标准输入对话框
getText 标准字符串输入对话框
getItem 下拉表条目输入框
getInt int类型数据收入对话框
getDouble double类型数据输入对话框
QMessageBox 消息对话框
QMessageBox::question Question消息框
QMessageBox::information Information消息框
QMessageBox::warning Warning消息框
QMessageBox::critical Critical消息框
QMessageBox::about About消息框
QMessageBox::aboutQt About Qt消息框

customdialog.h

#ifndef CUSTOMDIALOG_H
#define CUSTOMDIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>


class CustomDialog : public QDialog
{
    
    
    Q_OBJECT
public:
    CustomDialog(QWidget *parent=nullptr);
    ~CustomDialog();
};

#endif // CUSTOMDIALOG_H

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>

#include "inputdialog.h"
#include "msgboxdialog.h"
#include "customdialog.h"

class Dialog : public QDialog
{
    
    
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    QPushButton *fileBtn;
    QLineEdit *fileLineEdit;
    QGridLayout *mainLayout;

    QPushButton *colorBtn;
    QFrame  *colorFrame;

    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;

    QPushButton *inputBtn;
    InputDialog *inputDialog;

    QPushButton *msgBoxBtn;
    MsgBoxDialog *msgBoxDialog;

    QPushButton *customBtn;

private slots:
    void showFile();
    void showColor();
    void showFont();
    void showInputDialog();
    void showMsgBoxDialog();
    void showCustomDialog();
};
#endif // DIALOG_H

inputdialog.h

#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QDialog>
#include <QInputDialog>


class InputDialog : public QDialog
{
    
    
    Q_OBJECT
public:
    InputDialog(QWidget *parent = nullptr);
    ~InputDialog();

signals:

public slots:
    void ChangeName();
    void ChangeSex();
    void ChangeAge();
    void ChangeScore();

private:
    QLabel *nameLabel1;
    QLabel *sexLabel1;
    QLabel *ageLabel1;
    QLabel *scoreLabel1;
    QLabel *nameLabel2;
    QLabel *sexLabel2;
    QLabel *ageLabel2;
    QLabel *scoreLabel2;
    QPushButton *nameBtn;
    QPushButton *sexBtn;
    QPushButton *ageBtn;
    QPushButton *scoreBtn;
    QGridLayout *mainLayout;

};

#endif // INPUTDIALOG_H

msgboxdialog.h

#ifndef MSGBOXDIALOG_H
#define MSGBOXDIALOG_H

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QDialog>
#include <QMessageBox>

class MsgBoxDialog : public QDialog
{
    
    
    Q_OBJECT
public:
    MsgBoxDialog(QWidget *parent = nullptr);
    ~MsgBoxDialog();

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 // MSGBOXDIALOG_H

customdialog.cpp

#include "customdialog.h"

CustomDialog::CustomDialog(QWidget *parent):QDialog(parent)
{
    
    
    setWindowTitle("用户自定义消息");
    QLabel *label = new QLabel(this);
    label->setFrameStyle(QFrame::Panel|QFrame::Sunken);

    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle("用户自定义消息框");

    QPushButton *yesBtn = customMsgBox.addButton("Yes", QMessageBox::ActionRole);
    QPushButton *noBtn = customMsgBox.addButton("No", QMessageBox::ActionRole);
    QPushButton *cancelBtn = customMsgBox.addButton(QMessageBox::Cancel);

    customMsgBox.setText("这是一个用户自定义消息框");
    customMsgBox.setIconPixmap(QPixmap("123.png"));
    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;
}

CustomDialog::~CustomDialog()
{
    
    

}

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
    
    
    setWindowTitle("各种标准对话框实例");

    fileBtn = new QPushButton();
    fileBtn->setText("文件标准对话框");
    fileLineEdit = new QLineEdit();

    colorBtn = new QPushButton();
    colorBtn->setText("颜色标准对话框实例");
    colorFrame = new QFrame();
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);

    fontBtn = new QPushButton();
    fontBtn->setText("字体标准对话框实例");
    fontLineEdit = new QLineEdit();
    fontLineEdit->setText("Welcome!");

    inputBtn = new QPushButton();
    inputBtn->setText("标准输入对话框实例");

    msgBoxBtn = new QPushButton();
    msgBoxBtn->setText("标准消息对话框实例");

    customBtn = new QPushButton();
    customBtn->setText("用户自定义消息对话框实例");

    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(msgBoxBtn, 3, 1);

    mainLayout->addWidget(customBtn, 4, 0);

    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
    connect(inputBtn, SIGNAL(clicked()), this, SLOT(showInputDialog()));
    connect(msgBoxBtn, SIGNAL(clicked()), this, SLOT(showMsgBoxDialog()));
    connect(customBtn, SIGNAL(clicked()), this, SLOT(showCustomDialog()));
}

Dialog::~Dialog()
{
    
    
}

void Dialog::showFile()
{
    
    
    qDebug("******* showFile *****");
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/project/",
                                             "C++ files(*.cpp)::C files(*.c)::Head files(*.h)::All files(*.*)");
    fileLineEdit->setText(s);
}

void Dialog::showColor()
{
    
    
    QColor c = QColorDialog::getColor(Qt::blue);
    if (c.isValid()) {
    
    
        colorFrame->setPalette(QPalette(c));
    }
}

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

void Dialog::showInputDialog()
{
    
    
    inputDialog = new InputDialog(this);
    inputDialog->show();
}

void Dialog::showMsgBoxDialog()
{
    
    
    msgBoxDialog = new MsgBoxDialog();
    msgBoxDialog->show();
}

void Dialog::showCustomDialog()
{
    
    
    CustomDialog *cstmDialog = new CustomDialog();
    cstmDialog->show();
}

inputdialog.cpp

#include "inputdialog.h"

InputDialog::InputDialog(QWidget *parent) : QDialog(parent)
{
    
    
    setWindowTitle("标准输入对话框实例");

    nameLabel1 = new QLabel();
    nameLabel1->setText("姓名: ");
    nameLabel2 = new QLabel();
    nameLabel2->setText("啊三");
    nameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    nameBtn = new QPushButton;
    nameBtn->setText("修改姓名");

    sexLabel1 = new QLabel();
    sexLabel1->setText("性别: ");
    sexLabel2 = new QLabel();
    sexLabel2->setText("男");
    sexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    sexBtn = new QPushButton();
    sexBtn->setText("修改性别");

    ageLabel1 = new QLabel();
    ageLabel1->setText("年龄: ");
    ageLabel2 = new QLabel();
    ageLabel2->setText("22");
    ageLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    ageBtn = new QPushButton();
    ageBtn->setText("修改年龄");

    scoreLabel1 = new QLabel();
    scoreLabel1->setText("成绩: ");
    scoreLabel2 = new QLabel();
    scoreLabel2->setText("90");
    scoreLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    scoreBtn = new QPushButton();
    scoreBtn->setText("修改成绩");

    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()));
}

InputDialog::~InputDialog()
{
    
    

}

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

void InputDialog::ChangeSex()
{
    
    
    bool ok;
    QStringList SexItems;
    SexItems.insert(0, "男");
    SexItems.insert(1, "女");
    QString SexItem = QInputDialog::getItem(this, "标准条目选择对话框", "请选择性别: ", SexItems, 0, false, &ok);
    if (ok && !SexItem.isEmpty()) {
    
    
        sexLabel2->setText(SexItem);
    }

}

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

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

msgboxdialog.cpp

#include "msgboxdialog.h"

MsgBoxDialog::MsgBoxDialog(QWidget *parent) : QDialog(parent)
{
    
    
    setWindowTitle("标准消息对话框实例");

    label = new QLabel();
    label->setText("请选择一种消息框");

    questionBtn = new QPushButton();
    questionBtn->setText("QuestionMsg");

    informationBtn = new QPushButton();
    informationBtn->setText("InformationMsg");

    warningBtn = new QPushButton();
    warningBtn->setText("WarningMsg");

    criticalBtn = new QPushButton();
    criticalBtn->setText("CriticalMsg");

    aboutBtn = new QPushButton();
    aboutBtn->setText("AboutMsg");

    aboutQtBtn = new QPushButton();
    aboutQtBtn->setText("AboutQtMsg");

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(label, 0, 0, 1, 2);
    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()));
}


MsgBoxDialog::~MsgBoxDialog()
{
    
    

}

void MsgBoxDialog::showQuestionMsg()
{
    
    
    label->setText("Question Message Box");
    switch(QMessageBox::question(this, "Question 消息框", "修改完成是否退出程序?",
                                 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;
}

void MsgBoxDialog::showInformationMsg()
{
    
    
    label->setText("Information Message Box");
    QMessageBox::information(this, "Information 消息框", "This is Information Box,Welcome!");

    return;
}

void MsgBoxDialog::showWarningMsg()
{
    
    
    label->setText("Warning Message Box");
    QMessageBox::StandardButton btn =  QMessageBox::warning(this, "Warning 消息框", "内容为保存,是否保存修改?",
                                                            QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel, QMessageBox::Save);
    switch(btn) {
    
    
    case QMessageBox::Save:
        label->setText("Warning button/Save");
        break;
    case QMessageBox::Discard:
        label->setText("Warning button/Discard");
        break;
    case QMessageBox::Cancel:
        label->setText("Warning button/Cancel");
        break;
    default:
        break;
    }
    return;
}

void MsgBoxDialog::showCriticalMsg()
{
    
    
    label->setText("Critical Message Box");
    QMessageBox::critical(this, "Critical 消息框", "Critical Message Box Dialog");
    return;
}

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

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

main.cpp

#include "dialog.h"

#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
    Dialog *window = new Dialog;

    window->show();
    return a.exec();
}

在这里插入图片描述

扫描二维码关注公众号,回复: 12407477 查看本文章

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/109691664