Classes - Annotated - Tree - Functions - Home - Structure

Porting to Qt 3.x

You're probably looking at this page because you want to port your application from Qt 2.x to Qt 3.x, but to be sure, let's review the good reasons to do this:

The Qt 3.x series is not binary compatible with the 2.x series. This means programs compiled for Qt 2.x must be recompiled to work with Qt 3.x. Qt 3.x is also not completely source compatible with 2.x, however all points of incompatibility cause compiler errors or run-time messages (rather than mysterious results). The result is that Qt 3.x includes many additional features, discards obsolete functionality that is easily converted to use the new features, and that porting an application from Qt 2.x to Qt 3.x is a simple task well worth the amount of effort required.

To port code using Qt 2.x to use Qt 3.x:

  1. Briefly read the porting notes below to get an idea of what to expect.
  2. Be sure your code compiles and runs well on all your target platforms with Qt 2.x.
  3. Recompile with Qt 3.x. For each error, search below for related identifiers (eg. function names, class names) - this documented is structured to mention all relevant identifiers to facilitate such searching, even if that makes it a little verbose.
  4. If you get stuck, ask on the qt-interest mailing list, or Trolltech Technical Support if you're a Professional Edition licensee.

Many very major projects, such as ### have been ported, so there is plenty of expertise in the collective conscious that is the Qt Developer Community!

Header file inclusion changes

Qt 3.x remove some unnecessary nested #include directives from header files. This speeds up compilation when you don't need those nested header files. But in some cases you will find you need to add an extra #include to your files.

For example, if you get a message about QStringList or its functions not being defined, then add #include <qstringlist.h> at the top of the file giving the error.

Header files that you might need to add #include directives for include:

Namespace

Qt 3.x is namespace clean. A few global identifiers that had been left in Qt 2.x have been discarded.

Enumeration Qt::CursorShape and it values are now part of the special Qt class defined in qnamespace.h. If you get compilation errors about these being missing (unlikely, since most of your code will be in classes that inherit from the Qt namespace class), then change:

The name of some debugging macro variables has been changed. We have tried not to break source compatibility as much as possible. If you observe error messages on the UNIX console or the Windows debugging stream that were previously disabled, please check these macro variables:

The name of some debugging macro functions has been changed as well but source compatibility should not be affected if the macro variable QT_CLEAN_NAMESPACE is not defined:

For the record, undocumented macro variables that are not part of the API have been changed:

Removed functions

All these functions have been removed in Qt 3.x:

Additionally, the following preprocessor directives have been removed:

See the changes-3.0.0 document for motivation why this had to be done. You might have been relying on the non-portable and unpredictable behavior resulting from these directives. We strongly recommend to either make use of the safe qstr* variants directly or ensure that no 0 pointer is passed to the standard C functions in your code base.

Collection class renaming

The classes QArray, QCollection, QList, QListIterator, QQueue, QStack and QVector have been renamed QMemArray, QPtrCollection, QPtrList, QPtrListIterator, QPtrQueue and QPtrStack, and they are found in the header files <qmemarray.h>, <qptrcollection.h>, <qptrlist.h>, <qptrqueue.h>, <qptrstack.h> and <qptrvector.h>. To ease porting, the old names and the old header-file names are still supported.

QButtonGroup

In Qt 2.x, the function QButtonGroup::selected() returns the selected radio button (QRadioButton). In Qt 3.0, it returns the selected toggle button (QButton::toggleButton), a more general concept. This might affect programs that use QButtonGroup containing a mixture of radio buttons and non-radio toggle buttons.

QDate

Two member functions of QDate were virtual in Qt 2.0 and are not anymore in Qt 3.0. The information here is relevant only if you subclassed QDate and reimplemented these functions:

Apart from losing their virtualness, QDate::monthName() and QDate::dayName() have been renamed QDate::shortMonthName() and QDate::shortDayName() and have been made static (as they always should had been). The old names are still provided for source compatibility.

QFont

The internals of QFont have changed significantly between Qt 2.2 and Qt 3.0, to give better Unicode support and make developing internationalized applications easier. The external behaviour has however not changed a lot. The main change is that the CharSet enum in QFont and functions related to it have disappeared. This comes about because Qt does now handle all charset related issues internally, and takes this burden away from the developer.

If you used the CharSet enum anywhere or any functions as QFont::charSet(), QFont::setCharSet() just remove them from your code to get things working again. There are a few other functions that took a QFont::CharSet as parameter. Just remove the charset from the parameterlist.

QInputDialog

The two static getText(...) methods in QInputDialog have been merged. The echo parameter is the third parameter and defaults to QLineEdit::Normal.

If you used calls to QInputDialog::getText(...) that provided more than the first two required parameters you will have to add a value for the echo parameter.

QLayout and Related Abstract Layout Classes

The definition of QGLayoutIterator, QLayout, QLayoutItem, QLayoutIterator, QSpacerItem and QWidgetItem were moved from <qabstractlayout.h> to <qlayout.h>. The header <qabstractlayout.h> now includes <qlayout.h> for compatibility. It might be removed in a future version.

QMultiLineEdit

The QMultiLineEdit was a simple editor widget in former Qt versions. Since Qt 3.0, which comes with a new richtext engine, which also supports editing, QMultiLineEdit is obsolete. It is still included for compatibility reasons. It is now a subclass of QTextEdit which wrappes the old QMultiLineEdit so that it is mostly source compatible to keep old applications working.

If you implement something new with QMultiLineEdit, you should use QTextEdit instead.

Although most of the old QMultiLineEdit API is still available, there is one important difference. Because of a design flaw of the old QMultiLineEdit it operated on lines and not on paragraphs. As lines do change all the time during wordwrap, the new richtext engine only knows paragraphs as elements in the data structure. So all functions (like numLines(), textLine()), which worked on lines, now work on paragraphs.

Also the function getString() has been removed as this function published the internal data structure.

So, applications which made normal usage of the QMultiLineEdit, should keep working without problems. Programs which did some special stuff with it might require some porting.

The source code for the old 2.x version of QMultiLineEdit can be found in $QTDIR/src/attic/qtmultilineedit.h/cpp. Note that the class has been renamed to QtMultiLineEdit to avoid name clashes. If you really need to keep compatibility with the old QMultiLineEdit, simply include this class in your project and rename all references from QMultiLineEdit to QtMultiLineEdit.

QPrinter

QPrinter has undergone some changes, to make it more flexible and force the same runtime behaviour on Unix and on Windows. In 2.x, QPrinter behaved differently on Windows and Unix, when using view transformations on the QPainter. This has changed now, and QPrinter behaves consistent across all platforms. A compatibilty mode has been added that behaves exactly as in Qt-2.x, to ease porting from Qt-2.x to Qt-3. This compatibilty mode can be enabled by passing a QPrinter::Compatible flag to the QPrinter constructor.

On X11, QPrinter used to generate encapsulated postscript when fullPage() was TRUE and only one page was printed. This does not happen by default anymore, giving a more consistent printing output.

QRegExp

The QRegExp class has been rewritten to support most of the features of Perl regular expressions. Both the regular expression syntax and the QRegExp interface were modified.

Be also aware that <qregexp.h> is no longer included automatically when you include <qstringlist.h>. See above for details.

New special characters

There are five new special characters: (, ), {, | and } (parentheses, braces and pipe). When porting old regular expressions, you have to add \ (backslash) in front of any of these (actually, \\ in C++ strings), unless it is already there.

Example: Old code like

    QRegExp rx( "([0-9|]*\\)" );        // works in Qt 2.x

should be converted into

    QRegExp rx( "\\([0-9]\\|]*\\)" );   // works in Qt 2.x and 3.x

However, wildcard patterns need no conversion. Here are two examples:

    QRegExp wild( "(*.*)" );
    wild.setWildcard( TRUE );

    // TRUE as third argument means wildcard
    QRegExp wild( "(*.*)", FALSE, TRUE );

Usually, QRegExp is easy to spot in programs. However, in one unfortunate case, it is subtle: as first argument to QString::replace(). Code like

    QString text = fetch_it_from_somewhere();
    text.replace( QString("([^)]*)"), "" );

involves an implicit QRegExp (namely, "([^)]*)") that should be changed.

QRegExp::operator=()

This function has been replaced by QRegExp::setPattern() in Qt 2.2. Old code such as

    QRegExp rx( "alpha" );
    rx.setCaseSensitive( FALSE );
    rx.setWildcard( TRUE );
    rx = "beta";

still compiles with Qt 3, but produces a different result (the case sensitivity and wildcard options are forgotten). This way,

    rx = "beta";

is the same as

    rx = QRegExp( "beta" );

which is what one expects.

QRegExp::match()

The following function is now obsolete, as it has an unwieldy parameter list and a bad name:

It will be removed in a future version of Qt. Its documentation explains how to accomplish the same.

QRegExp::find()

This function was removed, after a brief appearance in Qt 2.2. Its name was too confusing. Use QRegExp::search() or QString::find() instead.

QTableView

The QTableView class has been obsoleted and is no longer a part of the Qt API. You should either use the powerful QTable class or the simplistic QGridView in any new code you create. If you really need the old table view for compatibility issues you can find it in $QTDIR/src/attic/qttableview.{cpp,h}. Note that the class has been renamed from QTableView to QtTableView to avoid name clashes. To use it, simply include it in your project and rename all references in your code from QTableView to QtTableView.

QToolButton

The QToolButton class used to distinguish between "on" and "off" icons. In 3.0, this mechanism was moved into the QIconSet class (see QIconSet::State).

The old QToolButton::onIconSet and QToolButton::offIconSet properties are still provided for old source to compile, but their semantics have changed: They are now synonyms for QToolButton::iconSet. If you used that distinction in Qt 2, you will need to adjust your code to use the QIconSet On/Off mechanism.

Likewise, the on parameter of these two functions is now ignored:

These functions are still provided only for ease of porting. New code should rather use the following:

Finally, this function is no longer virtual:

If you have a class that inherits QToolButton and that reimplements QToolButton::setIconSet(), you should make the signature of the reimplementation agree with the new QToolButton::setIconSet(), a virtual function.

QTextStream

The global QTextStream manipulators setw(), setfill() and setprecison() were renamed to qSetW(), qSetFill() and qSetPrecision() to avoid conflicts with the std libs. If you used them, you have to rename the occurences to the new name.

QTranslator

The QTranslator class was extended in Qt 2.2, and these extensions lead to a new interface. This interface is used mainly by translation tools (such as Qt Linguist). For source compatibility, no member function was effectively removed. The QTranslator documentation points out which functions are obsolete.

However, the following function is no longer virtual:

If you have a class that inherits QTranslator and that reimplements QTranslator::find(), you should reimplement QTranslator::findMessage() instead. In fact, find() is now defined in terms of findMessage(). By doing the conversion, you will also gain support for translator comments and for any future extension.

QXml classes

QXmlInputSource

The semantics of QXmlInputSource has slightly changed. This change affects only code that parses the same data from the same input source multiple times. In that cases you have to do a QXmlInputSource::reset() before the second call to QXmlSimpleReader::parse().

So code like:

    QXmlInputSource source( &xmlFile );
    QXmlSimpleReader reader;
    ...
    reader.parse( source );
    ...
    reader.parse( source );

Has to be changed to:

    QXmlInputSource source( &xmlFile );
    QXmlSimpleReader reader;
    ...
    reader.parse( source );
    ...
    source.reset();
    reader.parse( source );

QXmlLocator

Due to some internal changes, it was necessary to clean-up the semantics of QXmlLocator: this class is now an abstract class. This should be in general no problem, since one usually used the QXmlLocator that was reported by QXmlContentHandler::setDocumentLocator().

If you used this class in another way as the above described, you have to adjust you code to use the QXmlLocator that is reported by the QXmlContentHandler::setDocumentLocator() function.

Bezier curves

The function names for Bezier curves in QPainter and QPointArray have been corrected. They now properly reflect their cubic form instead of a quadratic one. If you have been using either QPainter::drawQuadBezier() or QPointArray::quadBezier() you will have to replace these calls with

respectively. Neither the arguments nor the resulting curve have changed.

Locale-aware string comparisons in item classes (QIconView, QListBox, QListView and QTable)

In Qt 2.x, QString provided only string comparison using the Unicode values of the characters of a string. This is efficient and reliable, but it is not the appropriate order for most languages. For example, French users expect 'é' (e acute) to be treated essentially as 'e' and not put after 'z'.

In Qt 3.0, QString::localeAwareCompare() implements that on some platforms. The classes QIconView, QListBox, QListView and QTable now use QString::localeAwareCompare() instead of QString::compare(). The impact of this is usually positive. Otherwise, you can always reimplement QIconViewItem::compare(), QListBox::text(), QListViewItem::compare() or QTableItem::key().


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta3