Results 1 to 2 of 2

Thread: I have a problem with opening the double-clicked file in my Qt app

  1. #1
    Join Date
    Sep 2021
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default I have a problem with opening the double-clicked file in my Qt app

    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

    sometimes when the file name is in Arabic, the argument looks like this:

    C:/the/file/path/???????. txt

    And I can't open the file

    but sometimes it works with no problems for example when the file name is(????????) I got it like(????????), but when it is(??) or (?? ????) it works.

    (When I open the file via QFileDialog, it works fine with any file name)

    what is the problem and how can I fix it?

    and thanks

    main.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QApplication>
    4.  
    5. #include <QDebug>
    6.  
    7. using namespace std;
    8.  
    9. int main(int argc, char *argv[])
    10.  
    11. {
    12.  
    13. QApplication a(argc, argv);
    14.  
    15. MainWindow w;
    16.  
    17. if(qApp->arguments().count() > 1)
    18.  
    19. w.openFile(qApp->arguments().at(1));
    20.  
    21. w.show();
    22.  
    23. return a.exec();
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow::openFile(QString):

    Qt Code:
    1. void MainWindow::openFile(QString URL)
    2.  
    3. {
    4.  
    5. QFile file(URL);
    6.  
    7. if(!file.open(QFile::ReadOnly | QFile::Text))
    8.  
    9. QMessageBox::warning(this, "Error, Can't open the file", file.errorString(), QMessageBox::Ok);
    10.  
    11. else
    12.  
    13. {
    14.  
    15. QTextStream text(&file);
    16.  
    17. text.setCodec("UTF-8");
    18.  
    19. QString fileContents = text.readAll();
    20.  
    21. TextEdit->setPlainText(fileContents);
    22.  
    23. file.close();
    24.  
    25. edited = false;
    26.  
    27. url = URL;
    28.  
    29. changeTitle();
    30.  
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2021
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I have a problem with opening the double-clicked file in my Qt app

    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 :)

Similar Threads

  1. Replies: 0
    Last Post: 5th February 2019, 12:10
  2. Replies: 8
    Last Post: 10th July 2015, 13:12
  3. double clicked on an item QTreeWidget
    By milli in forum Newbie
    Replies: 3
    Last Post: 7th May 2011, 22:49
  4. Getting the Id of a double clicked item
    By thefatladysingsopera in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2011, 15:18
  5. Problem in opening a file
    By Abc in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 13:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.