选择字体对话框QFontDialog
Qt 中提供了预定义的字体对话框 QFontDialog 类,用于提供选择字体的对话框部件
字体对话框使用方式
//构造字体对话框对象 并指定父组件
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
Qt中提供了 预定义的进度对话框 QProgressDialog 类
QProgressDialog 类用于显示进度信息
QProgressDialog 类用于需要用户等待的场合
QProgressDialog 进度对话框类的使用
//构造进度对话框 并指定父窗口
QProgressDialog dlg(this)
//设置进度对话框的相关属性
dlg.setWindowTitle("Updating...");//标题
dlg.setLabelText("Downloading from server...");//提示性字符串信息
dlg.setMinimum(0);//设置最小进度值
dlg.setMaximum(1000);//设置最小进度值
dlg.exec();
打印对话框QPrintDialog类
Qt中提供了预定义的打印对话框 QPrintDialog类,用于设置打印相关的参数信息
打印对话框使用方式
//构造打印对话框对象
QPrintDialog dlg(this);
//设置打印对话框的相关属性
dlg.setWindowTitle("Print Dialog");//标题
if( dlg.exec() == QPrintDialog::Accepted )
{
QPrinter* p = dlg.printer();
}
Qt中的QPrinter 类是打印设备及其参数的封装
QPrinter类封装了系统中打印设备的驱动接口
QPrinter以相同方式使用系统中的不同打印设备
QTextDocument:文本文档类
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));//设置初始字体,大小为10 粗体
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();//创建QPrinter对象
QTextDocument td;//创建被打印的对象 QTextDocument文本文档类
//td.setPlainText("Printer object test!");//向文本文档中写入内容
td.setHtml("<h1>Print html object test</hl>");
p->setOutputFileName("D:\\test.xps");//打印到指定文件
td.print(p);//将QTextDocument文本文档类对象 打印到 QPrinter对象中
}
}
Widget::~Widget()
{
}
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();
}