Linux 创建启动器(.Desktop文件) 及 例子。QSettings目前规则限制无法正确读取。

目录

x.desktop文件的表现:

问题集

要点:

1. 直接拷贝法

/usr/share/applications/ # 在此目录下,体现在左下角的开始菜单中。

/home/userName/Desktop #在此目录下,体现在具体用户userName(如:用户 Abc)的桌面上。

直接到 /usr/share/applications/ 目录下,拷贝对应的 .desktop 文件,放在自己的桌面目录下 /home/userName/Desktop。

2. 自建 .desktop 文件

并不是所有的可执行文件,都有对应的 .desktop 文件。因此,需要自建。

3. 典型的 .desktop 文件,由 linuxdeployqt 工具默认生成的:

[Desktop Entry]
Type=Application
Name=My Test                    #应用的名字(快捷方式显示的文字)
Exec=/opt/Test/Test             #应用的执行路径(绝对路径)
Icon=/opt/Test/Test.png         #应用的图标(绝对路径)
Comment=This is my test         #说明信息
Terminal=true                   #是否允许在终端启动

参考:

使用linuxdeployqt在linux下进行Qt打包发布 (超详细) (*****)  https://blog.csdn.net/ken2232/article/details/131305081

=================================

问题集:

1. 启动 desktop

1.1. 启动。但每次都会弹出这个警告页面

1.2. 点击“Mark Executable”后,点击可直接启动。

2. Qt 的 QSettings不能被是用来读写 desktop文件。

因为 Qt的规则设定问题造成的,用户无法更改。除非 Qt未来修改了这条规则。

Qt 来源于 C++,所以除了采用 Qt自带的其他 QFile读写方法之外,还可以采用 C++的方法。

QSettings->setValue , group contains space will save to %20 ?

[Desktop%20Entry]
Name=KuWo

https://forum.qt.io/topic/89987/qsettings-setvalue-group-contains-space-will-save-to-20/2

The INI file format has severe restrictions on the syntax of a key. Qt works around this by using % as an escape character in keys.

https://doc.qt.io/qt-5/qsettings.html#Format-enum

The INI file format has severe restrictions (严格的限制)  on the syntax of a key. Qt works around this by using % as an escape character (转义字符;换码字符)  in keys.

In addition, if you save a top-level setting (a key with no slashes in it, e.g., "someKey"), it will appear in the INI file's "General" section. To avoid overwriting other keys, if you save something using a key such as "General/someKey", the key will be located in the "%General" section, not in the "General" section.

Following the philosophy that we should be liberal in what we accept and conservative in what we generate, QSettings will accept Latin-1 encoded INI files, but generate pure ASCII files, where non-ASCII values are encoded using standard INI escape sequences. To make the INI files more readable (but potentially less compatible), call setIniCodec().

=================================

x.desktop文件的表现:

/usr/share/applications/ # 在此目录下,体现在左下角的开始菜单中。

/home/userName/Desktop #在此目录下,体现在具体用户userName(如:用户 Abc)的桌面上。

[Desktop Entry]
# 编码
Encoding=UTF-8
# 版本(非必须)
Version=0.8.9
# 类型
Type=Application

        
# 名称,在桌面上显示的名字。 快捷方式的名字
Name=Android Studio
# 注释
Comment=Android Studio IDE
# 通用名(描述)
GenericName=Android Studio
# 执行命令,可执行程序的路径
Exec=/opt/android-studio/bin/studio.sh %f   #启动程序。%f会出现在默认打开方式列表
# 图标位置,支持常见图片格式
Icon=/opt/android-studio/bin/studio.png

         
# 这个很关键,当前表明这个快捷方式不是从终端打开。是否允许终端打开,一般为false
Terminal=false

         
# 当 Type 为应用时,表示工作空间路径
Path=
# 分类
Categories=Development;
# 此参数请参考其他文章,我暂时没能理解:)
StartupNotify=true

https://www.cnblogs.com/mc-r/p/16115288.html

Don't declare StartupNotify=true in panel-specific desktop files

As per the Startup Notification Spec, when an application declares this in its desktop file, it the launcher (e.g. the shell) will initiate the startup process, expecting the launchee to finish it with a "remove" X message once it's ready, unless there's a failure launching it.

The problem here is that this panel-specific desktop files declaring that key confuses the shell, since it will make it initiate the startup process but then no "remove" message is ever received if g-c-c was already running, since the newly launched process will realize it's not the main instance and will exit gracefully after handing over control of the situation to the original process, with exit code 0, leaving the startup process unfinished.

Due to this, the shell will keep waiting for a supposedly new application (as per the the panel-specific desktop file used to launch) to finish, which will result in the cursor to keep spinning after the app is discarded by the shell on a timeout.

We should fix this in a more general way (see [2]), since apps exiting due to handing control over to a previous instance should still send the "remove" message, but in the meantime let's make our user's lives better by not declaring this key in any of the panel-specific desktop files.

[1] startup-notification-spec [2] Bug 795779 – Ensure that the the startup sequence is finished when the launchee exits early without an error code

https://gitlab.gnome.org/GNOME/gnome-control-center/-/merge_requests/35

 Linux 创建启动器(.Desktop文件)

有些软件提供的启动方式为 .sh 文件,每次启动都需要在终端运行命令。不是很方便,通过查找资料了解到可以创建一个启动器来解决问题。

在linux中,一切皆文件。 启动器就是一个后缀为 .desktop 文件。
我创建的 intelliJ idea 的启动器,文件内容如下

[Desktop Entry]
Encoding=UTF-8
Name=IntelliJ IDEA
GenericName=IntelliJ IDEA
Comment=The Java IDE for Professional Developers by JetBrains
Exec=/opt/SoftWare/idea-IU-172.4343.14/bin/idea.sh %f
Icon=/opt/SoftWare/idea-IU-172.4343.14/bin/idea.png
Terminal=false
Type=Application
Categories=Application;Programme;

语法解释:

关键词                      意义
[Desktop Entry]        文件头
Encoding                  编码
Name                       应用名称
Name[xx]                 不同语言的应用名称
GenericName          描述
Comment                 注释
Exec                        执行的命令
Icon                         图标路径
Terminal                  是否使用终端
Type                        启动器类型
Categories              应用的类型(内容相关)

使用流程:

创建文件,以.desktop为后缀。
编写内容,修改权限
双击启动程序,添加信任
移动到/usr/share/applications/目录下,这样就可以在左上角应用程序中显示

说明:

其中 Exec 常用的参数有:%f %F %u %U

%f:单个文件名,即使选择了多个文件。如果已选择的文件不在本地文件系统中(比如说在HTTP或者FTP上),这个文件将被作为一个临时文件复制到本地,%f将指向本地临时文件;

%F:文件列表。用于程序可以同时打开多个本地文件。每个文件以分割段的方式传递给执行程序。

%u:单个URL。本地文件以文件URL或文件路径的方式传递。

%U:URL列表。每个URL以分割段的方式传递给执行程序。本地文件以文件URL或文件路径的方式传递。
————————————————
版权声明:本文为CSDN博主「为何不能下定决心?」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Future_promise/article/details/78215201

Table 2. Standard Keys

KeyDescriptionValue TypeREQ?Type
TypeThis specification defines 3 types of desktop entries: Application (type 1), Link (type 2) and Directory (type 3). To allow the addition of new types in the future, implementations should ignore desktop entries with an unknown type.stringYES
VersionVersion of the Desktop Entry Specification that the desktop entry conforms with. Entries that confirm with this version of the specification should use 1.5. Note that the version field is not required to be present.stringNO1-3
NameSpecific name of the application, for example "Mozilla".localestringYES1-3
GenericNameGeneric name of the application, for example "Web Browser".localestringNO1-3
NoDisplayNoDisplay means "this application exists, but don't display it in the menus". This can be useful to e.g. associate this application with MIME types, so that it gets launched from a file manager (or other apps), without having a menu entry for it (there are tons of good reasons for this, including e.g. the netscape -remote, or kfmclient openURL kind of stuff).booleanNO1-3
CommentTooltip for the entry, for example "View sites on the Internet". The value should not be redundant with the values of Name and GenericName.localestringNO1-3
IconIcon to display in file manager, menus, etc. If the name is an absolute path, the given file will be used. If the name is not an absolute path, the algorithm described in the Icon Theme Specification will be used to locate the icon.iconstringNO1-3
HiddenHidden should have been called Deleted. It means the user deleted (at their level) something that was present (at an upper level, e.g. in the system dirs). It's strictly equivalent to the .desktop file not existing at all, as far as that user is concerned. This can also be used to "uninstall" existing files (e.g. due to a renaming) - by letting make install install a file with Hidden=true in it.booleanNO1-3
OnlyShowIn, NotShowIn

A list of strings identifying the desktop environments that should display/not display a given desktop entry.

By default, a desktop file should be shown, unless an OnlyShowIn key is present, in which case, the default is for the file not to be shown.

If $XDG_CURRENT_DESKTOP is set then it contains a colon-separated list of strings. In order, each string is considered. If a matching entry is found in OnlyShowIn then the desktop file is shown. If an entry is found in NotShowIn then the desktop file is not shown. If none of the strings match then the default action is taken (as above).

$XDG_CURRENT_DESKTOP should have been set by the login manager, according to the value of the DesktopNames found in the session file. The entry in the session file has multiple values separated in the usual way: with a semicolon.

The same desktop name may not appear in both OnlyShowIn and NotShowIn of a group.

string(s)NO1-3
DBusActivatableA boolean value specifying if D-Bus activation is supported for this application. If this key is missing, the default value is false. If the value is true then implementations should ignore the Exec key and send a D-Bus message to launch the application. See D-Bus Activation for more information on how this works. Applications should still include Exec= lines in their desktop files for compatibility with implementations that do not understand the DBusActivatable key.booleanNO
TryExecPath to an executable file on disk used to determine if the program is actually installed. If the path is not an absolute path, the file is looked up in the $PATH environment variable. If the file is not present or if it is not executable, the entry may be ignored (not be used in menus, for example).stringNO1
ExecProgram to execute, possibly with arguments. See the Exec key for details on how this key works. The Exec key is required if DBusActivatable is not set to true. Even if DBusActivatable is true, Exec should be specified for compatibility with implementations that do not understand DBusActivatable.stringNO1
PathIf entry is of type Application, the working directory to run the program in.stringNO1
TerminalWhether the program runs in a terminal window.booleanNO1
ActionsIdentifiers for application actions. This can be used to tell the application to make a specific action, different from the default behavior. The Application actions section describes how actions work.string(s)NO1
MimeTypeThe MIME type(s) supported by this application.string(s)NO1
CategoriesCategories in which the entry should be shown in a menu (for possible values see the Desktop Menu Specification).string(s)NO1
ImplementsA list of interfaces that this application implements. By default, a desktop file implements no interfaces. See Interfaces for more information on how this works.string(s)NO
KeywordsA list of strings which may be used in addition to other metadata to describe this entry. This can be useful e.g. to facilitate searching through entries. The values are not meant for display, and should not be redundant with the values of Name or GenericName.localestring(s)NO1
StartupNotifyIf true, it is KNOWN that the application will send a "remove" message when started with the DESKTOP_STARTUP_ID environment variable set. If false, it is KNOWN that the application does not work with startup notification at all (does not shown any window, breaks even when using StartupWMClass, etc.). If absent, a reasonable handling is up to implementations (assuming false, using StartupWMClass, etc.). (See the Startup Notification Protocol Specification for more details).booleanNO1
StartupWMClassIf specified, it is known that the application will map at least one window with the given string as its WM class or WM name hint (see the Startup Notification Protocol Specification for more details).stringNO1
URLIf entry is Link type, the URL to access.stringYES2
PrefersNonDefaultGPUIf true, the application prefers to be run on a more powerful discrete GPU if available, which we describe as “a GPU other than the default one” in this spec to avoid the need to define what a discrete GPU is and in which cases it might be considered more powerful than the default GPU. This key is only a hint and support might not be present depending on the implementation.booleanNO1
SingleMainWindowIf true, the application has a single main window, and does not support having an additional one opened. This key is used to signal to the implementation to avoid offering a UI to launch another window of the app. This key is only a hint and support might not be present depending on the implementation.booleanNO1

例子

在此目录下的例子:

/usr/share/applications/

# python3.9.desktop

[Desktop Entry]
Name=Python (v3.9)
Comment=Python Interpreter (v3.9)
Exec=/usr/bin/python3.9
Icon=/usr/share/pixmaps/python3.9.xpm
Terminal=true
Type=Application
Categories=Development;
StartupNotify=true
NoDisplay=true

# git-cola.desktop

        

[Desktop Entry]
Name=Git Cola
Comment=The highly caffeinated Git GUI
Comment[cs]=Git GUI s vysokým obsahem kofeinu
Comment[zh_TW]=高咖啡因含量的 Git 圖形介面
TryExec=git-cola
Exec=git-cola --prompt
Icon=git-cola
StartupNotify=true
Terminal=false
Type=Application
Categories=Development;RevisionControl;
X-KDE-SubstituteUID=false

# yad-icon-browser.desktop

         

[Desktop Entry]
Encoding=UTF-8
Name=Icon Browser
Name[fr]=Navigateur d'Icônes
Name[it]=Navigatore di icone
Name[pt_BR]=Ícone do navegador
Name[ru]=Броузер иконок
Name[sk]=Prehliadač ikon
Name[uk]=Павігатор іконок
Name[zh_TW]=圖示瀏覽器
Comment=Inspect GTK Icon Theme
Comment[fr]=Parcourir les icônes du thème GTK
Comment[it]=Sfoglia le icone del tema GTK
Comment[pt_BR]=Inspecionar ícone tema do GTK
Comment[ru]=Исследование темы иконок GTK
Comment[sk]=Preskúmať tému ikon GTK
Comment[uk]=Перегляд теми іконок GTK
Comment[zh_TW]=檢閱 GTK 圖示布景主題
Categories=GTK;Development;
Exec=yad-icon-browser
Icon=yad
Terminal=false
Type=Application
StartupNotify=true

# featherpad.desktop

         

[Desktop Entry]
Name=FeatherPad
GenericName=Text Editor
GenericName[cs]=Textový editor
GenericName[da]=Tekstredigering
GenericName[de]=Texteditor
GenericName[es]=Editor de texto
GenericName[eo]=Tekst-Redaktilo
GenericName[fa]=ويرايشگر متن
GenericName[hu]=Szövegszerkesztő
GenericName[it]=Editor di testo
GenericName[ja_JP]=テキストエディタ(Qt)
GenericName[pl]=Edytor tekstu
GenericName[pt]=Editor de texto
GenericName[ru]=Текстовый редактор
GenericName[sv]=Textredigering
GenericName[tr]=Metin Düzenleyici
GenericName[zh_CN]=文本编辑器
Comment=Lightweight Qt text editor
Comment[cs]=Nenáročný textový editor, založený na Qt
Comment[da]=Letvægts Qt-tekstredigering
Comment[es]=Editor de texto ligero basado en Qt
Comment[eo]=Malpeza tekst-redaktilo bazita sur Qt
Comment[fa]=ويرايشگر سبک متن
Comment[hu]=Gyors szövegszerkesztő Qt-ben
Comment[ja_JP]=軽量 Qt テキストエディタ
Comment[pl]=Lekki edytor tekstu oparty na Qt
Comment[pt]=Editor de texto leve baseado em Qt
Comment[ru]=Лёгкий текстовый редактор на Qt
Comment[sv]=Lättviktig Qt-textredigering
Comment[tr]=Hafif Qt tabanlı metin düzenleyici
Comment[zh_CN]=轻量级 Qt 文本编辑器
Exec=featherpad %U
Icon=featherpad
Terminal=false
Type=Application
MimeType=text/plain;
Categories=Qt;Utility;TextEditor;
X-KDE-StartupNotify=false;
Keywords=Text;Editor;Plaintext;
Actions=new-window;standalone-window;

         

[Desktop Action new-window]
Name=New Window
Name[cs]=Nové okno
Name[de]=Neues Fenster
Name[eo]=Nova Fenestro
Name[fa]=پنجره‌ی جديد
Name[it]=Nuova finestra
Name[hu]=Új ablak
Name[pt]=Nova Janela
Name[sv]=Nytt Fönster
Exec=featherpad --win

         

[Desktop Action standalone-window]
Name=Standalone Window
Name[de]=Eigenständiges Fenster
Name[eo]=Memstara Fenestro
Name[fa]=پنجره‌ی مستقل
Name[sv]=Fristående Fönster
Exec=featherpad --standalone

# firefox.desktop

        

[Desktop Entry]
Categories=Network;WebBrowser;
Comment[en_US]=Browse the World Wide Web
Comment=Browse the World Wide Web
Comment[bg]=Сърфиране в Мрежата
Comment[ca]=Navegueu per el web
Comment[cs]=Prohlížení stránek World Wide Webu
Comment[de]=Im Internet surfen
Comment[el]=Περιηγηθείτε στον παγκόσμιο ιστό
Comment[es]=Navegue por la web
Comment[fa]=صفحات شبکه جهانی اینترنت را مرور نمایید
Comment[fi]=Selaa Internetin WWW-sivuja
Comment[fr]=Navigue sur Internet
Comment[hu]=A világháló böngészése
Comment[it]=Esplora il web
Comment[ja]=ウェブを閲覧します
Comment[ko]=웹을 돌아 다닙니다
Comment[nb]=Surf på nettet
Comment[nl]=Verken het internet
Comment[nn]=Surf på nettet
Comment[no]=Surf på nettet
Comment[pl]=Przeglądanie stron WWW
Comment[pt]=Navegue na Internet
Comment[pt_BR]=Navegue na Internet
Comment[ru]=Обозреватель Всемирной Паутины
Comment[sk]=Prehliadanie internetu
Comment[sv]=Surfa på webben
Encoding=UTF-8
Exec=firefox %u
GenericName[en_US]=Web Browser
GenericName=Web Browser
GenericName[bg]=Интернет браузър
GenericName[ca]=Navegador web
GenericName[cs]=Webový prohlížeč
GenericName[de]=Webbrowser
GenericName[el]=Περιηγητής ιστού
GenericName[es]=Navegador web
GenericName[fa]=مرورگر اینترنتی
GenericName[fi]=WWW-selain
GenericName[fr]=Navigateur Web
GenericName[hu]=Webböngésző
GenericName[it]=Browser Web
GenericName[ja]=ウェブ・ブラウザ
GenericName[ko]=웹 브라우저
GenericName[nb]=Nettleser
GenericName[nl]=Webbrowser
GenericName[nn]=Nettlesar
GenericName[no]=Nettleser
GenericName[pl]=Przeglądarka WWW
GenericName[pt]=Navegador Web
GenericName[pt_BR]=Navegador Web
GenericName[ru]=Интернет-браузер
GenericName[sk]=Internetový prehliadač
GenericName[sv]=Webbläsare
Icon=firefox
MimeType=text/html;image/png;image/jpeg;image/gif;application/xml;application/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;application/rss+xml;application/rdf+xml;
Name[en_US]=Firefox
Name=Firefox
Name[bg]=Firefox
Name[ca]=Firefox
Name[cs]=Firefox
Name[el]=Firefox
Name[es]=Firefox
Name[fa]=Firefox
Name[fi]=Firefox
Name[fr]=Firefox
Name[hu]=Firefox
Name[it]=Firefox
Name[ja]=Firefox
Name[ko]=Firefox
Name[nb]=Firefox
Name[nl]=Firefox
Name[nn]=Firefox
Name[no]=Firefox
Name[pl]=Firefox
Name[pt]=Firefox
Name[pt_BR]=Firefox
Name[ru]=Firefox
Name[sk]=Firefox
Name[sv]=Firefox
StartupNotify=true
StartupWMClass=Firefox
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-GNOME-FullName=Firefox Web Browser
X-GNOME-FullName[bg]=Интернет браузър (Firefox)
X-GNOME-FullName[ca]=Navegador web Firefox
X-GNOME-FullName[cs]=Firefox Webový prohlížeč
X-GNOME-FullName[el]=Περιηγήτης Ιστού Firefox
X-GNOME-FullName[es]=Navegador web Firefox
X-GNOME-FullName[fa]=مرورگر اینترنتی Firefox
X-GNOME-FullName[fi]=Firefox-selain
X-GNOME-FullName[fr]=Navigateur Web Firefox
X-GNOME-FullName[hu]=Firefox webböngésző
X-GNOME-FullName[it]=Firefox Browser Web
X-GNOME-FullName[ja]=Firefox ウェブ・ブラウザ
X-GNOME-FullName[ko]=Firefox 웹 브라우저
X-GNOME-FullName[nb]=Firefox Nettleser
X-GNOME-FullName[nl]=Firefox webbrowser
X-GNOME-FullName[nn]=Firefox Nettlesar
X-GNOME-FullName[no]=Firefox Nettleser
X-GNOME-FullName[pl]=Przeglądarka WWW Firefox
X-GNOME-FullName[pt]=Firefox Navegador Web
X-GNOME-FullName[pt_BR]=Navegador Web Firefox
X-GNOME-FullName[ru]=Интернет-браузер Firefox
X-GNOME-FullName[sk]=Internetový prehliadač Firefox
X-GNOME-FullName[sv]=Webbläsaren Firefox
X-KDE-SubstituteUID=false
X-KDE-Username=
X-MultipleArgs=false

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值