最近学习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
}
最后编译通过,程序可运行