Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Opening PDF in QML (or running an external application)
Qt 6.11 is out! See what's new in the release blog

Opening PDF in QML (or running an external application)

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 6.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darkmalex
    wrote on last edited by aha_1980
    #1

    Hi, I'm working on an application for Embedded Linux (RaspPi, to be exact), but currently I'm compiling it for Windows, so I can have a "proof_of_concept" app. One of the features of the app should be working with (or at least rendering properly) container files (e.g PDF, DJVU). Obviously there is no obvious (well, at least for me) way to open and work with those in qml. One solution would be to run an external app, like Adobe Reader (not the best solution, but after all the trouble I went through, it's seems ok with me), but I can't seem to do even that. I probably made some stupid mistake, but I still haven't caught it yet.

    Here is my shell.h:

    @#ifndef SHELL_H
    #define SHELL_H

    #include <QObject>
    #include <QProcess>

    class Shell: public QObject
    {
    Q_OBJECT
    public:
    explicit Shell(QObject *parent = 0);
    ~Shell();
    Q_INVOKABLE void launch(const QString &program);

    protected:
    QProcess *m_process;

    };

    #endif // SHELL_H@

    Here is shell.cpp:

    @#include "shell.h"
    #include <QProcess>

    Shell::Shell(QObject *parent) :
    QObject(parent),
    m_process(new QProcess(this))
    {

    }

    void Shell::launch(const QString &program)
    {
    m_process->start(program);
    m_process->waitForFinished(-1);

    }

    Shell::~Shell() {

    }@

    Here is the line in main.cpp with witch I added shell to qml:

    @qmlRegisterType<QObject>("Shell", 1, 0, "Shell");@

    And here is the qml, which is loaded via Loader item:

    @import QtQuick 2.2
    import Shell 1.0

    Rectangle{
    Component.onCompleted: qprocess.launch("some_totaly_accurate_path_to_an_exe_application")
    Shell{
    id: qprocess
    }
    }@

    Aside from not launching app and giving me: TypeError: Property 'launch' of object QObject(0x4202448) is not a function
    everything else seems to be ok.

    So my questions are:
    1)What is the problem with the code, and how can it be improved.
    2)Would this code be any different on RaspPi (for example, would I be able to work with somthing like "xpdf name_of_my_pdf_file.pdf", or should the code be tweaked)
    3) But the major question is: are there any alternatives? Actualy I'm not restricted to pdf or anything like it. I just need a file that can be like an ebook(have markup, images and so on), so basicly html would also do the trick, but I don't think QWebView would work on embeded linux, and Rich Text is not quite that( no easy way to have images nearby). So any suggestions? Thanks in advance, an sorry for English.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You have 2 systems that are built into Qt and guaranteed to work on supported platforms:

      • in C++, there is "QDesktopServices":http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl
      • in QML, there is "Qt.openUrlExternally":http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#openUrlExternally-method

      You can feed those methods basically anything, and it will be opened by the native app for a given file, or the web browser.

      (Z(:^

      S 1 Reply Last reply
      0
      • D Offline
        D Offline
        Darkmalex
        wrote on last edited by
        #3

        [quote author="sierdzio" date="1404586826"]You have 2 systems that are built into Qt and guaranteed to work on supported platforms:

        • in C++, there is "QDesktopServices":http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl
        • in QML, there is "Qt.openUrlExternally":http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#openUrlExternally-method

        You can feed those methods basically anything, and it will be opened by the native app for a given file, or the web browser.[/quote]

        Thank you, openUrlExternally() is a life saver (though I can't seem to make it work either, but nothing I would not be able to handle myself :) )

        Still, I'm curious about the 3rd question. Basicly, what I want, is a ebook reader with images and interactivity. Openning a file in an external program is the most easy and obvious one, but not elegant, and I might have problems with application owners. Since it is a project that is thought out and made entirely by me for schools and educational purposes, I have freedom to chose which format will be used for books, therefore I'm looking towards HTML (or XML) but I can't think of a way to work with those on embeded linux (that doesn't mean that that is imposible). I would be happy if someone would give me a hint on what shoud i do in this situation

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aabc
          wrote on last edited by
          #4

          Hi Darkmalex,
          I suggest you to use the Poppler lib to open and view PDF documents.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Darkmalex
            wrote on last edited by
            #5

            [quote author="aabc" date="1404632512"]Hi Darkmalex,
            I suggest you to use the Poppler lib to open and view PDF documents. [/quote]

            Thanks. Might give it another try, because my previous attempts to work with poppler were not that good, but it still seems like the only option I have left to embed books into my app.

            1 Reply Last reply
            0
            • sierdzioS sierdzio

              You have 2 systems that are built into Qt and guaranteed to work on supported platforms:

              • in C++, there is "QDesktopServices":http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl
              • in QML, there is "Qt.openUrlExternally":http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#openUrlExternally-method

              You can feed those methods basically anything, and it will be opened by the native app for a given file, or the web browser.

              S Offline
              S Offline
              Shubham Gupta
              wrote on last edited by
              #6

              @sierdzio
              When i use Qt.openUrlExternally() it gives below message.

              The specified location is not supported

              Shubham Gupta

              sierdzioS 1 Reply Last reply
              0
              • S Shubham Gupta

                @sierdzio
                When i use Qt.openUrlExternally() it gives below message.

                The specified location is not supported

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                @Shubham-Gupta said in Opening PDF in QML (or running an external application):

                @sierdzio
                When i use Qt.openUrlExternally() it gives below message.

                The specified location is not supported

                What path / URL are you opening? On which Operating System? With which Qt version?

                (Z(:^

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Shubham Gupta
                  wrote on last edited by
                  #8

                  Operating system: Linux
                  Path: My local machine

                  Shubham Gupta

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    OK, but what path, specifically? Please paste the code.

                    Note: that methods expects an URL, so a path should have file:// prefix.

                    (Z(:^

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      Shubham Gupta
                      wrote on last edited by
                      #10

                      Like
                      Qt.openUrlExternally("file://Provisional Degree Certificate")
                      I used this.
                      and gave below error.

                      gio: file:./: Operation not supported

                      Shubham Gupta

                      jsulmJ 1 Reply Last reply
                      0
                      • S Shubham Gupta

                        Like
                        Qt.openUrlExternally("file://Provisional Degree Certificate")
                        I used this.
                        and gave below error.

                        gio: file:./: Operation not supported

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #11

                        @Shubham-Gupta said in Opening PDF in QML (or running an external application):

                        file://Provisional Degree Certificate

                        Try to use absolute path (I guess it is somewhere in your home directory?)
                        Also: is this a file name? There is no file extension, not sure how the system should decide how to open it?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        5
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi,

                          Also, you should use QUrl::fromLocalFile. It will build the proper URL.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          3

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved