QT-对话框

内置对话框

QColorDialog,
QErrorMessage
QFileDialog
QFontDialog
QInputDialog
QMessageBox
QProgressDialog

标准对话框

#ifndef WIDGET_H
#define WIDGET_H

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

#include<QPushButton>
#include <QTextEdit>
#include <QDebug>

class Widget : public QWidget
{
    Q_OBJECT

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

public slots:
    void setcolorf()
    {
        QColor c =  QColorDialog::getColor();//这个QColorDialog类里的函数,返回一个颜色
        te->setTextColor(c);//文本编辑器的调用方法
    }
    void showerr()
    {
        QErrorMessage *msg = QErrorMessage::qtHandler();
        msg->showMessage("EEEEEEEE");
    }

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

        qDebug()<<filename;
        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
#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()
{

}

自定义对话框

 

#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
#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);//状态为真,显示QWidget窗口,状态为假,程序直接退出
}

Widget::~Widget()
{

}
#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();//关闭myQDialog窗口
    }

public:
    static int getstat()//给私有对象提供获取接口
    {
        myQDialog a;
        a.exec();

        return a.stat;
    }

private:
    QLineEdit *le;
    QPushButton *pb;

    int stat;
};

#endif // MYQDIALOG_H
//myqdialog.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()));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值