2010年10月28日 星期四

Use winsock2 in QT

The Qt-SDK comes with a WinSock2-Library, called libws2_32.a .
The only thing you have to do, is to enter this line in your .pro .

LIBS += C:\Qt\2009.04\mingw\lib\libws2_32.a

2010年7月16日 星期五

QT Setting the Application Icon

Setting the Application Icon on Windows
First, create an ICO format bitmap file that contains the icon image. This can be done with e.g. Microsoft Visual C++: Select File|New, then select the File tab in the dialog that appears, and choose Icon. (Note that you do not need to load your application into Visual C++; here we are only using the icon editor.)

Store the ICO file in your application's source code directory, for example, with the name myappico.ico. Then, create a text file called, say, myapp.rc in which you put a single line of text:

IDI_ICON1 ICON DISCARDABLE "myappico.ico"

Finally, assuming you are using qmake to generate your makefiles, add this line to your myapp.pro file:

RC_FILE = myapp.rc

Regenerate your makefile and your application. The .exe file will now be represented with your icon in Explorer.

If you do not use qmake, the necessary steps are: first, run the rc program on the .rc file, then link your application with the resulting .res file.

Setting the Application Icon on Mac OS X
The application icon, typically displayed in the application dock area, is set by calling QWidget::setWindowIcon() on a top-level widget. It is possible that the program could appear in the application dock area before the function call, in which case a default icon will appear during the bouncing animation.

To ensure that the correct icon appears, both when the application is being launched, and in the Finder, it is necessary to employ a platform-dependent technique.

Although many programs can create icon files (.icns), the recommended approach is to use the Icon Composer program supplied by Apple (in the Developer/Application folder). Icon Composer allows you to import several different sized icons (for use in different contexts) as well as the masks that go with them. Save the set of icons to a file in your project directory.

If you are using qmake to generate your makefiles, you only need to add a single line to your .pro project file. For example, if the name of your icon file is myapp.icns, and your project file is myapp.pro, add this line to myapp.pro:

ICON = myapp.icns

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

If you do not use qmake, you must do the following manually:

1.Create an Info.plist file for your application (using the PropertyListEditor, found in Developer/Applications).
2.Associate your .icns record with the CFBundleIconFile record in the Info.plist file (again, using the PropertyListEditor).
3.Copy the Info.plist file into your application bundle's Contents directory.
4.Copy the .icns file into your application bundle's Contents/Resources directory.
Setting the Application Icon on Common Linux Desktops
In this section we briefly describe the issues involved in providing icons for applications for two common Linux desktop environments: KDE and GNOME. The core technology used to describe application icons is the same for both desktops, and may also apply to others, but there are details which are specific to each. The main source of information on the standards used by these Linux desktops is freedesktop.org. For information on other Linux desktops please refer to the documentation for the desktops you are interested in.

Often, users do not use executable files directly, but instead launch applications by clicking icons on the desktop. These icons are representations of "desktop entry files" that contain a description of the application that includes information about its icon. Both desktop environments are able to retrieve the information in these files, and they use it to generate shortcuts to applications on the desktop, in the start menu, and on the panel.

More information about desktop entry files can be found in the Desktop Entry Specification.

Although desktop entry files can usefully encapsulate the application's details, we still need to store the icons in the conventional location for each desktop environment. A number of locations for icons are given in the Icon Theme Specification.

Although the path used to locate icons depends on the desktop in use, and on its configuration, the directory structure beneath each of these should follow the same pattern: subdirectories are arranged by theme, icon size, and application type. Generally, application icons are added to the hicolor theme, so a square application icon 32 pixels in size would be stored in the hicolor/32x32/apps directory beneath the icon path.

K Desktop Environment (KDE)
Application icons can be installed for use by all users, or on a per-user basis. A user currently logged into their KDE desktop can discover these locations by using kde-config, for example, by typing the following in a terminal window:

kde-config --path icon

Typically, the list of colon-separated paths printed to stdout includes the user-specific icon path and the system-wide path. Beneath these directories, it should be possible to locate and install icons according to the conventions described in the Icon Theme Specification.

If you are developing exclusively for KDE, you may wish to take advantage of the KDE build system to configure your application. This ensures that your icons are installed in the appropriate locations for KDE.

The KDE developer website is at http://developer.kde.org/.

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

gnome-config --datadir

The path printed on stdout refers to a location that should contain a directory called pixmaps; the directory structure within the pixmaps directory is described in the Icon Theme Specification.

If you are developing exclusively for GNOME, you may wish to use the standard set of GNU Build Tools, also described in the relevant section of the GTK+/Gnome Application Development book. This ensures that your icons are installed in the appropriate locations for GNOME.

The GNOME developer website is at http://developer.gnome.org/.

QT在windows下用qDebug()输出

打開.pro文件,加入一行:

win32:CONFIG += console

然后保存。再重新編譯,就可以看到控制台输出了。

要在主控台螢幕(Windows平台上面叫做「命令提示字元」視窗)上面輸出文字,有三種呼叫方式:其一,加入#include (包含於QtGui表頭檔內,若已加入#include 則可以省略這行)以便使用qDebug() << arg語法,至於qDebug("...", arg)語法則只需加入任何Qt表頭檔即可使用;其二,若想要透過C++的std::cout來輸出,則需要加入#include ;其三,使用C的printf()函式也無妨,並視需要加入#include (所謂的視需要是指如果編譯器有發生錯誤的話才加入)。

針對C的語法,若要將QString的內容轉為 printf()可以接受的字元陣列,需要多呼叫兩層,亦即先用toAscii()轉換、再用constData()取值,所以Qt裡面內建了一個方便的巨集,叫做qPrintable(),協助完成這兩個動作,以免每次都還要努力回想QString轉換成char*的步驟。

但是不管是挑選上述的何種方法來實現在命令列上面的文字輸出,都務必要在.pro專案檔內加入「CONFIG += console」這行敘述,否則無論是qDebug、cout或者printf都將不會顯示結果出來。換言之,在撰寫命令列的主控台應用程式的時候,這行敘述是絕對不可或缺的(除非「qmake -project」指令自己有偵測到該應用程式為主控台類型),否則將無法看到輸出結果,甚至可能還會誤以為是程式邏輯發生了什麼錯誤。

2010年7月15日 星期四

QT static 出現找不到libgcc_s_dw2-1.dll

MinGW靜態編譯
1.修改mkspecs/win32-g++目录下的配置文件qmake.conf
将下面一行:
QMAKE_LFLAGS = -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc
修改为:
QMAKE_LFLAGS = -static -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc
2.然后将下面一行:
QMAKE_LFLAGS_DLL = -shared
修改为:
QMAKE_LFLAGS_DLL = -static

QT static 出現找不到mingwm10.dll

要是想要一勞永逸
就開啟
Qt底下的\qt\mkspecs\win32-g++\qmake.conf


-mthreads

全部刪除
這樣之後生成的Makefile都不會出現了

QT win7 static

QT編譯成靜態(static)

所以接下來就要將QT編譯成靜態
複製你安裝QT的資料夾
預設路徑為C:\qt\發布的版本
例如我的是C:\qt\2010.02.1
複製一份命名為C:\qt\2010.02.1-static
接下來開啟QT的Qt Command Prompt
預設路徑為
開始→程式集→Qt SDK by Nokia v2010.02.1 (open source)→Qt Command Prompt
開啟後將目錄轉到你複製完成的資料夾底下的qt

cd C:\qt\2010.02.1-ststic\qt


接下來所要下的命令為

configure -static -release -fast

會出現
Which edition of Qt do you want to use ?
Type 'c' if you want to use the Commercial Edition.
Type 'o' if you want to use the Open Source Edition.

這時候就要打
"o"
按Enter

因為通常安裝的都是開放原始碼的授權程式
除非你有去購買授權版的

接下來就是會出現
This is the Qt for Windows Open Source Edition.

You are licensed to use this software under the terms of
the GNU Lesser General Public License (LGPL) version 2.1
or the GNU General Public License (GPL) version 3.

Type '3' to view the GNU General Public License version 3 (GPLv3).
Type 'L' to view the Lesser GNU General Public License version 2.1 (LGPLv2.1).
Type 'y' to accept this license offer.
Type 'n' to decline this license offer.

Do you accept the terms of the license?

輸入3或L是看那些開放原始碼的法律條文
要看的可以自己進去看
所以在這裡輸入
"y"
按Enter

像上面出現
Qt is now configured for building. Just run mingw32-make.
To reconfigure, run mingw32-make confclean and configure.
就成功了

接下來就會開始自動設定檔案
設定完成就輸入

mingw32-make

輸入完後要等大約2-3小時
出現類似以下的訊息
就代表編譯完成
mingw32-make[4]:
mingw32-make[3]:
mingw32-make[2]:
mingw32-make[1]:



編完後此Qt的檔案就會變成靜態的模式