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()
{

}
相关推荐
Lhan.zzZ1 小时前
笔记_2026.4.28_004
c++·ide·笔记·qt
MATLAB代码顾问2 小时前
5大智能算法优化标准测试函数对比(Python实现)
开发语言·python
万粉变现经纪人3 小时前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
清风明月一壶酒3 小时前
OpenClaw自动处理Word文档全流程
开发语言·c#·word
其实防守也摸鱼3 小时前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学
小郑加油4 小时前
python学习Day12:pandas安装与实际运用
开发语言·python·学习
AC赳赳老秦4 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
KuaCpp5 小时前
C++面向对象(速过复习版)
开发语言·c++
wbs_scy5 小时前
Linux线程同步与互斥(三):线程同步深度解析之POSIX 信号量与环形队列生产者消费者模型,从原理到源码彻底吃透
java·开发语言
2zcode5 小时前
基于MATLAB元胞自动机(CA)的AZ80A镁合金动态再结晶(DRX)过程模拟
开发语言·matlab·动态再结晶