Associate File Type with Qt In Mac Os and Win

Win Registry

Question

One day, my boss want me to finish one function which let the users can double click project file(*.opr) to startup application easy and quickly. oh, This function is very common, Just like double clicked the Microsoft Word Document(*doc,*docx), then, we can startup Micrsoft Word Application to edit frich text. Ok, let's do it.

  1. Win And Mac OS use the different way to associated file type.
  2. Now the App only process one project at same time. if use the double click to open opr file. how to do when opend one project now?
  3. Current App mode is single-one mode.

How

When we get the new task about program, First to think about it. What's i will finish? Did i do it before? How i should to do ? If you think nothing, There is better way to get the answer from internet for you.

I think nothing. But, I know it is associate windows registry in Windows OS and List.Info file In Mac OS. So, I want to know how to finished it use the C++ or Qt. I also searched some documents about how to read/write registry with Qt. I was feel better that time. But when i tried all ways what them said, they are both failed. I spend half of the day, but not working. Their blogs not have the good introduction.

Now, I get the right answer from blogs of cnblogs.com. cnblogs.com is bester web site bout the programe than others.

Done

I used Qt frame to finished it. if you know QSettings classes, it will be very easy, if not, please to see thw Qt help manual about the QSettings class at first.

/** some include file */
#ifdef Q_OS_WIN
#include <QSettings>
#endif

#ifdef Q_OS_WIN
void checkWinRegistry()
{
    QString val;
    QString exePath = qApp->applicationFilePath();
    exePath.replace("/", "\\");

    QSettings regType("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.abc",
                                   QSettings::NativeFormat);
    QSettings regIcon("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.abc\\DefaultIcon",
                                   QSettings::NativeFormat);
    QSettings regShell("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.abc\\Shell\\Open\\Command",
                                   QSettings::NativeFormat);

    /** . means default value, you can also use the "Default" string */
    if("" != regType.value(".").toString()){
        regType.setValue(".","");
    }

    /** 0 使用当前程序内置图标 */
    val = exePath + ",0";
    if(val != regIcon.value(".").toString()){
        regIcon.setValue(".",val);
    }

    val = exePath + " \"%1\"";
    if(val != regShell.value(".").toString()){
        regShell.setValue(".",val);
    }
}
#endif

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    checkWinRegistry();

    /** if you want to get the file path, please use argv[1], if argc >= 2 */
    QString text;
    text = QString::number(argc);
    text += "\n";
    for(int i=0; i<argc; i++){
        text += QString(argv[i]);///< This way to get the QString may be is unreadable code. eg, include chinese language, we should use `QStringList ltArguments = a.arguments();`
        text += "\n";
    }

    Widget w(text);
    w.show();

    return a.exec();
}

Compared

if you don't clear about my way. Now, you could to see the following images about How to associate file type in QtCreater. we know that double *.pro file, we can startup QtCreater application to programing. How ?

  1. In Windows we can use Ctrl+R and input regedit, Enter to open the windows registry editer.
  2. Let we find HKEY_CURRENT_USER\SOFTWARE\Classes\.pro, you can see what keys and values under this item;
    01.png
  3. Then let we find HKEY_CURRENT_USER\SOFTWARE\Classes\QtProject.QtCreator.pro, what keys and values under this item;
    02.png
  4. There are other file type of qt.
    03.png

Compared and to finished your work by try and try.

Associate Aile Aype An Aac As

personal Blogs
qt.io
stackoverflow
Qt Mac Release
stackoverflow
qt.io
Info.plist Mac
stackoverflow:overriding Mac app file associations via CFBundleDocumentTypes in info.plist

Reference

wikipedia Windows Registry
support windows registry
wikipedia Registry cleaner
知乎:为什么Win要有注册表,而Unix其他系统不需要
win注册表详解
c# cnblogs
vc cnblogs
Qt Win wiki.qt.io
Qt Mac stackoverflow
Qt Mac csdn
Mac And Win csdn


转载于:https://www.cnblogs.com/zi-xing/p/6921315.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值