簡(jiǎn)述
在Windows系統(tǒng)中,雙擊某類擴(kuò)展名的文件,通過自己實(shí)現(xiàn)的程序打開文件,并正確加載及顯示文件。有兩種方式可以到達(dá)這個(gè)目的。
對(duì)于系統(tǒng)不知道的擴(kuò)展名的文件,第一次打開時(shí),需要自行設(shè)置打開方式。
設(shè)置流程:
右鍵或雙擊文件->打開方式,彈出以下界面
點(diǎn)擊“在這臺(tái)電腦上查找其他應(yīng)用”,選擇自己的可執(zhí)行程序即可。
方法一
方法一是通過參數(shù)個(gè)數(shù)及參數(shù)進(jìn)行設(shè)置雙擊啟動(dòng)。
// #include <QElapsedTimer>
int main(int argc, char *argv[])
{
QApplication a("xxxxx", argc, argv);
MainWindow w;
// 通過參數(shù)個(gè)數(shù)及參數(shù)進(jìn)行設(shè)置雙擊啟動(dòng)
{
if(argc > 1)
{
QString strOpenPath = QString(argv[1]);
if(!strOpenPath.isEmpty())
{
// 將“\”轉(zhuǎn)換成"/",因?yàn)?\"系統(tǒng)不認(rèn)
strOpenPath = strOpenPath.replace("\\", "/");
// 具體實(shí)現(xiàn)在下面這個(gè)函數(shù)
w.load(strOpenPath);
}
}
}
w.showMaximized();
return a.exec();
}
通過以上配置,再實(shí)現(xiàn)load()函數(shù),就可雙擊打開某一擴(kuò)展名的文件。
方法二
方法二是通過注冊(cè)表實(shí)現(xiàn)雙擊打開方式,具體實(shí)現(xiàn)如下,代碼可復(fù)制運(yùn)行。文章來源:http://www.zghlxwxcb.cn/news/detail-669308.html
#include "mainwindow.h"
#include <QApplication>
#include <QSettings>
void dectionRegedit(const QString& strClassName, const QString& strAppPath, const QString& strExt, const QString& strExtDescri)
{
QString strBaseUrl("HKEY_CURRENT_USER\\Software\\Classes");
QSettings setting(strBaseUrl, QSettings::NativeFormat);
setting.setValue("/" + strClassName + "/Shell/Open/Command/.", "\"" + strAppPath + "\" \"%1\"");
setting.setValue("/" + strClassName + "/.", strExtDescri);
setting.setValue("/" + strClassName + "/DefaultIcon/.", strAppPath + ",0");
// 關(guān)聯(lián)ext 和 類別
setting.setValue("/" + strExt + "/OpenWithProgIds/" + strClassName, "");
// 立即保存該修改
setting.sync();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
if(argc > 1){
// 具體實(shí)現(xiàn)
w.load(argv[1]);
}
w.show();
// 通過注冊(cè)表實(shí)現(xiàn)
QString strAppPath = QApplication::applicationDirPath().append("/debug/doubleClickedFileTest0822.exe");
strAppPath.split("/").join("\\");
QString strClassName("MainWindow");
QString strExt(".txt");
QString strExtDescri("fileTest 工程文件");
dectionRegedit(strClassName, strAppPath, strExt, strExtDescri);
return a.exec();
}
注意
一定要注意路徑。文章來源地址http://www.zghlxwxcb.cn/news/detail-669308.html
到了這里,關(guān)于Qt雙擊某一文件通過自己實(shí)現(xiàn)的程序打開,并加載文件顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!