QT full Platform settings icon, full platform static compilation good

Source: Internet
Author: User

1. Overview

When we use QT to write a software, to share your program, it is impossible to copy the compiled directory to others to run. The compiled program should be a main program, add some resource files, add some dynamic link library, some of the tall can also do a setup file.

QT development programs are often used in two ways when they are released:

L compile statically to generate a single executable file.

L compile dynamically and attach the required DLL files.

2. Release preparation

Either way, first we have to prepare the release of the project, for example, to prepare the program's icon, compile the project into release, etc.

2.1. Set the program icon

There are two types of icons for applications:

The main window icon, which is displayed primarily in the upper-left corner of the main window when the program is running, or on the taskbar.

L Run the program icon, usually in the desktop or folder to display the thumbnail icon, you can click on the program to run.

2.1.1. Setting the main window icon

You first design the icon in a drawing tool (such as Photoshop), and the resolution of the icon is better than 32*32. The icon needs to be *ico or *png files. If you want to be lazy, go directly here (http://www.iconfinder.com/) download, a lot of good-looking free icons.

Place the icon file you just generated in the Yourprojectfolder/resources/images directory, or anywhere a program can find it. It is a good idea to add the icon file to QRC for unified management, the QRC file is in the following format:

<RCC>

<qresourceprefix= "/background" >

<filealias= "Mainlogo" >christmas_stocking.png</file>

</qresource>

</RCC>

And then use the main window, code as follows, the code is very simple, but remember that this code must be placed in the MainWindow constructor, or not work.

/*setwindowicon*/

Setwindowicon (Qicon (Qstringliteral (":/background/mainlogo"));

The effect of the program after running is as follows:

2.1.2. Setting icons for running programs

Setting the icon for running the program is different on each platform, as described below. First, prepare the icon file with the best resolution greater than 32*32. The icon requires a *ico file.

1) Windows platform

A) using visual Studio IDE development tools

If you are using the VisualStudio IDE development tool, for example, in VS2015, right-click on the project, add, select Icon, and then import the *ico icon file that was previously prepared, and determine that a projectname.rc file will appear in the project.

B) IDE Development tools using Qtcreator

If you are using Qmake to generate makefile files or use the QT Creator IDE, follow these three steps:

L First step: Create an ICO file containing the icon image and save it in the resource file directory, for example named: Myapp.ico;

L Second Step: Create an. rc file that contains the following:

Idi_icon1 ICON discardable "Myapp.ico"

L Third Step: write the following in the project file Myapp.pro file:

Rc_file = myapp.rc

Compile the program again to achieve the effect, as follows:

2) Mac OS x platform

Although many programs can create icon files (. icns), the recommended method is to use the program Iconutil provided by Apple. Iconutil is a command-line tool that allows you to import multiple icons of different sizes (used in different contexts) and to compress files. Save all the series of icons in a file in your project directory.

If you are using Qmake to generate the makefile file, you only need to add a single row to your. Pro Project file. For example, if the name of your icon file is Myapp.icns, and your project file name is Myapp.pro, join this line to Myapp.pro:

ICON = Myapp.icns

This will ensure that Qmake puts your icon in place and creates an Info.plist entry for the icon.

If you do not use Qmake, you must manually perform the following points:

1) Create a info.plist file for your application (which can be found in the developer/applications using Propertylisteditor).

2) Associate your. Icns records and Cfbundleiconfile Records in the Info.plist file (again, using Propertylisteditor).

3) Copy the Info.plist file to the contents directory of your application package.

4) Copy the. icns file to the Contents/resoures directory of your application package.

3) Common Linux Platform

In this section, we briefly describe some of the issues related to providing icons for applications in two common Linux desktop environments: KDE and GNOME. The core techniques used to describe application icons are the same for both desktops, and can be applied to others, but each has its own specifics. The primary source of standard information for using these Linux desktop Systems is freedesktop.org. For information about other Linux desktops, see the desktop documentation that you are interested in.

Typically, the user does not use the executable file directly, but instead launches the application by tapping the icon on the desktop. These icons are "desktop items" files that contain a description of the application with information about its icons. Both desktop environments can retrieve information from these files and use them to generate shortcuts to the application to the desktop, either in the Start menu or on the panel.

For more information about desktop item files, you can find them in the desktops entryspecification.

Although desktop item files can effectively encapsulate the details of your application, we still need to store the icons in a regular location in each desktop environment. Some of the locations used for icons are given in the icon themespecification.

Although the path used to locate the icon relies on the use of the desktop and its configuration, all of the following directory structures should follow the same pattern: subdirectories are organized by topic, icon size, and application type. Typically, the application icon is added to the high-color theme, so the size of the square application icon is 32 pixels, which will be stored under the icon path of the Hicolor/32x32/apps directory.

1. K Desktop Environment (KDE)

The application icon can be used by all users or by a single user installation. Users who are currently logged on to their KDE desktop can find these locations by using kde-config, for example, by typing in the terminal window to perform the following actions:

Kde-config--pathicon

Typically, a colon-delimited list of paths that are output to stdout contains user-specific icon paths and system-wide paths. In these directories, you can locate and install icons based on the conventions described in icon themespecification.

If you are developing specifically for KDE, you may want to leverage the benefits of the KDE build system to configure your application. This will ensure that your icon is installed in the appropriate location in KDE.

The KDE developer site is http://developer.kde.org/.

2. GNOME

Application icons are stored in a standard full-system directory that contains architecture-independent files. This location can be determined by using gnome-config, for example, by typing the following command in a terminal window:

Gnome-config--datadir

The output path in the standard output stdout refers to a location that contains a directory named Pixmaps, which is described in the directory structure in the Pixmaps directory in the icon themespecification.

If you are developing specifically for GNOME, you may want to use a standard set of GNU Build Tools, as explained in the relevant sections of gtk+/gnomeapplication development book. This will ensure that your icon is installed in the appropriate location for GNOME.

2.2. Compiling release version

Note that the Run Program compilation method is set to release because debugging information is included in the debug version of the program and can be used for debugging. When you really want to publish a program, use the release version, which reduces the volume of the publisher and increases the security of the software.

To demonstrate how to set up in Qt Creator, select the project on the left side sidebar of the IDE, and then set the build configuration to release in the build settings.

Demonstrate how you set up in VS2015, select the project in Solution manager on the right side of the IDE, and then set the build configuration to release in the toolbar on the top of the IDE.

3. Publisher Program

After installing the QT SDK, the default is to compile the dynamic link library, if you need to publish the program, you need to add the necessary dynamic link library in the executable file, but some of the dynamic link library file is very large, this is not the result we want.

The best way is to submit a statically linked program. However, the installed Qt is dynamically compiled, to generate a static version, you need to re-compile.

3.1. Static compilation

L Advantages, simple release, single file.

L Disadvantage, the library file is very large, the update program version is inconvenient. Every time you upgrade, you redistribute all of your content.

Static release Although it does not require more DLLs, the release is simple, convenient, but often involve licensing issues (for details, see QT LGPL authorization), dynamic release can be avoided ... If the DLL with QT is included, it is equivalent to releasing a QT DLL, and these libraries are QT, which is enough to ensure that the user knows that the program uses the LGPL version of Qt.

1) Windows platform static compilation

Static compilation first requires the

MinGW platform static compilation, when compiling QT, there is a configure.exe program,

Configure.exe-static-platform win32-g++

Can produce a statically compiled project file. And then

Mingw32-makesub-src

You can compile a static library. If you are just distributing the program, you can mingw32-makerelease sub-src to compile only one static library.

VC2015 platform, when compiling QT, Configure.exe-static-platform win32-msvc2015

Then NMAKESUB-SRC or NMAKE release SUB-SRC completes the static compilation.

Then, re-compile your program again with the statically compiled Qt.

You will get a very large executable program. It is recommended to use Asppack compression. Can be sent to the user.

2) Linux Platform static compilation

1, download the source installation program, such as Qt-everywhere-opensource-src-5.5.1.tar.gz

2. Unzip to a directory

3, CD into the directory after decompression, command:

./configure-static-release-qt-zlib-qt-gif-qt-libpng-qt-libmng-qt-libjpeg-nomakedemos-nomake Examples- Qt-sql-sqlite-prefix/usr/local/trolltech/qt-5.5.1_static

Parameter-static refers to the method of statically compiling QT

Parameter-release refers to a library that compiles only the release version

The parameter-qt-zlib-qt-gif-qt-libpng-qt-libmng-qt-libjpeg is better to determine that QT compiles programs that run under the current unknown system.

Parameter-nomake demos-nomake examples refers to not compiling demos and examples

Parameter-qt-sql-sqlite If you do not have this parameter, configure may indicate that SQLite has a problem and abort.

The parameter-prefix/usr/local/trolltech/qt-5.5.1_static indicates a statically compiled directory of QT installations, named Qt-5.5.1_static, to differentiate between the dynamically compiled and installed QT, because without this parameter, The installation will overwrite QT (if any) that was previously compiled and installed dynamically.

U Note: If an error occurs: Basic xlibfunctionality test failed!

You might need to modify theinclude and the library search paths by editing

Qmake_incdir_x11 andqmake_libdir_x11

Workaround: Yum installlibx*

(If it is Ubuntu you can also run sudo apt-getinstall libx11-dev libxext-dev libxtst-dev)

4, no problem after

Make

5, make no problem and then

sudo make install

6, add (or change) environment variables: (You can also do not add environment variables, using absolute path to compile), in your home directory under your name in the directory, in the. profile file (or change) the following environment variables: (set some specific environment variables for QT, this is very important!) The profile file is hidden and can be found in your name directory, press Ctrl+h to display all files.

qtdir=/usr/local/trolltech/qt-5.5.1_static/

Path= $QTDIR/bin: $PATH

Manpath= $QTDIR/doc/man: $MANPATH

Ld_library_path= $QTDIR/lib: $LD _library_path

Export Qtdir Pathmanpath Ld_library_path

7. Restart

8. Test the qmake at the terminal

3) static compilation of MAC OS x system Platform

In the Mac OS X system platform to statically compile QT application is still more troublesome, the first need to statically compile the QT application depends on the various libraries, and then the connection to build the run file, the specific steps to see the link: http://doc.qt.io/qt-5/osx-deployment.html.

3.2. Attaching DLL files

L advantages, easy to update, when publishing multiple products, you can use one library uniformly.

L shortcomings, many documents, miscellaneous.

The official QT development environment uses the dynamic link library method by default, when releasing the generated executable, we need to copy a lot of dynamic library, if we go to copy the dynamic library, it is likely absent-minded, cause the program will not function in other computers. As a result, the QT official development environment comes with a deployment tool to help developers automatically copy most of the dependent libraries. Different platforms are used differently.

1) Windows platform

The WINDEPLOYQT tool under the Windows Development environment (if you have added the Qt bin directory to the PATH environment, you can use the WINDEPLOYQT call directly at the command line). First, copy the executable file from the release file in the project to a new folder. For example, Project.exe, using the QT to generate the necessary DLL files WINDEPLOYQT, to the required dynamic library to the folder, open the command line, input windeployqtproject.exe, this time most of the DLL files are automatically copied, but if the project is also used Some other SDK, such as opencv,chartdir51 and so on, you need to manually copy the required DLLs, if you do not know which software you need, you can use Dependency walker to see which DLL files are missing.

The list of files under the folder after the copy is complete is as follows:

Note: If the published app is a QT Quick application app, then the command line needs to be added to the QML installation directory. The D:\QT\QT5.5.1\QML in the command is the QML installation directory, please replace it with your own QML installation directory!!!!!

WINDEPLOYQT Hello.exe--qmldir D:\QT\QT5.5.1\QML

Next, a tall friend can use the Enigma VirtualBox software virtualization tool to encapsulate multiple files into the application master file, making it a green software for single execution files.

2) Linux Platform

Under the X11 platform QT program, first prepare the program need to use the resources, libraries and plugins ..., such as your running program called the panel, that put your panel, those libqt*.so.4 and libqt*. so.4.6.0 (both the link and the shared library) are placed in the same directory (and can be different, as long as the shell file is changed slightly). Plugins not much to say.

In the same directory of the program, create a new empty document named Panel.sh (with the same name as the program name, with the extension Sh,shell file). Write the following statement intact in panel.sh:

#!/bin/sh

Appname= ' basename | Sed s,/.sh$,, '

Dirname= ' dirname$0 '

Tmp= "${dirname#?}"

If ["${dirname% $tmp}"! = "/"]; Then

Dirname= $PWD/$dirname

Fi

Ld_library_path= $dirname

Export Ld_library_path

$dirname/$appname $*

Save the file and exit. In terminal to file +x properties: Switch to the program's directory, enter

chmod +x panel.sh

Then run the shell file (make sure the panel program has the x attribute), it will automatically change the environment variables, run the program.

If you are debugging a shell file, you only need to enter it at the terminal:

Sh-x panel.sh

This will be OK.

Please refer to the link if you need to make a deb or RPM package for the execution program:

Deploying AQT5 Application Linux:https://wiki.qt.io/deploying_a_qt5_application_linux

3) MAC OS x System Platform

Since the QT library is not standard for OS X, we have to copy the library to the app package ourselves to allow the app to run on other computers that don't have QT installed.

Fortunately, QT provides us with the MACDEPLOYQT tool, which is the simplest of all platforms to publish QT-written programs on OS X.

Note: My computer is configured with a QT bin environment, so you can use MACDEPLOYQT directly, if not configured, you can use the absolute path to find.

1. Release the Widgets program

A) This is more convenient. Select release mode, compile

b) Run MACDEPLOYQT

For my project, the command is:

Macdeployqt/users/ocean/desktop/build-untitled-desktop_5_5_1_64bit-release/untitled.app-dmg

Then go to the carriage and pack. After that we will find that the app directory has more than one DMG file

This DMG file, inside the app is our release of the app. Copy the DMG to someone else and you can use it directly.

L Note: If you copy the app files directly to others, others are unable to run directly, there will be permissions problems (to use chmod to the executable file plus x permission to run). And after compressing (zip or dmg), copy to others, others can be run directly, no permissions problems.

L Note:-dmg means that after copying a good library, generate a DMG file, you can not add this parameter.

2. Release the QUICK2 program

    This is relatively troublesome.

A)         Select release mode, compile

B)         open the terminal, first switch to the target directory of the compilation

        for my project, the command is:

           cd/users/ocean/desktop/build-untitled-desktop_5_5_1_64bit-release

    

C)         run MACDEPLOYQT

        for my project, the command is:

& nbsp           MACDEPLOYQT untitled.app-qmldir=. /UNTITLED-DMG

        then enter and pack.

l   Note 1: Unlike the widgets publisher, Untitled.app the name, do not write it directly./untitled.app or other absolute/relative paths, Otherwise the packaged files cannot be used!! will be error!!!

l   Note 2:-qmldir=: /untitled means to be in a. /untitled  directory has qml files, let macdeployqt to analyze them, to use the library to find.

http://blog.csdn.net/liuyez123/article/details/50462637

QT full Platform settings icon, full platform static compilation good

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.