一.字体对话框
字体对话框使用方式:
//构造字体对话框对象 并指定父组件
QFontDialog dlg(this);
//设置字体对话框的相关属性
dlg.setWindowTitle("Font Editor");//标题
dlg.setCurrentFont(
QFont("Courier New", 10, QFont::Bold)//设置初始字体,大小为10 粗体
);
if(dlg.exec() == QFontDialog::Accepted)
{
qDebug() << dlg.selectedFont();
}
二.进度对话框
进度对话框的使用:
//构造进度对话框 并指定父窗口
QProgressDialog dlg(this)
//设置进度对话框的相关属性
dlg.setWindowTitle("Updating...");//标题
dlg.setLabelText("Downloading from server...");//提示性字符串信息
dlg.setMinimum(0);//设置最小进度值
dlg.setMaximum(1000);//设置最小进度值
dlg.exec();
三.打印对话框
打印对话框类的使用:
//构造进度对话框 并指定父窗口
QProgressDialog dlg(this)
//设置进度对话框的相关属性
dlg.setWindowTitle("Updating...");//标题
dlg.setLabelText("Downloading from server...");//提示性字符串信息
dlg.setMinimum(0);//设置最小进度值
dlg.setMaximum(1000);//设置最小进度值
dlg.exec();
四.对话框代码实现
main.cpp:
#include <QtGui/QApplication>
#include "Widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Widget.h:
#ifndef _WIDGET_H_
#define _WIDGET_H_
#include <QtGui/QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton FontDialogBtn;
QPushButton ProgressDialogBtn;
QPushButton PrintDialogBtn;
private slots:
void FontDialogBtn_Clicked();
void PrintDialogBtn_Clicked();
void ProgressDialogBtn_Clicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif
Widget.cpp:
#include "Widget.h"
#include <QDebug>
#include <QPrinter>
#include <QTextDocument>
#include <QPrintDialog>
#include <QProgressDialog>
#include <QFontDialog>
Widget::Widget(QWidget *parent) : QWidget(parent),
FontDialogBtn(this), ProgressDialogBtn(this), PrintDialogBtn(this)
{
FontDialogBtn.setText("Font Dialog");
FontDialogBtn.move(20, 20);
FontDialogBtn.resize(160, 30);
ProgressDialogBtn.setText("Progress Dialog");
ProgressDialogBtn.move(20, 70);
ProgressDialogBtn.resize(160, 30);
PrintDialogBtn.setText("Print Dialog");
PrintDialogBtn.move(20, 120);
PrintDialogBtn.resize(160, 30);
resize(200, 170);
setFixedSize(200, 170);
connect(&FontDialogBtn, SIGNAL(clicked()), this, SLOT(FontDialogBtn_Clicked()));
connect(&ProgressDialogBtn, SIGNAL(clicked()), this, SLOT(ProgressDialogBtn_Clicked()));
connect(&PrintDialogBtn, SIGNAL(clicked()), this, SLOT(PrintDialogBtn_Clicked()));
}
void Widget::FontDialogBtn_Clicked()
{
QFontDialog dlg(this);
dlg.setWindowTitle("Font Dialog Test");
dlg.setCurrentFont(QFont("Courier New", 10, QFont::Bold));
if( dlg.exec() == QFontDialog::Accepted )
{
qDebug() << dlg.selectedFont();
}
}
void Widget::ProgressDialogBtn_Clicked()
{
QProgressDialog dlg(this);
dlg.setWindowTitle("Updating...");
dlg.setLabelText("Downloading update from server...");
dlg.setMinimum(0);
dlg.setMaximum(100);
dlg.setValue(35);
// create a new thread
dlg.exec();
}
void Widget::PrintDialogBtn_Clicked()
{
QPrintDialog dlg(this);
dlg.setWindowTitle("Print Dialog Test");
if( dlg.exec() == QPrintDialog::Accepted )
{
QPrinter* p = dlg.printer();
QTextDocument td;
//td.setPlainText("Printer object test!");
td.setHtml("<h1>Print html object test</hl>");
p->setOutputFileName("D:\\test.xps");
td.print(p);
}
}
Widget::~Widget()
{
}