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. Solved Application Interface
Forum Updated to NodeBB v4.3 + New Features

Solved Application Interface

Scheduled Pinned Locked Moved General and Desktop
qt5
12 Posts 4 Posters 3.4k 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.
  • J Offline
    J Offline
    JasonB
    wrote on last edited by JasonB
    #1

    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.

    Joel BodenmannJ 1 Reply Last reply
    0
    • J JasonB

      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.

      Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      @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."

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JasonB
        wrote on last edited by JasonB
        #3

        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"));;

        }

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          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

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JasonB
            wrote on last edited by
            #5

            @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.

            M 1 Reply Last reply
            0
            • J JasonB

              @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.

              M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              @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

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              J 1 Reply Last reply
              0
              • M mcosta

                @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

                J Offline
                J Offline
                JasonB
                wrote on last edited by
                #7

                @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.

                JKSHJ M 2 Replies Last reply
                0
                • J JasonB

                  @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.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @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();
                  

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • Joel BodenmannJ Offline
                    Joel BodenmannJ Offline
                    Joel Bodenmann
                    wrote on last edited by
                    #9

                    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();
                    }
                    

                    Industrial process automation software: https://simulton.com
                    Embedded Graphics & GUI library: https://ugfx.io

                    J 1 Reply Last reply
                    1
                    • J JasonB

                      @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.

                      M Offline
                      M Offline
                      mcosta
                      wrote on last edited by
                      #10

                      @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

                      Once your problem is solved don't forget to:

                      • Mark the thread as SOLVED using the Topic Tool menu
                      • Vote up the answer(s) that helped you to solve the issue

                      You can embed images using (http://imgur.com/) or (http://postimage.org/)

                      1 Reply Last reply
                      0
                      • Joel BodenmannJ Joel Bodenmann

                        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();
                        }
                        
                        J Offline
                        J Offline
                        JasonB
                        wrote on last edited by
                        #11

                        @Joel-Bodenmann
                        Thanks.
                        Its working.

                        Joel BodenmannJ 1 Reply Last reply
                        0
                        • J JasonB

                          @Joel-Bodenmann
                          Thanks.
                          Its working.

                          Joel BodenmannJ Offline
                          Joel BodenmannJ Offline
                          Joel Bodenmann
                          wrote on last edited by
                          #12

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

                          Industrial process automation software: https://simulton.com
                          Embedded Graphics & GUI library: https://ugfx.io

                          1 Reply Last reply
                          0

                          • Login

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