Important: Please read the Qt Code of Conduct -

[SOLVED]Open file with it default program

  • Hi guys...

    I need to open a file with it default program defined in the OS.

    I'm doing this to open a pdf:
    @qDebug() << QDesktopServices::openUrl(QUrl("file:///home/freddy/unordered.pdf", QUrl::TolerantMode));@
    and my program crash it . I test with this code too:
    @qDebug() << QDesktopServices::openUrl(QUrl("/home/freddy/unordered.pdf", QUrl::TolerantMode));@
    @qDebug() << QDesktopServices::openUrl(QUrl("file:///home/freddy/unordered.pdf"));@
    and the result is the same:
    @The program has unexpectedly finished.@
    I'm using Qt5.2.1 in Archlinux, but my app will run over Mac and Windows too,

    any advice please ?? thanks !!!

    best regards

  • Lifetime Qt Champion


    Can you try building your url using "fromLocalFile": ?

  • Hi SGaist !!! thanks for answer !!!

    I had try:
    @qDebug() << QDesktopServices::openUrl(QUrl::fromLocalFile("file:///home/freddy/unordered.pdf"));@

    but the program stil crash it !!!

  • Works for me on OSX (Qt-5.3-beta) and Ubuntu (Qt-5.2.1)
    qDebug() << QDesktopServices::openUrl(QUrl::fromLocalFile("/home/user/tdd.pdf"));
    qDebug() << QDesktopServices::openUrl(QUrl("file:///home/user/GUIdesign.pdf", QUrl::TolerantMode));

    Do you have a core file? Could you post a backtrace log here.

    You don't need to add "file://" if you use @QUrl::fromLocalFile(...)@

  • yes...

    my core file is only:

    #include <QDesktopServices>

    int main()
    qDebug() << QDesktopServices::openUrl(QUrl::fromLocalFile("file:///home/user/unordered.pdf"));
    return 0;

    and the backtrace log just tell me

    @Starting /home/freddy/Trabajo/bpg/andy&pedro/proyecto/build-adviser-Clang-Debug/adviser...
    QML debugging is enabled. Only use this in a safe environment.
    The program has unexpectedly finished.
    /home/freddy/Trabajo/bpg/andy&pedro/proyecto/build-adviser-Clang-Debug/adviser crashed@

  • You need to initialize QApplication in order in use QDesktopServices

    Try this
    #include <QApplication>
    #include <QDebug>
    #include <QUrl>
    #include <QDesktopServices>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    qDebug() << QDesktopServices::openUrl(QUrl::fromLocalFile&#40;"/home/user/tdd.pdf"&#41;);
    qDebug() << QDesktopServices::openUrl(QUrl("file:///home/user/GUIdesign.pdf", QUrl::TolerantMode));
    return 0;



  • That right andreyc

    Thank you for your help guys !!!

    It's work !!!

  • @ QString str(QDir::currentPath());

Log in to reply