lv21 QT对话框3

1 内置对话框

标准对话框样式

内置对话框基类

复制代码
QColorDialog,
QErrorMessage
QFileDialog
QFontDialog
QInputDialog
QMessageBox
QProgressDialog

QDialog Class帮助文档

示例:各按钮激发对话框实现基类提供的各效果

第一步:实现组件布局()

第二步:实现信号与槽函数

文件静态成员方法

打印实现使用qdebug

代码:

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QTextEdit>

#include<QColorDialog>
#include<QErrorMessage>
#include<QFileDialog>
#include<QFontDialog>
#include<QInputDialog>
#include<QMessageBox>
#include<QProgressDialog>

#include <QDebug>


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
public slots:
    void setcolorf()
       {
           QColor c =  QColorDialog::getColor();
           te->setTextColor(c);
       }
       void showerr()
       {
           QErrorMessage *msg = QErrorMessage::qtHandler();
           msg->showMessage("EEEEEEEE");
       }

       void getfile()
       {
           QString filename = QFileDialog::getOpenFileName();

           qDebug()<<filename;    //qt中打印方法
           te->setText(filename);
       }

       void setfont()
       {
           bool ok;
           QFont myfont = QFontDialog::getFont(&ok);
           if(ok)
                te->setFont(myfont);
       }

       void getstr()
       {
            QString str =  QInputDialog::getText(this, "xxxx","yyyyy");
            te->setText(str);

       }

       void showmsg()
       {
           QMessageBox::information(this, "vvvv", "hello", "AAA");
       }

       void showprogress()
       {
              QProgressDialog p;
              p.setValue(50);
              p.exec();

       }

private:
    QPushButton *btcolor;
    QPushButton *bterrm;
    QPushButton *btfile;
    QPushButton *btfont;
    QPushButton *btinput;
    QPushButton *btmsg;
    QPushButton *btprg;

    QTextEdit *te;

};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include "QVBoxLayout"
#include "QHBoxLayout"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    btcolor = new QPushButton("setcolor");
    bterrm = new QPushButton("errmsg");
    btfile = new QPushButton("getfile");
    btfont = new QPushButton("setfont");
    btinput = new QPushButton("getstr");
    btmsg = new QPushButton("msg");
    btprg = new QPushButton("progress");

    te = new QTextEdit;

    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(btcolor);
    vbox->addWidget(bterrm);
    vbox->addWidget(btfile);
    vbox->addWidget(btfont);
    vbox->addWidget(btinput);
    vbox->addWidget(btmsg);
    vbox->addWidget(btprg);

    QHBoxLayout *mainbox = new QHBoxLayout;
    mainbox->addLayout(vbox);
    mainbox->addWidget(te);
    this->setLayout(mainbox);

    connect(btcolor, SIGNAL(clicked(bool)), this, SLOT(setcolorf()));
    connect(bterrm, SIGNAL(clicked(bool)), this, SLOT(showerr()));
    connect(btfile, SIGNAL(clicked(bool)), this, SLOT(getfile()));
    connect(btfont, SIGNAL(clicked(bool)), this, SLOT(setfont()));
    connect(btinput, SIGNAL(clicked(bool)), this, SLOT(getstr()));
    connect(btmsg, SIGNAL(clicked(bool)), this, SLOT(showmsg()));
    connect(btprg, SIGNAL(clicked(bool)), this, SLOT(showprogress()));


}

Widget::~Widget()
{

}

2 自定义对话框

现象:模态显示,前面不关后面不关,小框可以卡其后面的态

有时候对于弹出对话框可以点确定关闭,可以点X全部关闭,那么这时候需要实现自定义对话框

添加新文件,添加C++

文件添加 姓名,基类

生成myQdialog的cpp和.h文件,需要用到信号与槽,Q_OBJEC

myqdialog.h

cpp 复制代码
#ifndef MYQDIALOG_H
#define MYQDIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QPushButton>

class myQDialog : public QDialog
{
    Q_OBJECT
public:
    myQDialog();

public slots:
    void ok_pushed()
    {
        stat = true;
        close();
    }

public:
    static int getstat()
    {
        myQDialog a;
        a.exec();

        return a.stat;
     }

private:
    QLineEdit *le;
    QPushButton *pb;

    int stat;
};

#endif // MYQDIALOG_H

myqdialog.cpp

cpp 复制代码
#include "myqdialog.h"
#include <QVBoxLayout>

myQDialog::myQDialog()
{
    le = new QLineEdit("aaaaaa");
    pb = new QPushButton("OK");

    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(le);
    vbox->addWidget(pb);

    setLayout(vbox);

    stat = false;
    connect(pb, SIGNAL(clicked(bool)), this, SLOT(ok_pushed()));
}

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
};

#endif // WIDGET_H

widget.cpp

cpp 复制代码
#include "widget.h"
#include <QWidget>
#include <QDialog>
#include "myqdialog.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
#if 0
    myQDialog a;
    a.setFixedSize(100, 100);
    a.exec();

    if(!a.stat)
        exit(0);

#endif

    int s = myQDialog::getstat();

    if(!s)
          exit(0);
}

Widget::~Widget()
{

}
相关推荐
嵌入式-老费10 分钟前
自己动手写深度学习框架(快速学习python和关联库)
开发语言·python·学习
ctgu9016 分钟前
PyQt5(八):ui设置为可以手动随意拉伸功能
开发语言·qt·ui
CVer儿24 分钟前
libtorch ITK 部署 nnUNetV2 模型
开发语言
asyxchenchong88833 分钟前
OpenLCA、GREET、R语言的生命周期评价方法、模型构建
开发语言·r语言
没有梦想的咸鱼185-1037-16631 小时前
【生命周期评价(LCA)】基于OpenLCA、GREET、R语言的生命周期评价方法、模型构建
开发语言·数据分析·r语言
程序猿20231 小时前
Python每日一练---第三天:删除有序数组中的重复项
开发语言·python
一只游鱼1 小时前
Springboot+BannerBanner(启动横幅)
java·开发语言·数据库
一只游鱼1 小时前
抖音上的用python实现激励弹窗
开发语言·python
行走在电子领域的工匠2 小时前
2.2 常用控件
开发语言·python
散峰而望2 小时前
Dev-C++一些问题的处理
c语言·开发语言·数据库·c++·编辑器