Qt 21 standard dialog-select font dialog QFontDialog, progress dialog QProgressDialog, print dialog QPrintDialog

Select font dialog QFontDialog

Qt provides a predefined font dialog QFontDialog class, which is used to provide dialog components for selecting fonts

How to use the font dialog

//构造字体对话框对象 并指定父组件
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 provides a predefined progress dialog box QProgressDialog class
QProgressDialog class is used to display progress information
QProgressDialog class is used in occasions where the user needs to wait

The use of QProgressDialog progress dialog class

//构造进度对话框 并指定父窗口
QProgressDialog dlg(this)

//设置进度对话框的相关属性
dlg.setWindowTitle("Updating...");//标题
dlg.setLabelText("Downloading from server...");//提示性字符串信息
dlg.setMinimum(0);//设置最小进度值
dlg.setMaximum(1000);//设置最小进度值

dlg.exec();

Print dialog box QPrintDialog class
Qt provides a predefined print dialog box QPrintDialog class, which is used to set print-related parameter information

How to use the print dialog

//构造打印对话框对象
QPrintDialog dlg(this);

//设置打印对话框的相关属性
dlg.setWindowTitle("Print Dialog");//标题

if( dlg.exec() == QPrintDialog::Accepted )
    {
        QPrinter* p = dlg.printer();
    }

The QPrinter class in Qt is an encapsulation of the printing device and its parameters. The
QPrinter class encapsulates the driver interface of the printing device in the system.
QPrinter uses different printing devices in the system in the same way.

QTextDocument: text document class

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();
}

Insert picture description here
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/115024303