嵌入式Qt 选择字体对话框QFontDialog-进度对话框 QProgressDialog-打印对话框QPrintDialog

本文介绍了如何在Qt中使用QFontDialog、QProgressDialog和QPrintDialog创建字体选择、进度指示和打印对话框,通过实例展示了它们的创建、属性设置和响应点击事件的过程。
摘要由CSDN通过智能技术生成

一.字体对话框

 字体对话框使用方式:

//构造字体对话框对象 并指定父组件
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()
{
    
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式_笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值