gpt4 book ai didi

c++ - 如何用QPrinter打印文本文件

转载 作者:行者123 更新时间:2023-11-30 01:55:48 25 4
gpt4 key购买 nike

我在 TreeView 中有一个文本文件列表(使用 QFileSystemModel)。如果选择了文本文件并按下了打印按钮。它应该显示一个打印对话框并且应该打印出文件。我想(在阅读文档和示例之后)它应该是这样的:

void berichtenhistorie::on_printButton_released()
{
QModelIndex index = ui->treeView->currentIndex();
QFileSystemModel *model = (QFileSystemModel*)ui->treeView->model();

QString path = model->filePath(index);
QString name = model->fileName(index);

QString dir = path;
dir.remove(dir.size() - name.size(), name.size());
QFile file(path);

if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
file.close();
if(file.rename(QString("%1geprint %2").arg(dir, name)))
qDebug() << "renamed";
}
//all above works correctly

QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
printer.setPageMargins (15,15,15,15,QPrinter::Millimeter);
printer.setFullPage(false);
printer.setOutputFileName(path);
printer.setOutputFormat(QPrinter::NativeFormat);
QPainter painter(&printer);
painter.end();
}

重命名部分(最重要的是打印内容)正常工作,没有错误或任何错误。但是我在打印错误时遇到了很多错误。我认为这是因为库的原因,因为我使用的是 Qt5。

#include <QDirModel>
#include <QDebug>
#include <QMessageBox>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QFile>

以下是错误: enter image description here

最佳答案

显然您使用的是 Qt5,其中打印功能已放置在单独的插件中(在 Qt4 中它是 QtGui 模块的一部分),参见 documentation .所以你必须将这一行添加到 pro 文件中:

QT += printsupport

这将修复您的构建错误,但您的代码尚未打印。你必须使用 painter

如果你打算支持Qt4,应该是这样的

greaterThan(QT_MAJOR_VERSION, 4) {
QT += printsupport
}

关于c++ - 如何用QPrinter打印文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com