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. how to open a file in my qt app via double clicking on it
Forum Updated to NodeBB v4.3 + New Features

how to open a file in my qt app via double clicking on it

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 5 Posters 3.9k Views 2 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.
  • eyllanescE eyllanesc

    @_-mohamed-_ No, precisely QCommandLineParser considers that it is not necessary and only obtains the arguments, not the name of the program. If you want to know the name of the executable then use QFileInfo(QCoreApplication::applicationFilePath()).fileName().

    The format is:

    executable arg1 arg2 ... argN
    

    The executable name is not a program argument for QCommandLineParser.

    _ Offline
    _ Offline
    _-mohamed-_
    wrote on last edited by
    #17

    @eyllanesc oh,
    I thought it was likeQCoreApplication::arguments()

    I need the second argument which is the path of the file I want to open

    eyllanescE 1 Reply Last reply
    0
    • _ _-mohamed-_

      @eyllanesc oh,
      I thought it was likeQCoreApplication::arguments()

      I need the second argument which is the path of the file I want to open

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #18

      @_-mohamed-_ If you want to pass arguments using Qt Creator then click on the "Projects" on the left side and press the "Run" button, then put the arguments in "Command Line Arguments"

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      _ 1 Reply Last reply
      3
      • eyllanescE eyllanesc

        @_-mohamed-_ If you want to pass arguments using Qt Creator then click on the "Projects" on the left side and press the "Run" button, then put the arguments in "Command Line Arguments"

        _ Offline
        _ Offline
        _-mohamed-_
        wrote on last edited by _-mohamed-_
        #19

        @eyllanesc oh thanks this information will help me and save my time.

        I tried to get the argument using QCommandLineParser, I got the same result
        (C:\Users\moham\OneDrive\Desktop\????.txt)

        note: when I open the same file using windows notepad it workes fine.

        this is main.cpp:

        #include "mainwindow.h"
        #include <QApplication>
        #include <QDebug>
        using namespace std;
        
        int main(int argc, char *argv[])
        {    
            QApplication a(argc, argv);
        
            QCommandLineParser parser;
            parser.process(a);
        
            QStringList args = parser.positionalArguments();
        
            MainWindow w;
            w.settext(args.at(0));
        
            w.show();
            return a.exec();
        }
        

        and this is settext() function from mainwindow.cpp:

        void MainWindow::settext(QString URL)
        {
            QStringList arguments = QCoreApplication::arguments();
            if(arguments.count() > 1)
            {
        
                QFile file(URL);
        
                if(!file.open(QFile::ReadOnly | QFile::Text))
                    QMessageBox::information(this, "can't open file", "can't open the specified file");
        
                else
                {
                    QTextStream text(&file);
                    text.setCodec("UTF-8");
                    QString fileContents = text.readAll();
                    ui->TextEdit->setPlainText(fileContents);
                    file.close();
        
                    edited = false;
                    url = URL;
                    changeTitle();
                }
            }
        }
        

        when i double click the text file i got error(QMessageBox::information(this, "can't open file", "can't open the specified file");)

        JonBJ 1 Reply Last reply
        0
        • _ _-mohamed-_

          @eyllanesc oh thanks this information will help me and save my time.

          I tried to get the argument using QCommandLineParser, I got the same result
          (C:\Users\moham\OneDrive\Desktop\????.txt)

          note: when I open the same file using windows notepad it workes fine.

          this is main.cpp:

          #include "mainwindow.h"
          #include <QApplication>
          #include <QDebug>
          using namespace std;
          
          int main(int argc, char *argv[])
          {    
              QApplication a(argc, argv);
          
              QCommandLineParser parser;
              parser.process(a);
          
              QStringList args = parser.positionalArguments();
          
              MainWindow w;
              w.settext(args.at(0));
          
              w.show();
              return a.exec();
          }
          

          and this is settext() function from mainwindow.cpp:

          void MainWindow::settext(QString URL)
          {
              QStringList arguments = QCoreApplication::arguments();
              if(arguments.count() > 1)
              {
          
                  QFile file(URL);
          
                  if(!file.open(QFile::ReadOnly | QFile::Text))
                      QMessageBox::information(this, "can't open file", "can't open the specified file");
          
                  else
                  {
                      QTextStream text(&file);
                      text.setCodec("UTF-8");
                      QString fileContents = text.readAll();
                      ui->TextEdit->setPlainText(fileContents);
                      file.close();
          
                      edited = false;
                      url = URL;
                      changeTitle();
                  }
              }
          }
          

          when i double click the text file i got error(QMessageBox::information(this, "can't open file", "can't open the specified file");)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #20

          @_-mohamed-_
          So why not debug out URL and the file path from QFile file for yourself? Do you mean that filename really has ???? in it??

          _ 1 Reply Last reply
          0
          • JonBJ JonB

            @_-mohamed-_
            So why not debug out URL and the file path from QFile file for yourself? Do you mean that filename really has ???? in it??

            _ Offline
            _ Offline
            _-mohamed-_
            wrote on last edited by
            #21

            @JonB no the real file name is "عربي.txt" but i can't get the real name from app's argument

            JonBJ 1 Reply Last reply
            0
            • _ _-mohamed-_

              @JonB no the real file name is "عربي.txt" but i can't get the real name from app's argument

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #22

              @_-mohamed-_
              Look at file.errorString() when the file.open() fails. And print out URL and file.fileName().

              _ 1 Reply Last reply
              0
              • JonBJ JonB

                @_-mohamed-_
                Look at file.errorString() when the file.open() fails. And print out URL and file.fileName().

                _ Offline
                _ Offline
                _-mohamed-_
                wrote on last edited by
                #23

                @JonB i printed the URL and i got (C:\Users\moham\OneDrive\Desktop????.txt) as i said in the previous post
                So file.open () can't open a strange text (????.txt)

                Christian EhrlicherC 1 Reply Last reply
                0
                • _ _-mohamed-_

                  @JonB i printed the URL and i got (C:\Users\moham\OneDrive\Desktop????.txt) as i said in the previous post
                  So file.open () can't open a strange text (????.txt)

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #24

                  @_-mohamed-_ said in how to open a file in my qt app via double clicking on it:

                  i printed the URL and i got (C:\Users\moham\OneDrive\Desktop????.txt) as i said in the previous post

                  You must not print non-ascii characters on a windows console - windows is too stupid for this. You QMessageBox or similar.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  _ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @_-mohamed-_ said in how to open a file in my qt app via double clicking on it:

                    i printed the URL and i got (C:\Users\moham\OneDrive\Desktop????.txt) as i said in the previous post

                    You must not print non-ascii characters on a windows console - windows is too stupid for this. You QMessageBox or similar.

                    _ Offline
                    _ Offline
                    _-mohamed-_
                    wrote on last edited by
                    #25

                    @Christian-Ehrlicher I have set the text in QTextEdit using ui->TextEdit->setPlainText (URL)

                    JonBJ 1 Reply Last reply
                    0
                    • _ _-mohamed-_

                      @Christian-Ehrlicher I have set the text in QTextEdit using ui->TextEdit->setPlainText (URL)

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #26

                      @_-mohamed-_
                      I said:

                      Look at file.errorString() when the file.open() fails.

                      _ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @_-mohamed-_
                        I said:

                        Look at file.errorString() when the file.open() fails.

                        _ Offline
                        _ Offline
                        _-mohamed-_
                        wrote on last edited by
                        #27

                        @JonB why? I said that file.open() fail because the path is incorrect

                        ok now I tried file.errorString() and it returns The filename, directory name, or volume label syntax is incorrect.

                        1 Reply Last reply
                        0
                        • _ Offline
                          _ Offline
                          _-mohamed-_
                          wrote on last edited by
                          #28

                          I tried to open the same file with all text editors on my computer like (notepad++, VScode, Pycharm, Qt creator...) And they all opened the file successfully, is the problem in Qt?

                          1 Reply Last reply
                          0
                          • _ Offline
                            _ Offline
                            _-mohamed-_
                            wrote on last edited by
                            #29

                            I got a new problem when i override dropEvent function in QTextEdit
                            I used event->mimedata()->urls->at (0) to get the url of the droped file

                            But i got the same problem which is in arabic letters files name i got questions marks instade of the real file name(?????)

                            JonBJ 1 Reply Last reply
                            0
                            • _ _-mohamed-_

                              I got a new problem when i override dropEvent function in QTextEdit
                              I used event->mimedata()->urls->at (0) to get the url of the droped file

                              But i got the same problem which is in arabic letters files name i got questions marks instade of the real file name(?????)

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #30

                              @_-mohamed-_
                              How do you know that? @Christian-Ehrlicher wrote earlier

                              You must not print non-ascii characters on a windows console - windows is too stupid for this. You QMessageBox or similar.

                              Did you act on that? It is not clear to me how you are outputting this string, have you looked at it on a QMessageBox or a QLabel, or have you used the debugger to check what the actual bytes are in the string?

                              _ 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @_-mohamed-_
                                How do you know that? @Christian-Ehrlicher wrote earlier

                                You must not print non-ascii characters on a windows console - windows is too stupid for this. You QMessageBox or similar.

                                Did you act on that? It is not clear to me how you are outputting this string, have you looked at it on a QMessageBox or a QLabel, or have you used the debugger to check what the actual bytes are in the string?

                                _ Offline
                                _ Offline
                                _-mohamed-_
                                wrote on last edited by _-mohamed-_
                                #31

                                @JonB
                                Oh it's my fault this time, I used qDebug() to see the output, Sorry
                                now I used QMessageBox and it works(i mean dropEvent() function)

                                Thanks for your alert!

                                1 Reply Last reply
                                0
                                • _ Offline
                                  _ Offline
                                  _-mohamed-_
                                  wrote on last edited by
                                  #32

                                  I found a solution to this problem
                                  the URL is coming from the system, so the problem with the system

                                  I fixed that problem by enabling UTF-8 for windows by following these steps:

                                  • Open Control Panel
                                  • Click on Clock and Region
                                  • Click on Region
                                  • Go to Administrative tab
                                  • Click on Change system locale...
                                  • Click on Beta: Use Unicode UTF-8 for worldwide language support check box.
                                  • Then when you restart your computer the problem will disappear :)

                                  if you have any other solution please tell me

                                  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