QT5 Qbs编译系统下添加打印机支持模块

最近学习QT5开发及实例(陆文周)第四版,从中用到了打印机

由于原文使用的是qmake,本人电脑系统使用时间较长,用qmake有bug,后发现使用Qbs能正常编译

使用打印机打印文字的代码如下

void ImgProcessor::ShowPrintText()
{
    QPrinter printer;							//新建一个QPrinter对象
    QPrintDialog printDialog(&printer,this);	//(a)
    if(printDialog.exec())						//(b)
    {
       //获得QTextEdit对象的文档
        QTextDocument *doc =showWidget->text->document();
        doc->print(&printer);					//打印
    }
}

添加头文件

#include <QPrintDialog>
#include <QPrinter>

其它略过,编译时,提示无法找到文件

原文给出的做法是在工程文件(.pro)中添加打印机支持


qmake:
QT += printsupport

然而Qbs编译系统中工程文件不是.pro,且文本为另一画风,后经个人努力尝试,发现Qbs中使用

Depends { name: "Qt.widgets" }替代qmake中的QT += widgets

遂在工程文件.qbs中相应位置插入

Depends { name: "Qt.printsupport" } 

工程文件如下:

import qbs.FileInfo

QtApplication {
    Depends { name: "Qt.widgets" }
    Depends { name: "Qt.printsupport" }     //添加打印机支持

    // The following define makes your compiler emit warnings if you use
    // any Qt feature that has been marked deprecated (the exact warnings
    // depend on your compiler). Please consult the documentation of the
    // deprecated API in order to know how to port your code away from it.
    // You can also make your code fail to compile if it uses deprecated APIs.
    // In order to do so, uncomment the second entry in the list.
    // You can also select to disable deprecated APIs only up to a certain version of Qt.
    cpp.defines: [
        "QT_DEPRECATED_WARNINGS",
        /* "QT_DISABLE_DEPRECATED_BEFORE=0x060000" */ // disables all the APIs deprecated before Qt 6.0.0
    ]

    files: [
        "main.cpp",
        "imgprocessor.cpp",
        "imgprocessor.h",
        "showwidget.cpp",
        "showwidget.h",
    ]

    install: true
    installDir: qbs.targetOS.contains("qnx") ? FileInfo.joinPaths("/tmp", name, "bin") : base
}

最后编译通过,程序可运行

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值