QT 基本对话框

包括:

1.标准文件对话框

QT 基本对话框_第1张图片

 QT 基本对话框_第2张图片

 dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "inputdlg.h"
#include "msgboxdlg.h"
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);

    ~Dialog();
private:
    //标准文件对话框实例
    QPushButton *btnfile;
    QLineEdit *linefile;
    QGridLayout *mainLayout;

    //标准颜色对话框
    QPushButton *btncolor;
    QFrame *framecolor;

    //标准字体对话框
    QPushButton *btnfont;
    QLineEdit *linefont;

    //标准输入对话框
    QPushButton *btninput;
    InputDlg *Dlginput;

    //标准消息对话框
    QPushButton  *btnMsg;
    MsgBoxDlg   *Dlgmsg;


    //自定义消息对话框
    QPushButton  *btnCustom;
    QLabel   *label;

private slots:
    void  showFile();
    void  showColor();
    void  showFont();
    void  showInputDlg();
    void  showMsgDlg();
    void  showCustomDlg();

};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include 
#include 
#include 
#include 
#include 
#include 
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(QStringLiteral("各种标准对话框的实例"));
//标准文件对话框实例
    btnfile=new QPushButton;
    btnfile->setText(QStringLiteral("文件标准对话框"));
    linefile=new QLineEdit;

//标准颜色对话框
    btncolor=new QPushButton;
    btncolor->setText(QStringLiteral("颜色标准对话框"));
    framecolor=new QFrame;
    framecolor->setFrameShape(QFrame::Box);
    framecolor->setAutoFillBackground(true);

//标准字体对话框
    btnfont=new QPushButton;
    btnfont->setText(QStringLiteral("字体标准对话框"));
    linefont=new QLineEdit;
    linefont->setText(QStringLiteral("Welcome"));

//标准输入对话框
    btninput=new QPushButton;
    btninput->setText(QStringLiteral("输入标准对话框"));

//标准消息对话框
    btnMsg=new QPushButton;
    btnMsg->setText(QStringLiteral("输入消息对话框"));

//自定义消息对话框
    btnCustom=new QPushButton;
    btnCustom->setText(QStringLiteral("用户自定义消息对话框"));
    label=new QLabel;
    label->setFrameStyle(QFrame::Panel|QFrame::Sunken);


    //布局
    mainLayout =new  QGridLayout(this);
    mainLayout->addWidget(btnfile,0,0);
    mainLayout->addWidget(linefile,0,1);

    mainLayout->addWidget(btncolor,1,0);
    mainLayout->addWidget(framecolor,1,1);

    mainLayout->addWidget(btnfont,2,0);
    mainLayout->addWidget(linefont,2,1);

    mainLayout->addWidget(btninput,3,0);
    mainLayout->addWidget(btnMsg,3,1);

    mainLayout->addWidget(btnCustom,4,0);
    mainLayout->addWidget(label,4,1);

    //添加信号与槽
    connect(btnfile,SIGNAL(clicked()),this,SLOT(showFile()));//文件
    connect(btncolor,SIGNAL(clicked()),this,SLOT(showColor()));//颜色
    connect(btnfont,SIGNAL(clicked()),this,SLOT(showFont()));//字体
    connect(btninput,SIGNAL(clicked()),this,SLOT(showInputDlg()));//输入????用qDebug调试一下,你来、
    connect(btnMsg,SIGNAL(clicked()),this,SLOT(showMsgDlg()));//消息
    connect(btnCustom,SIGNAL(clicked()),this,SLOT(showCustomDlg()));//消息

}

Dialog::~Dialog()
{
}

//标准文件对话框实例
void Dialog::showFile()
{
   QString s=QFileDialog::getOpenFileName(this,"open file dialog","/","c++ files(*.cpp)::c files(*.c)::Head files(*.h)");
   linefile->setText(s);
}



//标准颜色对话框
void Dialog::showColor()
{
    QColor c =QColorDialog::getColor(Qt::blue);
    if(c.isValid())
    {
        framecolor->setPalette(QPalette(c));
    }
}

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

}

//输入标准对话框
void Dialog::showInputDlg()
{
    Dlginput=new InputDlg(this);//this啥意思,你这个是弹窗,不是替换原有界面,this删掉
    Dlginput->show();//????
    qDebug()<<"hhhhhh"<show();


}

//用户自定义消息框
void Dialog::showCustomDlg()
{
    label->setText(QStringLiteral("Custom Message Box"));
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle(QStringLiteral("用户自定义消息框"));

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

    customMsgBox.setText(QStringLiteral("这是一个用户自定义消息框"));
    customMsgBox.setIconPixmap(QPixmap("Qt.png"));
    customMsgBox.exec();

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

    return;
}

QT 基本对话框_第3张图片

2.标准颜色对话框

QT 基本对话框_第4张图片

 3.标准字体对话框

QT 基本对话框_第5张图片

 4.标准输入对话框

4.1标准字符串输入对话框

QT 基本对话框_第6张图片 

4.2标准条目选择对话框

QT 基本对话框_第7张图片

4.3标准int类型输入对话框

QT 基本对话框_第8张图片

4.4标准double类型输入对话框

QT 基本对话框_第9张图片

 inputdlg.h

#ifndef INPUTDLG_H
#define INPUTDLG_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

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

signals:

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

private:
    QLabel *labname1;
    QLabel *labsex1;
    QLabel *labage1;
    QLabel *labscore1;
    QLabel *labname2;
    QLabel *labsex2;
    QLabel *labage2;
    QLabel *labscore2;

    QPushButton  *btnname;
    QPushButton  *btnsex;
    QPushButton  *btnage;
    QPushButton  *btnscore;
    QGridLayout  *mainLayout;
};

#endif // INPUTDLG_H

 inputdlg.cpp

#include "inputdlg.h"
#include 
#include 
#include 
#include 
InputDlg::InputDlg(QWidget *parent) : QDialog(parent)
{
   setWindowTitle(QStringLiteral("各种标准对话框的实例"));
   labname1=new QLabel;
   labname1->setText(QStringLiteral("姓名"));
   labname2=new QLabel;
   labname2->setText(QStringLiteral("张三"));
   labname2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
   btnname=new QPushButton;
   btnname->setText(QStringLiteral("修改姓名"));

    labsex1=new QLabel;
    labsex1->setText(QStringLiteral("性别"));
    labsex2=new QLabel;
    labsex2->setText(QStringLiteral("男"));
    labsex2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    btnsex=new QPushButton;
    btnsex->setText(QStringLiteral("修改性别"));

    labage1=new QLabel;
    labage1->setText(QStringLiteral("年龄"));
    labage2=new QLabel;
    labage2->setText(QStringLiteral("21"));
    labage2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    btnage=new QPushButton;
    btnage->setText(QStringLiteral("修改年龄"));

    labscore1=new QLabel;
    labscore1->setText(QStringLiteral("成绩"));
    labscore2=new QLabel;
    labscore2->setText(QStringLiteral("98"));
    labscore2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    btnscore=new QPushButton;
    btnscore->setText(QStringLiteral("修改成绩"));


    //布局“QGridLayout::QGridLayout(const QGridLayout &)”: 无法将参数 1 从“InputDlg *”转换为“QWidget *” ---说明QObject不能实现
    mainLayout =new  QGridLayout(this);
    mainLayout->addWidget(labname1,0,0);
    mainLayout->addWidget(labname2,0,1);
    mainLayout->addWidget(btnname,0,2);

    mainLayout->addWidget(labsex1,1,0);
    mainLayout->addWidget(labsex2,1,1);
    mainLayout->addWidget(btnsex,1,2);

    mainLayout->addWidget(labage1,2,0);
    mainLayout->addWidget(labage2,2,1);
    mainLayout->addWidget(btnage,2,2);

    mainLayout->addWidget(labscore1,3,0);
    mainLayout->addWidget(labscore2,3,1);
    mainLayout->addWidget(btnscore,3,2);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);

//信号与槽的连接
    connect(btnname,SIGNAL(clicked()),this,SLOT(ChangeName()));
    connect(btnsex,SIGNAL(clicked()),this,SLOT(ChangeSex()));
    connect(btnage,SIGNAL(clicked()),this,SLOT(ChangeAge()));
    connect(btnscore,SIGNAL(clicked()),this,SLOT(ChangeScore()));

}

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

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

}

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

}

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

5.消息对话框

5.1Question 消息框

QT 基本对话框_第10张图片

5.2Information 消息框

 QT 基本对话框_第11张图片

5.3Warning 消息框

QT 基本对话框_第12张图片

5.4Critical 消息框

QT 基本对话框_第13张图片

5.5About 消息框

QT 基本对话框_第14张图片

5.6About  Qt 消息框

QT 基本对话框_第15张图片

 msgboxdlg.h

#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include 
#include 
#include 
#include 
#include 
#include 
class MsgBoxDlg : public QWidget
{
    Q_OBJECT
public:
    explicit MsgBoxDlg(QWidget *parent = nullptr);

signals:
private:
     QLabel *label;
     QPushButton  *btnquestion;
     QPushButton  *btninformation;
     QPushButton  *btnwarning;
     QPushButton  *btncritical;
     QPushButton  *btnabout;
     QPushButton  *btnQtabout;
     QGridLayout  *mainLayout;

private slots:
    void  showQuestionMsg();
    void  showInformationMsg();
    void  showWarningMSg();
    void  showCriticalMsg();
    void  showAboutMsg();
    void  showAboutQtMsg();

};

#endif // MSGBOXDLG_H

 msgboxdlg.cpp

#include "msgboxdlg.h"
#include 
MsgBoxDlg::MsgBoxDlg(QWidget *parent) : QWidget(parent)
{

    setWindowTitle(QStringLiteral("标准消息对话框实例"));
    label=new QLabel;
    label->setText(QStringLiteral("请选择一种消息框"));

    btnquestion=new QPushButton;
    btnquestion->setText(QStringLiteral("QuestionMSg"));

    btninformation=new QPushButton;
    btninformation->setText(QStringLiteral("InformationMSg"));

    btnwarning=new QPushButton;
    btnwarning->setText(QStringLiteral("WarningMSg"));

    btncritical=new QPushButton;
    btncritical->setText(QStringLiteral("CriticalMSg"));

    btnabout=new QPushButton;
    btnabout->setText(QStringLiteral("AboutMSg"));

    btnQtabout=new QPushButton;
    btnQtabout->setText(QStringLiteral("AboutQtMSg"));


    //布局

    mainLayout =new  QGridLayout(this);
    mainLayout->addWidget(label,0,0,1,2);
    mainLayout->addWidget(btnquestion,1,0);

    mainLayout->addWidget(btninformation,1,1);
    mainLayout->addWidget(btnwarning,2,0);

    mainLayout->addWidget(btncritical,2,1);
    mainLayout->addWidget(btnabout,3,0);

    mainLayout->addWidget(btnQtabout,3,1);

//信号与槽
    connect(btnquestion,&QPushButton::clicked,[this](){showQuestionMsg();});
    connect(btninformation,&QPushButton::clicked,[this](){showInformationMsg();});
    connect(btnwarning,&QPushButton::clicked,[this](){showWarningMSg();});
    connect(btncritical,&QPushButton::clicked,[this](){showCriticalMsg();});
    connect(btnabout,&QPushButton::clicked,[this](){showAboutMsg();});
    connect(btnQtabout,&QPushButton::clicked,[this](){showAboutQtMsg();});

}

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

}

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

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

    default:
        break;
    }
   return;
}

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

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

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

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