Solved Application Interface



  • Hi,

    How can I open a certain file with the default application when a button is clicked?

    In other words what I want is to be able to open a file when a button is click.



  • @JasonB You can use QDesktopServices::openUrl(). From the documentation:

    "If the URL is a reference to a local file (i.e., the URL scheme is "file") then it will be opened with a suitable application instead of a Web browser."



  • This is my code ,I m trying to open a pdf file (ubuntu 12.04) but it is not opening.

    void MainWindow::on_pushButton_clicked()
    {
    // QProcess* process = new QProcess;
    // QString program = "/home/mytsp00042/Desktop/try.txt";
    // process->start(program);

    QDesktopServices::openUrl(QUrl::fromUserInput("file::/home/mytsp00042/Desktop/c-gui-programming-with-qt-4-2ndedition(1).pdf"));;

    }



  • Hi and welcome to devnet,

    for local files I suggest to use QUrl::fromLocalFile()
    BTW the to use file protocol you should use 3 slashes file:///home/mytsp00042/Desktop/c-gui-programming-with-qt-4-2ndedition(1).pdf



  • @mcosta Hey, i tried both.. none of them is working.
    void MainWindow::on_pushButton_clicked()
    {
    QProcess* process = new QProcess;
    QString program = "cmd.exe";
    process->execute(program);
    i tried this code on windows and it worked. but same code is not working on Ubuntu 12.04.



  • @JasonB said:

    @mcosta Hey, i tried both.. none of them is working.
    void MainWindow::on_pushButton_clicked()
    {
    QProcess* process = new QProcess;
    QString program = "cmd.exe";
    process->execute(program);
    i tried this code on windows and it worked. but same code is not working on Ubuntu 12.04.

    "cmd.exe" on Linux? (or I missed something)

    To make QDestokService::openUrl() working you must be sure your system has a default application for PDF files



  • @mcosta yeah it is having deafault pdf viewer

    QDesktopServices ::openUrl(QUrl::fromLocalFile("file::///home/mytsp00042/Desktop/c-gui-programming-with-qt-4-2ndedition(1).pdf"));

    i tried that code. no success.


  • Moderators

    @JasonB said:

    QDesktopServices ::openUrl(QUrl::fromLocalFile("file::///home/mytsp00042/Desktop/c-gui-programming-with-qt-4-2ndedition(1).pdf"));

    Is that a typo? You're only supposed to have 1 colon (':').

    Anyway, you can check if your QUrl is correct or not:

    QUrl url = QUrl::fromLocalFile("file:///home/mytsp00042/Desktop/c-gui-programming-with-qt-4-2ndedition(1).pdf");
    qDebug() << url.isValid();
    
    QString str = url.toLocalFile();
    QFile file(str);
    qDebug() << file.exists();
    


  • This Qt 5.4 program works very well for me on both Linux and Windows:

    #include <QApplication>
    #include <QString>
    #include <QDir>
    #include <QUrl>
    #include <QDesktopServices>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QString path = QDir::toNativeSeparators("/home/tectu/Desktop/foo.pdf");
        QDesktopServices::openUrl(QUrl("file:///" + path));
    
        return a.exec();
    }
    


  • @JasonB said:

    @mcosta yeah it is having deafault pdf viewer

    QDesktopServices ::openUrl(QUrl::fromLocalFile("file::///home/mytsp00042/Desktop/c-gui-programming-with-qt-4-2ndedition(1).pdf"));

    i tried that code. no success.

    you have to use a single colon : character in the URL



  • @Joel-Bodenmann
    Thanks.
    Its working.



  • @JasonB Glad to hear that.
    Can you please mark this thread as [SOLVED] and upvote the answer(s) that helped you solving your problem?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.