ホーム · All Namespaces · 全てのクラス · 主要なクラス · グループ別クラス · モジュール一覧 · 関数一覧

[次: Tulip Container クラス]

Qt 4 の新機能

このドキュメントは Qt 3 と Qt 4 の最も重要な違いをカバーしています。これは包括的なポーティングガイドを意図しているものではなく、あなたが遭遇するであろう最も重要な移植性に関して述べています。また、Qt3からの互換性サポートについて説明します。

New Technologies in Qt 4

Qt 4 では以下のコアテクノロジーが導入されています:

Recent Additions to Qt 4

以下の新機能は Qt 4 の最初のリリース後に Qt に追加されたものです:

In Qt 4.3:

In Qt 4.2:

In Qt 4.1:

For more information about improvements in the current release, see the detailed list of changes.

Significant Improvements

The following modules have been significantly improved for Qt 4:

Build System

Unlike previous Qt releases, Qt 4 is a collection of smaller libraries:

LibraryDescription
QtCoreCore non-GUI functionality
QtGuiにリンクするようになっているCore GUI functionality
QtNetworkNetwork module
QtOpenGLOpenGL module
QtSqlSQLモジュール
QtSvgSVG rendering classes
QtXmlXML module
Qt3SupportQt 3 support classes
QAxContainerActiveQt client extension
QAxServerActiveQt server extension
QtAssistantClasses for launching Qt Assistant
QtDesignerClasses for extending and embedding Qt Designer
QtUiToolsClasses for dynamic GUI generation
QtTestTool classes for unit testing

QtCore contains tool classes like QString, QList, and QFile, as well as kernel classes like QObject ではなく QTimer. The QApplication class has been refactored so that it can be used in non-GUI applications. It is split into QCoreApplication (in QtCore) and QApplication (in QtGui).

This split makes it possible to develop server applications using Qt without linking in any unnecessary GUI-related code and without requiring GUI-related system libraries to be present on the target machine (e.g. Xlib on X11, Carbon on Mac OS X).

If you use qmake to generate your makefiles, qmake will by default link your application against QtCore ではなく QtGui. To remove the dependency upon QtGui, add the line

 QT -= gui

to your .pro file. To enable the other libraries, add the line

 QT += network opengl sql qt3support

Another change to the build system is that moc now understands preprocessor directives. qmake automatically passes the defines set for your project (using "DEFINES +=") on to moc, which has its own built-in C++ preprocessor.

To compile code that uses .ui files, you will also need this line in the .pro file:

 CONFIG += uic3

Include Syntax

The syntax for including Qt class definitions has become

 #include <QClassName>

For example:

 #include <QString>
 #include <QApplication>
 #include <QSqlTableModel>

This is guaranteed to work for any public Qt class. The old syntax,

 #include <qclassname.h>

still works, but we encourage you to switch to the new syntax.

If you attempt to include a header file from a library that isn't linked against the application, this will result in a compile-time warning (e.g., "QSqlQuery: No such file or directory"). You can remedy to this problem either by removing the offending include or by specifying the missing library in the QT entry of your .pro ファイルの QT エントリに加えることで回避できる. (上記の Build System を見よ)

ライブラリの名前を単純に記述することで、ライブラリに含まれる全てのクラス定義をインクルードすることができる. 例:

 #include <QtCore>

名前空間

Qt 2 introduced a class called Qt for global-like constants (e.g., Qt::yellow) が導入された. Qt 2 がリリースされた当時には C++ の名前空間を扱えないコンパイラもあったので、名前空間の導入は見送られていた.

Qt 4 では、この Qt クラスは 名前空間 Qt に取って代えられる. 名前空間 Qt 内にある定数にアクセスするには、プレフィクス Qt::: (e.g., Qt::yellowとする) か、

 using namespace Qt;

という指令をソースファイルの先頭、 #include 指令の後に追加すること. using namespace syntax you don't need the prefix (e.g., yellow で十分である)

Qt 3 アプリケーションを移植する場合、これに関連したソースコードの互換性問題に遭遇するおそれがある. たとえば、Qt 3 ではたとえば、Qt 3 では QWidget::yellow の代わりに Qt::yellowと書くことができた. しかし QWidget -これはQTから継承されていて、Qt4では動かない。そのため Qt::yellow と書きなおすか、あるいは"using namespace" 指令を追加して Qt::プレフィクスを取り除くかしなければならない.

この qt3to4 porting tool automates this conversion.

QObject/QWidget コンストラクタ

Qt4で、私たちは QObject/QWidget サブクラスのコンストラクタを簡素化しようとしました。 これは、サブクラス化をより簡単にして、それと同時にQtライブラリをより効率的にするのを助けます。

コンストラクタは「const char *name」を引数に取らなくなりました。もし、貴方が名前を QObjectに付けたければ、貴方は、 QObject::setObjectName(const QString & name)メソッドをインスタンス化の後に実行しなければなりません。オブジェクトの名前は QString. The reasons for this change are:

QWidget's WFlags data type has been split in two: Qt::WindowFlags specifies low-level window flags (the type of window and the frame style), whereas Qt::WidgetAttribute specifies various higher-level attributes about the widget (e.g., WA_StaticContents). Widget attributes can be set at any time using QWidget::setAttribute(); low-level window flags can be passed to the QWidget constructor or set later using QWidget::setParent(). As a consequence, the constructors of most QWidget subclasses don't need to provide a WFlags parameter.

The parent parameter of all QObject classes in Qt defaults to a 0 pointer, as it used to do in Qt 1. This enables a style of programming where widgets are created without parents and then inserted in a layout, at which point the layout automatically reparents them.

動的キャスト

Qt 4 provides a qobject_cast<>() function that performs a dynamic cast based on the meta-information generated by moc for QObject subclasses. Unlike the standard C++ dynamic_cast<>() construct, qobject_cast<>() works even when RTTI is disabled, and it works correctly across DLL boundaries.

Here's the Qt 3 idiom to cast a type to a subtype:

 // DEPRECATED
 if (obj->inherits("QPushButton")) {
     QPushButton *pushButton = (QPushButton *)obj;
     ...
 }

The Qt 4 idiom is both cleaner and safer, because typos will always result in compiler errors:

 QPushButton *pushButton = qobject_cast<QPushButton *>(obj);
 if (pushButton) {
     ...
 }

QPointer<T>

The QPointer<T> class provides a pointer to type T (where T inherits from QObject) that is automatically set to 0 when the referenced object is destroyed. Guarded pointers are useful whenever you want to store a pointer to an object you do not own.

Example:

 QLabel *label = new QLabel;
 QPointer<QLabel> safeLabel = label;
 safeLabel->setText("Hello world!");
 delete label;
 // safeLabel is now 0, whereas label is a dangling pointer

QPointer<T> is more or less the same as the old QGuardedPtr<T> class, except that it is now implemented in a much more lightweight manner than before. The cost of one QPointer<T> object is now approximately the same as that of a signal--slot connection.

描画イベント

Qt 4はプラットフォーム透過なダブルバッファリングをサポートします.この特徴は,QWidget::setAttribute(Qt::WA_PaintOnScreen)を呼ぶことでウィジェットごとに機能を止めることができます.

この結果,すべての描画はpaintEvent()関数から行なわなければなりません(原文:must be done).これはMac OS XのHIView APIでも同様です.実際には,これはほとんど問題にはなりません.なぜなら描画イベントをつくる(原文:create paint event)ために,更新領域を引数にupdate()をコード内のどこからでも呼ぶことができるからです.

ポーティングを助けるために, QWidget Qt::WA_PaintOutsidePaintEvent 属性をサポートします.この属性はWindowsやX11で paintEvent() on Windows and X11.

Qt3サポート層

Qt 4 provides an extension library that applications based on Qt 3, called Qt3Support, that Qt applications can link against. This allows for more compatibility than ever before, without bloating Qt.

To enable the Qt 3 support classes and functions, add the line

 QT += qt3support

to your .pro file.

On Visual C++ 7 and GCC 3.2+, using compatibility functions often results in a compiler warning (e.g., "'find' is deprecated"). If you want to turn off that warning, add the line

 DEFINES += QT3_SUPPORT

to your .pro file.

If you want to use compatibility functions but don't want to link against the Qt3Support library, add the line

 DEFINES += QT3_SUPPORT_WARNINGS

or

 DEFINES += QT3_SUPPORT

to your .pro file, depending on whether you want compatibility function calls to generate compiler warnings or not.

[Next: The Tulip Container Classes]


Copyright © 2008 Nokia Trademarks
Qt 4.4.3