我已经在此存储库中找到了大家好
https://github.com/dept2/Poppler-QML-plugin
用于将pdf文件显示为qml文件的qml插件,但是我不知道如何使用它,有人可以帮助我吗?
在ubuntu 18.04版本中,我发现此插件带有命令行sudo apt-cache search poppler,并且我已经安装了该软件包,但我遇到了同样的问题,如何使用它?
预先感谢
答案 0 :(得分:3)
有2种可能的方法:
要安装此软件包,我必须先为其安装插件,然后再下载项目,在项目目录中打开终端并执行以下操作:
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): ""
}
}
}
输出:
我创建了.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中,您可以找到一个示例。
注意:
该插件的初始代码有一个错误,因为如果它要求一个不存在的页面,则应用程序应返回QImagenulo,但由于不进行验证,因此该应用程序可能会被破坏
page = document->page(numPage -1);
if(!page)
return result;