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 4.1k 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.
  • _ Offline
    _ Offline
    _-mohamed-_
    wrote on last edited by
    #1

    Hi
    I made a notepad in Qt and I want to open the file I double clicked on my app
    I used app arguments (QCoreApplication::arguments()), the second argument is the file path
    It works perfectly when the file name is in English but when the file name is in Arabic, there are some problems
    When it is more than 6 Arabic characters, the argument looks like:
    C:/the/file/path/???????. txt
    And I can't open the file
    But when it is less than 6 characters it works fine
    (When I open the file via QFileDialog, it works fine with any file name even if it's an Arabic name of more than 6 characters)
    what is the problem and how can I fix it?
    and thanks

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

      Hi,

      Can you show the code you use to parse your argument ?

      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
      0
      • SGaistS SGaist

        Hi,

        Can you show the code you use to parse your argument ?

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

        @SGaist

            QStringList arguments = QCoreApplication::arguments();
            if(arguments.count() > 1)
            {
        
                QFile file(arguments.at(1));
        
                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 = arguments.at(1);
                    changeTitle();
                }
            }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you try again using QCommandLineParser ?

          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
          0
          • SGaistS SGaist

            Can you try again using QCommandLineParser ?

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

            @SGaist how can i use QCommandLineParser to get application's arguments

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

              @SGaist how can i use QCommandLineParser to get application's arguments

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

              @_-mohamed-_ See the official example: https://doc.qt.io/qt-5/qcommandlineparser.html#details

              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
              0
              • _ Offline
                _ Offline
                _-mohamed-_
                wrote on last edited by
                #7

                @eyllanesc
                @SGaist
                i cant get application's arguments using QCommandLineParser

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

                  @eyllanesc
                  @SGaist
                  i cant get application's arguments using QCommandLineParser

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

                  @_-mohamed-_ Basic rule of the forum: Without code we cannot help you. Show the code where you implement the logic with QCommandLineParser, so we can analyze where the error is and so we can probably give you a solution.

                  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
                  1
                  • eyllanescE eyllanesc

                    @_-mohamed-_ Basic rule of the forum: Without code we cannot help you. Show the code where you implement the logic with QCommandLineParser, so we can analyze where the error is and so we can probably give you a solution.

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

                    @eyllanesc I just don't know how to do that

                    I tried

                    QCommandLineParser command;
                    
                       QStringList arguments = command.positionalArguments();
                       if(arguments.count() > 1)
                       {
                    
                           QFile file(arguments.at(1));
                    
                           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 = arguments.at(1);
                               changeTitle();
                           }
                       }
                    
                    eyllanescE 1 Reply Last reply
                    0
                    • _ _-mohamed-_

                      @eyllanesc I just don't know how to do that

                      I tried

                      QCommandLineParser command;
                      
                         QStringList arguments = command.positionalArguments();
                         if(arguments.count() > 1)
                         {
                      
                             QFile file(arguments.at(1));
                      
                             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 = arguments.at(1);
                                 changeTitle();
                             }
                         }
                      
                      eyllanescE Offline
                      eyllanescE Offline
                      eyllanesc
                      wrote on last edited by
                      #10

                      @_-mohamed-_ Your sentence: i cant get application's arguments using QCommandLineParser is confusing. You should have said I don't know how to implement, why don't you use the official example? Try it and when it fails, they receive goods with the code that does not work.

                      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
                      0
                      • eyllanescE eyllanesc

                        @_-mohamed-_ Your sentence: i cant get application's arguments using QCommandLineParser is confusing. You should have said I don't know how to implement, why don't you use the official example? Try it and when it fails, they receive goods with the code that does not work.

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

                        @eyllanesc
                        when i use args like qDebug() << args.at(0); i got this The program has unexpectedly finished.

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

                          @eyllanesc
                          when i use args like qDebug() << args.at(0); i got this The program has unexpectedly finished.

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

                          @_-mohamed-_ what is args?, please provide a minimal and verifiable example.

                          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
                          0
                          • eyllanescE eyllanesc

                            @_-mohamed-_ what is args?, please provide a minimal and verifiable example.

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

                            @eyllanesc it is from the official example

                            const QStringList args = parser.positionalArguments();
                               // source is args.at(0), destination is args.at(1)
                            

                            you can find it here:

                            int main(int argc, char *argv[])
                            {
                                QCoreApplication app(argc, argv);
                                QCoreApplication::setApplicationName("my-copy-program");
                                QCoreApplication::setApplicationVersion("1.0");
                            
                                QCommandLineParser parser;
                                parser.setApplicationDescription("Test helper");
                                parser.addHelpOption();
                                parser.addVersionOption();
                                parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
                                parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
                            
                                // A boolean option with a single name (-p)
                                QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
                                parser.addOption(showProgressOption);
                            
                                // A boolean option with multiple names (-f, --force)
                                QCommandLineOption forceOption(QStringList() << "f" << "force",
                                        QCoreApplication::translate("main", "Overwrite existing files."));
                                parser.addOption(forceOption);
                            
                                // An option with a value
                                QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                                        QCoreApplication::translate("main", "Copy all source files into <directory>."),
                                        QCoreApplication::translate("main", "directory"));
                                parser.addOption(targetDirectoryOption);
                            
                                // Process the actual command line arguments given by the user
                                parser.process(app);
                            
                                const QStringList args = parser.positionalArguments();
                                // source is args.at(0), destination is args.at(1)
                            
                                bool showProgress = parser.isSet(showProgressOption);
                                bool force = parser.isSet(forceOption);
                                QString targetDir = parser.value(targetDirectoryOption);
                                // ...
                            }
                            
                            eyllanescE 1 Reply Last reply
                            0
                            • _ _-mohamed-_

                              @eyllanesc it is from the official example

                              const QStringList args = parser.positionalArguments();
                                 // source is args.at(0), destination is args.at(1)
                              

                              you can find it here:

                              int main(int argc, char *argv[])
                              {
                                  QCoreApplication app(argc, argv);
                                  QCoreApplication::setApplicationName("my-copy-program");
                                  QCoreApplication::setApplicationVersion("1.0");
                              
                                  QCommandLineParser parser;
                                  parser.setApplicationDescription("Test helper");
                                  parser.addHelpOption();
                                  parser.addVersionOption();
                                  parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
                                  parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
                              
                                  // A boolean option with a single name (-p)
                                  QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
                                  parser.addOption(showProgressOption);
                              
                                  // A boolean option with multiple names (-f, --force)
                                  QCommandLineOption forceOption(QStringList() << "f" << "force",
                                          QCoreApplication::translate("main", "Overwrite existing files."));
                                  parser.addOption(forceOption);
                              
                                  // An option with a value
                                  QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                                          QCoreApplication::translate("main", "Copy all source files into <directory>."),
                                          QCoreApplication::translate("main", "directory"));
                                  parser.addOption(targetDirectoryOption);
                              
                                  // Process the actual command line arguments given by the user
                                  parser.process(app);
                              
                                  const QStringList args = parser.positionalArguments();
                                  // source is args.at(0), destination is args.at(1)
                              
                                  bool showProgress = parser.isSet(showProgressOption);
                                  bool force = parser.isSet(forceOption);
                                  QString targetDir = parser.value(targetDirectoryOption);
                                  // ...
                              }
                              
                              eyllanescE Offline
                              eyllanescE Offline
                              eyllanesc
                              wrote on last edited by
                              #14

                              @_-mohamed-_ How have you run your executable? You must run: your_executable input_filename output_filename.

                              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
                              0
                              • eyllanescE eyllanesc

                                @_-mohamed-_ How have you run your executable? You must run: your_executable input_filename output_filename.

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

                                @eyllanesc
                                I run the app from qt creator

                                but the first argument should be the exe file path and it does not need second argument.
                                It should work when i call the first argument

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

                                  @eyllanesc
                                  I run the app from qt creator

                                  but the first argument should be the exe file path and it does not need second argument.
                                  It should work when i call the first argument

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

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

                                  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
                                  0
                                  • 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 Online
                                          JonBJ Online
                                          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

                                          • Login

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