gpt4 book ai didi

qt - 如何在我的应用程序中使用 Poppler-QML-plugin?

转载 作者:行者123 更新时间:2023-12-04 13:34:12 26 4
gpt4 key购买 nike

大家好,我在这个存储库中找到了

https://github.com/dept2/Poppler-QML-plugin

一个qml插件,用于将pdf文件显示为qml文件,但我不知道如何使用它有人可以帮助我吗?

在 ubuntu 18.04 版本中,我使用命令行 sudo apt-cache search poppler 找到了这个插件,并且我已经安装了该软件包,但我遇到了同样的问题,我该如何使用它?

提前致谢

最佳答案

有两种可能的方法:
1.编译安装插件:
要安装这个包我必须先安装插件下载项目,在项目目录中打开一个终端并执行以下命令:

qmake
make
sudo make install
然后在 .qml 中导入模块, Poppler项目提供 imageProvider所以你应该使用如下所示的图像:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.11
import org.docviewer.poppler 1.0 // <--- import
import QtQuick.Dialogs 1.3

Window {
id: win
visible: true
width: 640
height: 480
title: qsTr("Poppler Example")

function urlToPath(urlString) {
var s
if (urlString.startsWith("file:///")) {
var k = urlString.charAt(9) === ':' ? 8 : 7
s = urlString.substring(k)
} else {
s = urlString
}
return decodeURIComponent(s);
}

FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
nameFilters: ["PDF files (*.pdf)", "All files (*)"]
onAccepted: timer.running = true
Component.onCompleted: visible = true
}

Timer {
id: timer
interval: 100; repeat: false
onTriggered: {
poppler.path = urlToPath(""+fileDialog.fileUrl)
view.focus = true
}
}

Poppler{
id: poppler
}

ListView{
id: view
height: parent.height
width: 100
model: poppler.numPages
delegate: Image{
id: image
width: parent.width
source: poppler.loaded? "image://poppler/page/" + (modelData+1): ""
sourceSize.width: width
MouseArea{
anchors.fill: parent
onClicked: {
image.ListView.view.currentIndex = index
image.ListView.view.focus = true
}
}
}
}
Flickable {
height: parent.height
anchors.left: view.right
anchors.right: parent.right
contentWidth: bigImage.width;
contentHeight: bigImage.height
boundsBehavior: Flickable.StopAtBounds
Image{
id: bigImage
sourceSize.width: win.width - view.width
source: (poppler.loaded && view.currentIndex >= 0)? "image://poppler/page/"+(view.currentIndex+1): ""
}
}
}
输出:
enter image description here

2. 创建.pri
我创建了一个 .pri,这是一种将文件附加到项目的简单方法:
poppler-qml.pri:
INCLUDEPATH += $$PWD

SOURCES += \
$$PWD/pdfModel.cpp \
$$PWD/pageImageProvider.cpp

HEADERS += \
$$PWD/pdfModel.h \
$$PWD/pageImageProvider.h


unix|win32: LIBS += -lpoppler-qt5
文件必须具有以下结构:
poppler-qml
   ├── pageImageProvider.cpp
   ├── pageImageProvider.h
   ├── pdfModel.cpp
   ├── pdfModel.h
   └── poppler-qml.pri
然后将其添加到您的 .pro 中:
...
include(poppler-qml/poppler-qml.pri)
和 main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <pdfModel.h>

int main(int argc, char *argv[])
{
qputenv("POPPLERPLUGIN_DEBUG", "1"); /// enable debug
qmlRegisterType<PdfModel>("org.docviewer.poppler", 1, 0, "Poppler");
...
例如在下面 link你可以找到一个例子。
注:
插件的初始代码有一个错误,因为如果它要求一个不存在的页面,应用程序应该返回一个空 QImage 但由于它不进行验证,应用程序可能会被破坏
page = document->page(numPage -1);
if(!page)
return result;

关于qt - 如何在我的应用程序中使用 Poppler-QML-plugin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843694/

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