QT open file path contains Chinese and space problems

Posted by mdj on Wed, 01 Jan 2020 17:03:37 +0100

The software made with QT MinGW version is sent to the customer and said that it does not work normally, the configuration file cannot be opened, or the loading data file is not normal. After remote viewing, it is found that the client often places the program in the path with spaces in Chinese, resulting in abnormal file opening. So I want to solve the problem in the program recently, so that the customer can have fewer problems.

First of all, I checked the path of QT open file for Chinese and space, copied the following code: (https://blog.csdn.net/m0_/article/details/76672634)

 1     QString fileName=QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image Files(*.png *.jpg *jpeg *.bmp *.avi *.mp4)"));
 2  
 3     QTextCodec *code = QTextCodec::codecForName("GB2312");//Solve the problem of Chinese path
 4     std::string name = code->fromUnicode(fileName).data();
 5  
 6     if(fileName.isEmpty())
 7     {
 8             return;
 9     }
10  
11     inputVideo.open(name);//I'm going to open a local video file

It's very simple, so it's added to your code:

 1 if(ui->bt_replay->text()=="playback")
 2 {
 3     auto name=QFileDialog::getOpenFileName(0,"","","pcap file(*.pcap)");
 4     QTextCodec *code = QTextCodec::codecForName("gb2312");//Solve the problem of Chinese path
 5     name = code->fromUnicode(name).data();
 6     cout<<"convert gb2312:"<<name.toStdString()<<endl;
 7     if(name!="")
 8     {
 9 
10         lctrl.replay_dat(name.toStdString());
11         ui->bt_replay->setText("Exit playback");
12         lctrl.replay_stat=1;
13         ui->lb_replay_stat->setText("Loading data");
14     }
15 }

It didn't work out. We think it's coding problem. If we change the coding and add the judgment of coding success, we can't do it anyway, but the codes found on the Internet are very simple.

Finally, it is found that the storage of QString is stored according to Unicode, and the input data will be converted to unicode when input. The default transcoding method is Latin-1, which causes the file name after transcoding to be transferred back again, and the conversion is not correct

Then use std::string to solve this problem:

 1 if(ui->bt_replay->text()=="playback")
 2 {
 3     auto name=QFileDialog::getOpenFileName(0,"","","pcap file(*.pcap)");
 4     QTextCodec *code = QTextCodec::codecForName("gb2312");//Solve the problem of Chinese path
 5     string namestd = code->fromUnicode(name).data();
 6     cout<<"convert gb2312:"<<namestd<<endl;
 7     if(namestd!="")
 8     {
 9         lctrl.replay_dat(namestd);
10         ui->bt_replay->setText("Exit playback");
11         lctrl.replay_stat=1;
12         ui->lb_replay_stat->setText("Loading data");
13     }
14 }

If there are spaces in the path, the solution for online query is: path.replace("", "\" \ "");

Finally, it was found that fopen itself supports the input of file names with spaces and does not need to be converted

Topics: C++ Qt