Qt5.4 Open the file directory and select the file

Source: Internet
Author: User

1. If you do not need to open the specified file, you can use the following code

Qdesktopservices::openurl (Qurl::fromlocalfile (File_path));

2. If you need to specify a file

Can call the Windows API ShellExecute () to complete

Specific as follows:

2.1 <mainwindow.h>

#include <windows.h>
#include <shellapi.h>

qtextcodec *codec;
 

2.2 <mainwindow.cpp>

codec = Qtextcodec::codecforname ("GB18030"); Windows uses GBK encoding by default, QT uses UTF-8 encoding by default and requires conversion

2.3 Call Explorer to open the file browser and make the file, format "Explorer/select, E:\Table_1.txt" must be a backslash, otherwise fail, I've been here a long time

and the QT default Qfileinfo::filepath () is a backslash, ie '/', so you have to do the code as follows


QString argsstr = "/select," +fileinfo.filepath (). Replace ("/", "\ \"); Replace file directory delimited form
Shellexecutea (NULL, "Open", "Explorer", Codec->fromunicode (ARGSSTR). Constdata (), Null,sw_ Showdefault);//convert UTF-8 to GBK and convert to const char* type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.