1 内置对话框
标准对话框样式
内置对话框基类
QColorDialog,
QErrorMessage
QFileDialog
QFontDialog
QInputDialog
QMessageBox
QProgressDialog
QDialog Class帮助文档
示例:各按钮激发对话框实现基类提供的各效果
第一步:实现组件布局()
第二步:实现信号与槽函数
文件静态成员方法
打印实现使用qdebug
代码:
#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_OBJECTpublic: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
#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
#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
#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
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>class Widget : public QWidget
{Q_OBJECTpublic: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 0myQDialog a;a.setFixedSize(100, 100);a.exec();if(!a.stat)exit(0);#endifint s = myQDialog::getstat();if(!s)exit(0);
}Widget::~Widget()
{}