qt-10基本对话框(文件--颜色--字体)

基本对话框--输入对话框

Dialog

Dialog.h

cpp 复制代码
#ifndef DIALOG_H
#define DIALOG_H


#include "exdialog.h"

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



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* ExPutBtn;
    ExDialog* Exdialog;

private slots:
    void ShowFile();
    void ShowColor();
    void ShowFont();
    void ShowExPutDlg();

};
#endif // DIALOG_H

Dialog.cpp

cpp 复制代码
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //实例化组件对象
    //文件
    FileBtn = new QPushButton(tr("文件标准对话框"));
    FileLineEdit = new QLineEdit;
    //颜色
    ColorBtn = new QPushButton(tr("颜色标准对话框"));
    ColorFrame = new QFrame;
    ColorFrame->setFrameShape(QFrame::Box);
    ColorFrame->setAutoFillBackground(true);
    //字体
    FontBtn = new QPushButton(tr("字体标准对话框实例"));
    FontLineEdit = new QLineEdit(tr("welcome!"));
    //弹出输入框的Dialog
    ExPutBtn = new QPushButton(tr("标准输入对话框实例"));


    //布局
    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(ExPutBtn,3,0,1,2);


    //添加事件--点击按钮的时候
    connect(FileBtn,SIGNAL(clicked()),this,SLOT(ShowFile()));
    connect(ColorBtn,SIGNAL(clicked()),this,SLOT(ShowColor()));
    connect(FontBtn,SIGNAL(clicked()),this,SLOT(ShowFont()));
    connect(ExPutBtn,SIGNAL(clicked()),this,SLOT(ShowExPutDlg()));



}

Dialog::~Dialog() {}

void Dialog::ShowFile()
{
    QString s = QFileDialog::getOpenFileName(this,QObject::tr("打开文件"),"/");
    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::ShowExPutDlg()
{
    Exdialog = new ExDialog(this);
    Exdialog->show();
}

exdialog.h

exdialog.h

cpp 复制代码
#ifndef EXDIALOG_H
#define EXDIALOG_H

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

class ExDialog : public QDialog
{
    Q_OBJECT
public:
    explicit ExDialog(QWidget *parent = nullptr);
private slots:
    void ChangName();
    void ChangSex();
    void ChangAge();
    void ChangScore();
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* ExLayout;
signals:
};

#endif // EXDIALOG_H

exdialog.cpp

cpp 复制代码
#include "exdialog.h"

ExDialog::ExDialog(QWidget *parent)
    : QDialog{parent}
{
    setWindowTitle(tr("标准输入对话框"));
    //实例化标签和按钮
    NameLabel1 = new QLabel(tr("姓名:"));
    NameLabel2 = new QLabel(tr("小明"));
    NameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    NameBtn = new QPushButton(tr("修改姓名"));

    SexLabel1 = new QLabel(tr("性别:"));
    SexLabel2 = new QLabel(tr("男"));
    SexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    SexBtn = new QPushButton(tr("修改性别"));

    AgeLabel1 = new QLabel(tr("年龄:"));
    AgeLabel2 = new QLabel(tr("21"));
    AgeLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    AgeBtn = new QPushButton(tr("修改年龄"));

    ScoreLabel1 = new QLabel(tr("成绩:"));
    ScoreLabel2 = new QLabel(tr("80"));
    ScoreLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    ScoreBtn = new QPushButton(tr("修改成绩"));
    //布局
    ExLayout = new QGridLayout(this);
    ExLayout->setMargin(15);
    ExLayout->setSpacing(10);

    ExLayout->addWidget(NameLabel1,0,0);
    ExLayout->addWidget(NameLabel2,0,1);
    ExLayout->addWidget(NameBtn,0,2);

    ExLayout->addWidget(SexLabel1,1,0);
    ExLayout->addWidget(SexLabel2,1,1);
    ExLayout->addWidget(SexBtn,1,2);

    ExLayout->addWidget(AgeLabel1,2,0);
    ExLayout->addWidget(AgeLabel2,2,1);
    ExLayout->addWidget(AgeBtn,2,2);

    ExLayout->addWidget(ScoreLabel1,3,0);
    ExLayout->addWidget(ScoreLabel2,3,1);
    ExLayout->addWidget(ScoreBtn,3,2);

    //绑定槽
    connect(NameBtn,SIGNAL(clicked()),this,SLOT(ChangName()));
    connect(SexBtn,SIGNAL(clicked()),this,SLOT(ChangSex()));
    connect(AgeBtn,SIGNAL(clicked()),this,SLOT(ChangAge()));
    connect(ScoreBtn,SIGNAL(clicked()),this,SLOT(ChangScore()));



}

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

void ExDialog::ChangSex()
{
    QStringList SexItems;
    SexItems <<tr("男") <<tr("女");
    bool Ok;
    QString SexItem= QInputDialog::getItem(this,tr("标准条目选择对话框"),tr("请选择性别:"),SexItems,0,false,&Ok);
    if(Ok && !SexItem.isEmpty())
    {
        SexLabel2->setText(SexItem);
    }

}

void ExDialog::ChangAge()
{
    bool Ok;
    int nAge =QInputDialog::getInt(this,tr("标准int输入对话框"),tr("请输入年龄:"),AgeLabel2->text().toInt(&Ok),0,100,1,&Ok);
    if(Ok)
    {
        AgeLabel2->setText(QString(tr("%1").arg(nAge)));

    }

}

void ExDialog::ChangScore()
{
    bool Ok;
    double nScore =QInputDialog::getDouble(this,tr("标准int输入对话框"),tr("请输入年龄:"),ScoreLabel2->text().toDouble(&Ok),0,200,1,&Ok);
    if(Ok)
    {
        ScoreLabel2->setText(QString(tr("%1").arg(nScore)));

    }

}

运行图

文件对话框

颜色对话框

字体对话框

输入对话框-字符串

输入对话框-选择 Item

输入对话框-数字

相关推荐
伤不起bb36 分钟前
Redis 哨兵模式
数据库·redis·缓存
派阿喵搞电子37 分钟前
在UI界面内修改了对象名,在#include “ui_mainwindow.h“没更新
c++·qt·ubuntu·ui
卑微的Coder38 分钟前
Redis Set集合命令、内部编码及应用场景(详细)
java·数据库·redis
2501_9153738838 分钟前
Redis线程安全深度解析:单线程模型的并发智慧
数据库·redis·安全
呼拉拉呼拉41 分钟前
Redis知识体系
数据库·redis·缓存·知识体系
霖檬ing42 分钟前
Redis——主从&哨兵配置
数据库·redis·缓存
C++ 老炮儿的技术栈2 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
wgslucky2 小时前
Dubbo报错:module java.base does not “opens java.lang“ to unnamed module
java·开发语言·dubbo
whyeekkk2 小时前
python打卡第48天
开发语言·python
DougLiang3 小时前
关于easyexcel动态下拉选问题处理
java·开发语言