Development/Tutorials/KDE4 Porting Guide (zh CN)

Jump to: navigation, search


Contents

Development/Tutorials/KDE4_Porting_Guide


简介

本教程帮助开发人员将 Qt3/KDE3的程序移植到Qt4/KDE4。这并不是个很困难的过程。现在已经有了很多脚本,代码和文档;可以提供很多帮助。

常用语

本教程中使用以下短语:

  • program指可执行程序。
  • path 指路径
  • file 指文件
  • $SVN 是可以从代码仓库中检出的完整路径。

CMake

与KDE3不同, KDE4借助于 CMake来构建整个项目。The easiest way to port your autotools system to CMake is with the help of the am2cmake script which can be found in the cmake/scripts directory of the kdesdk module. 这会根据你原有的联编文件创建一系列CMakeLists.txt文件。

例如,在/path/to/src 有源代码,那么:

% cd /path/to/src
% $SVN/trunk/KDE/kdesdk/cmake/scripts/am2cmake --kde4
</code>
运行 <tt> am2cmake --help</tt>,检查你是否需要有<tt>--kde4</tt>标志。
 
有个工具可以检查产生的 {{path|CMakeList.txt}}文件,以期避免一些潜在的问题。这个工具叫做 {{program|cmakelint.pl}},在{{path|$SVN/trunk/kde/kdesdk/scripts}}可以找到。用法如下:
<syntaxhighlight lang="bash">
% cd /path/to/src
% $SVN/trunk/KDE/kdesdk/scripts/cmakelint.pl CMakeLists.txt
</code>
或者,在你整个代码树目录下运行:
<syntaxhighlight lang="bash">
% cd /path/to/src
% find . -name CMakeLists.txt | \
  xargs $SVN/trunk/KDE/kdesdk/scripts/cmakelint.pl
</code>
 
==Qt4 API==
An overview of the Qt3 to Qt4 transition is provided in Trolltech's 
[http://www.trolltech.com/products/qt/whatsnew/porting "Moving from Qt 3 to Qt 4"] paper.  This document provides an excellent overview into the major functionality changes with Qt4 and is highly recommended.
 
The follow-on [http://doc.trolltech.com/latest/porting4.html "Porting to Qt 4"] page gives an amazingly detailed description of the porting process, along with a list of the changes in the classes and functions.
 
These documents describe a tool provided by Trolltech called {{program|qt3to4}} that can help port the Qt parts of your code from Qt3 to Qt4, using compatibility functions.  Run {{program|qt3to4}} as follows:
<syntaxhighlight lang="bash">
% $SVN/trunk/qt-copy/bin/qt3to4 [options] <Infile>, [Infile], ...
</code>
Infile can be a source file or a project file. If you specify a project file, ending with '.pro' or '.pri', {{program|qt3to4}} will port all files specified in that project.
 
For more information, run {{program|qt3to4}} with the "--help" option or visit 
the [http://doc.trolltech.com/latest/qt3to4.html "qt3to4-The Qt 3 to 4 Porting Tool"] page.
 
Additionally, there is a program called {{program|remove-qt3-support.pl}} in the {{module|kdesdk}} module that will search and replace lots of deprecated Qt3 stuff for you. Simply run this program without any options in the source directory.
<syntaxhighlight lang="bash">
% $SVN/trunk/KDE/kdesdk/scripts/qt4/remove-qt3-support.pl
</code>
 
Chances are that some Qt3 classes will still remain in the code, so make sure your top-level CMakeLists.txt file defines the QT3_SUPPORT and QT3_SUPPORT_WARNINGS macros for the Qt headers, like so:
<syntaxhighlight lang="text">
add_definitions (-DQT3_SUPPORT -DQT3_SUPPORT_WARNINGS)
</code>
 
==KDE4 API==
Much of the porting effort consists of simply renaming class names and header files. Since it would be rather tedious to change all these by hand, there is a handy script in the {{path|scripts/qt4}} directory of {{module|kdesdk}} called {{program|adapt-to-kde4-api.pl}}. This will update all your files and output a {{program|diff}} so you can see what changes were made.
 
Once that simple code substitution has been done, you will still have to go through your code to port to, for example, the new <tt>KAction</tt> API. Documentation about all API changes is kept in the [http://websvn.kde.org/*checkout*/trunk/KDE/kdelibs/KDE4PORTING.html KDE4PORTING.html] file in the {{module|kdelibs}} module.
 
==Qt Designer 图形文件==
Qt designer ".ui" files created using Qt3 must be converted to the new Qt4 format.  This can be done using the {{program|uic3}} program available from your Qt4 installation.
 
<syntaxhighlight lang="bash">
% $SVN/trunk/qt-copy/bin/uic3 -convert file.ui > foo.ui
% mv foo.ui file.ui
</code>
 
Or, if you prefer a graphical tool, you can use Qt4's designer program
 
<syntaxhighlight lang="bash">
% $SVN/trunk/qt-copy/bin/designer file.ui
(you can save file.ui over top itself, or save to a new file)
</code>
{{Warning|Beware, the conversion process loses any custom slots, table columns, etc.  So, you may need to put some stuff back by hand.}}
You should also run the {{program|fixuifiles}} program from the {{module|kdesdk}} module, it performs cleanups and sanity checks:
<syntaxhighlight lang="bash">
% $SVN/trunk/KDE/kdesdk/scripts/fixuifiles
</code>
 
==D-Bus==
与KDE3的DCOP不同,KDE4使用D-Bus来完成进程间通讯。从DCOP向D-Bus移植是个大标题,在 [[Development/Tutorials/Porting_to_D-Bus|D-Bus移植教程]]有关于他的详细内容。
更多信息,请见[[Development/Tutorials#D-Bus|D-Bus教程]].
 
==图标==
KDE4 uses the [http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html freedesktop.org icon naming specification] as the basis for icon names. This means that both the icons that ship with KDE4 (Oxygen) as well as components in kdelibs that use icons follow this specification.
 
Porting your app from the icon names used in KDE3 to the ones used in KDE4 is as easy as running the [http://websvn.kde.org/*checkout*/trunk/KDE/kdesdk/scripts/qt4/adapt-to-icon-spec.py adapt-to-icon-spec.py] script from the root directory of your project and follow the instructions on screen. 
 
The script automatically converts confirmable positives (e.g. uses of KIcon or KIconLoader), skips confirmable negatives and prompts for what to do with possible positives. It shows the latter with additonal context if desired and makes it a simple matter of pressing 'y' or 'n' for the possible hits to complete the porting.
 
==国际化==
To create your ".pot" file, copy the commands from the 'messages' rule in your projects {{path|Makefile.am}} to a shell script called {{path|Messages.sh}}.  You may assume the same variables ($PREPARETIPS, $XGETTEXT, $podir, etc.) still exist, but keep in mind the differences between Makefile and shell script syntax.
 
Also be careful that if you use the -k parameter with $XGETTEXT, you will need to explicitly list all variants that you use.
 
例如, 消息('messages')的创建规则:
<syntaxhighlight lang="text">
messages: rc.cpp
        rm -f tips.cpp
        $(PREPARETIPS) > tips.cpp
        $(XGETTEXT) -ktranslate *.cpp *.h -o $(podir)/kmail.pot
        rm -f tips.cpp
</code>
会有{{path|Messages.sh}}脚本:
<syntaxhighlight lang="bash">
#! /usr/bin/env bash
$PREPARETIPS > tips.cpp
$XGETTEXT -ktranslate:1,1t -ktranslate:1c,2,2t *.cpp *.h -o $podir/kmail.pot
rm -f tips.cpp
</code>
 
==要和不要==   
* 不要使用老的套接字类
* 不要使用{{qt3|QPtrList}}, 总的来说,用setAutoDelete()   
* 不要调用raster操作。
* 不要在画图事件以外的地方编写代码改变图形界面。
* 不要使用{{qt3|QHBox}}, {{qt3|QVBox}}, {{qt3|QGrid}}.建议使用布局(layouts)
* Do NOT play with frames of groupboxes, labels, or lineedits to fake a different widget. Use the appropriate widget instead. e.g., instead of setting a label to have a sunken lineedit border, how about using a readonly {{class|KLineEdit}} instead? And instead of using a {{class|KLineEdit}} without a border for a copyable widget, use {{class|KActiveLabel}}.   
* 不要使用没有边界的Groupbox来把所有的元素都归起来。使用布局(layout)。
 
==资源==   
===文档===   
* [http://doc.trolltech.com/4.3 Qt4.3参考]   
* [http://developer.kde.org/documentation/library/svn-api.php KDE4 API参考]   
* [http://www.cmake.org/HTML/Documentation.html CMake夸平台Make]   
* [http://websvn.kde.org/*checkout*/trunk/KDE/kdelibs/KDE4PORTING.html KDE4PORTING.html]
 
===其他帮助===   
* #kde-devel on irc.freenode.net   
 
[[Category:KDE4]]

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal