| Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |
This document describes porting applications from Qt 3 to Qt 4. If you haven't yet made the decision about porting, or are unsure about whether it is worth it, take a look at the key features offered by Qt 4. See also Getting Ready for Qt 4 for tips on how to write Qt 3 code that is easy to port to Qt 4.
The Qt 4 series is not binary compatible with the 3 series. This means programs compiled for Qt 3 must be recompiled to work with Qt 4. Qt 4 is also not completely source compatible with 3, however nearly all points of incompatibility cause compiler errors or run-time messages (rather than mysterious results). Qt 4 includes many additional features and discards obsolete functionality. Porting from Qt 3 to Qt 4 is straightforward, and once completed makes the considerable additional power and flexibility of Qt 4 available for use in your applications.
To port code from Qt 3 to Qt 4:
The qt3to4 porting tool replaces occurrences of Qt 3 classes that don't exist anymore in Qt 4 with the corresponding Qt 3 support class; for example, QListBox is turned into Q3ListBox.
At some point, you might want to stop linking against the Qt 3 support library (Qt3Support) and take advantage of Qt 4's new features. The instructions below explain how to do that for each compatibility class.
In addition to the Qt3Support classes (such as Q3Action, Q3ListBox, and Q3ValueList), Qt 4 provides compatibility functions when it's possible for an old API to cohabit with the new one. For example, QString provides a QString::simplifyWhiteSpace() compatibility function that's implemented inline and that simply calls QString::simplified(). The compatibility functions are not documented here; instead, they are documented for each class.
If you have the line QT += qt3support in your .pro file, qmake will automatically define the QT3_SUPPORT symbol, turning on compatibility function support. You can also define the symbol manually (e.g., if you don't want to link against the Qt3Support library), or you can define QT3_SUPPORT_WARNINGS instead, telling the compiler to emit a warning when a compatibility function is called. (This works only with GCC 3.2+ and MSVC 7.)
If you get stuck, ask on the qt4-preview-feedback mailing list. If you are a licensed customer, you can also contact Trolltech support.
Table of contents:
The table below lists the classes that have been renamed in Qt 4. If you compile your applications with QT3_SUPPORT defined, the old names will be available.
Whenever you see an occurrence of the name on the left, you can safely replace it with the Qt 4 equivalent in your program. The qt3to4 tool performs the conversion automatically.
| Qt 3 class name | Qt 4 class name |
|---|---|
| QIconSet | QIcon |
| QWMatrix | QMatrix |
| QGuardedPtr | QPointer |
The table below lists the enums and typedefs that have been renamed in Qt 4. If you compile your applications with QT3_SUPPORT defined, the old names will be available.
Whenever you see an occurrence of the name on the left, you can safely replace it with the Qt 4 equivalent in your program. The qt3to4 tool handles performs the conversion automatically.
| Qt 3 type name | Qt 4 type name |
|---|---|
| QApplication::ColorMode | QApplication::ColorSpec |
| QButton::ToggleState | QCheckBox::ToggleState |
| QCursorShape | Qt::CursorShape |
| QFile::FilterSpec | QFile::Filters |
| QFile::PermissionSpec | QFile::Permission |
| QFile::SortSpec | QFile::SortFlags |
| QFile::Status | QFile::Error |
| QFileInfo::PermissionSpec | QFile::Permission |
| QGrid::Direction | Qt::Orientation |
| QGridWidget::Direction | Qt::Orientation |
| QIODevice::Offset | qlonglong |
| QImage::ScaleMode | Qt::AspectRatioMode |
| QSize::ScaleMode | Qt::AspectRatioMode |
| QSocket::Error | Q3Socket::Error |
| QSocket::State | Q3Socket::State |
| QStyle::SCFlags | QStyle::SubControls |
| QStyle::SFlags | QStyle::State |
| QTS | QTextStream |
| QUrlDrag | QUriDrag |
| QWidget::FocusPolicy | Qt::FocusPolicy |
| Q_LLONG | qlonglong |
| Q_ULLONG | qulonglong |
| Qt::Dock | Qt::ToolBarDock |
| Qt::MacintoshVersion | QSysInfo::MacVersion |
| Qt::TextFlags | Qt::TextFlag |
| Qt::WindowsVersion | QSysInfo::WinVersion |
The table below lists the enum values that have been renamed in Qt 4. If you compile your applications with QT3_SUPPORT defined, the old names will be available.
Whenever you see an occurrence of the name on the left, you can safely replace it with the Qt 4 equivalent in your program. The qt3to4 tool handles performs the conversion automatically.
In addition, the following window flags have been either replaced with widget attributes or have been deprecated:
| Qt 3 type | Qt 4 equivalent |
|---|---|
| Qt::WDestructiveClose | Use QWidget::setAttribute(Qt::WA_DeleteOnClose) instead. |
| Qt::WStaticContents | Use QWidget::setAttribute(Qt::WA_StaticContents) instead. |
| Qt::WNorthWestGravity | |
| Qt::WNoAutoErase | Use QWidget::setAttribute(Qt::WA_NoBackground) instead. |
| Qt::WResizeNoErase | |
| Qt::WRepaintNoErase | |
| Qt::WPaintClever | Unnecessary in Qt 4. |
| Qt::WMacNoSheet | Unnecessary in Qt 4. |
Some properties have been renamed in Qt 4, to make Qt's API more consistent and more intuitive. For example, QWidget's caption property has been renamed windowTitle to make it clear that it refers to the title shown in the window's title bar.
The table below lists the Qt properties that have been renamed in Qt 4. Occurrences of these in Qt Designer .ui files are automatically converted to the new name by uic.
| Qt 3 name | Qt 4 name |
|---|---|
| QButton::accel | QButton::shortcut |
| QButton::on | QButton::checked |
| QButton::toggleButton | QAbstractButton::checkable |
| QDial::lineStep | QDial::singleStep |
| QDial::maxValue | QDial::maximum |
| QDial::minValue | QDial::minimum |
| QDialog::modal | QDialog::isModal |
| QLineEdit::edited | QLineEdit::modified |
| QLineEdit::hasMarkedText | QLineEdit::hasSelectedText |
| QLineEdit::markedText | QLineEdit::selectedText |
| QObject::name | QObject::objectName |
| QProgressDialog::progress | QProgressDialog::value |
| QProgressDialog::totalSteps | QProgressDialog::maximum |
| QProgressDialog::wasCancelled | QProgressDialog::wasCanceled |
| QPushButton::iconSet | QPushButton::icon |
| QScrollBar::draggingSlider | QScrollBar::sliderDown |
| QScrollBar::lineStep | QScrollBar::singleStep |
| QScrollBar::maxValue | QScrollBar::maximum |
| QScrollBar::minValue | QScrollBar::minimum |
| QSlider::lineStep | QSlider::singleStep |
| QSlider::maxValue | QSlider::maximum |
| QSlider::minValue | QSlider::minimum |
| QSpinBox::lineStep | QSpinBox::singleStep |
| QSpinBox::maxValue | QSpinBox::maximum |
| QSpinBox::minValue | QSpinBox::minimum |
| QTabBar::currentTab | QTabBar::currentIndex |
| QTabWidget::currentPage | QTabWidget::currentWidget |
| QToolButton::iconSet | QToolButton::icon |
| QToolButton::textLabel | QToolButton::text |
| QWidget::caption | QWidget::windowTitle |
| QWidget::icon | QWidget::windowIcon |
| QWidget::iconText | QWidget::windowIconText |
A handful of properties in Qt 3 are no longer properties in Qt 4, but the access functions still exist as part of the Qt 4 API. These are not used by Qt Designer; the only case where you need to worry about them is in highly dynamic applications that use Qt's meta-object system to access properties. Here's the list of these properties with the read and write functions that you can use instead:
| Qt 3 property | Qt 4 read function | Qt 4 write function |
|---|---|---|
| QSqlDatabase::connectOptions | QSqlDatabase::connectOptions() | QSqlDatabase::setConnectOptions() |
| QSqlDatabase::databaseName | QSqlDatabase::databaseName() | QSqlDatabase::setDatabaseName() |
| QSqlDatabase::hostName | QSqlDatabase::hostName() | QSqlDatabase::setHostName() |
| QSqlDatabase::password | QSqlDatabase::password() | QSqlDatabase::setPassword() |
| QSqlDatabase::port | QSqlDatabase::port() | QSqlDatabase::setPort() |
| QSqlDatabase::userName | QSqlDatabase::userName() | QSqlDatabase::setUserName() |
Some properties have been removed from Qt 4, but the associated access functions are provided if QT3_SUPPORT is defined to help porting to Qt 4. When converting Qt 3 .ui files to Qt 4, uic generates calls to the Qt 3 compatibility functions.
The table below lists these properties with the read and write functions that you can use instead. The documentation for the individual functions explains how to replace them with non-compatibility Qt 4 functions.
| Qt 3 property | Qt 4 read function (QT3_SUPPORT) | Qt 4 write function (QT3_SUPPORT) |
|---|---|---|
| QMenuBar::separator | QMenuBar::separator() | QMenuBar::setSeparator() |
| QPushButton::menuButton | QPushButton::isMenuButton() | N/A |
| QTabWidget::margin | QTabWidget::margin() | QTabWidget::setMargin() |
| QWidget::backgroundBrush | QWidget::backgroundBrush() | N/A |
| QWidget::backgroundMode | QWidget::backgroundMode() | QWidget::setBackgroundMode() |
| QWidget::backgroundOrigin | QWidget::backgroundOrigin() | QWidget::setBackgroundOrigin() |
| QWidget::colorGroup | QWidget::colorGroup() | QWidget::setColorGroup() |
| QWidget::customWhatsThis | QWidget::customWhatsThis() | QWidget::setCustomWhatsThis() |
| QWidget::inputMethodEnabled | QWidget::inputMethodEnabled() | QWidget::setInputMethodEnabled() |
| QWidget::ownCursor | QWidget::ownCursor() | N/A |
| QWidget::ownFont | QWidget::ownFont() | N/A |
| QWidget::ownPalette | QWidget::ownPalette() | N/A |
| QWidget::paletteBackgroundColor | QWidget::paletteBackgroundColor() | QWidget::setPaletteBackgroundColor() |
| QWidget::paletteBackgroundPixmap | QWidget::paletteBackgroundPixmap() | QWidget::setPaletteBackgroundPixmap() |
| QWidget::paletteForegroundColor | QWidget::paletteForegroundColor() | QWidget::setPaletteForegroundColor() |
| QWidget::underMouse | QWidget::underMouse() | N/A |
The following Qt 3 properties and their access functions are no longer available in Qt 4. In most cases, Qt 4 provides similar functionality.
| Qt 3 property | Qt 4 equivalent |
|---|---|
| QButton::autoRepeat | N/A |
| QButton::autoResize | Call QWidget:setFixedSize(QWidget::sizeHint()) whenever you change the contents. |
| QButton::exclusiveToggle | See QAbstractButton::autoExclusive. |
| QButton::pixmap | Use QAbstractButton::icon instead. |
| QButton::toggleState | Use QCheckBox::setState() and QCheckBox::state() instead. |
| QButton::toggleType | Use QCheckBox::setTristate() instead. |
| QComboBox::autoResize | Call QWidget:setFixedSize(QWidget::sizeHint()) whenever you change the contents. |
| QFrame::contentsRect | Use Q3Frame::contentsRect() instead. |
| QFrame::margin | Use QWidget::setContentsMargins() instead. |
| QTabBar::keyboardFocusTab | N/A |
| QToolButton::offIconSet | Use the off component of QAbstractButton::icon instead. |
| QToolButton::onIconSet | Use the on component of QAbstractButton::icon instead. |
| QWidget::microFocusHint | N/A |
Qt 4 is the first version of Qt that contains no explicitly shared classes. All classes that were explicitly shared in Qt 3 are implicitly shared in Qt 4:
This means that if you took a copy of an instance of the class (using operator=() or the class's copy constructor), any modification to the copy would affect the original and vice versa. Needless to say, this behavior is rarely desirable.
Fortunately, nearly all Qt 3 applications don't rely on explicit sharing. When porting, you typically only need to remove calls to detach() and/or copy(), which aren't necessary anymore.
If you deliberately rely on explicit sharing in your application, you can use pointers or references to achieve the same result in Qt 4.
For example, if you have code like
void asciify(QByteArray array)
{
for (int i = 0; i < (int)array.size(); ++i) {
if ((uchar)array[i] >= 128)
array[i] = '?';
}
}
you can rewrite it as
void asciify(QByteArray &array)
{
for (int i = 0; i < array.size(); ++i) {
if ((uchar)array[i] >= 128)
array[i] = '?';
}
}
(Notice the & in the parameter declaration.)
The QAccel class has been renamed Q3Accel and moved to the Qt3Support module. In new applications, you have three options:
The Q3Accel class also supports multiple accelerators using the same object, by calling Q3Accel::insertItem() multiple times. In Qt 4, the solution is to create multiple QShortcut objects.
The QAccessibleInterface class has undergone some API changes in Qt 4, to make it more consistent with the rest of the Qt API.
If you have classes that inherit QAccessibleInterface or one of its subclasses (QAccessibleObject, QAccessibleWidget, etc.), you must port them the new QAccessibleInterface API.
See Virtual Functions for a list of QAccessibleInterface virtual member functions in Qt 3 that are no longer virtual in Qt 4.
The QAccessibleTitleBar has been renamed Q3AccessibleTitleBar and moved to the Qt3Support library.
The QAction class has been redesigned in Qt 4 to integrate better with the rest of the menu system. It unifies the old QMenuItem class and the old QAction class into one class, avoiding unnecessary data duplication and the need to learn two different APIs.
The old QAction and QActionGroup classes have been renamed Q3Action and Q3ActionGroup and moved to Qt3Support. In addition, the new QAction class has compatibility functions to ease transition to Qt 4.
See Virtual Functions for a list of QAction virtual member functions in Qt 3 that are no longer virtual in Qt 4.
The QAction class has been completely redesigned in Qt 4 to integrate better with the rest of the menu system. See the section on QAction for details.
The QApplication class has been split into two classes: QCoreApplication and QApplication. The new QApplication class inherits QCoreApplication and adds GUI-related functionality. In practice, this has no consequences for existing Qt applications.
In addition, the following API changes were made:
Also, QWidgetList has changed from being a typedef for QPtrList<QWidget> to being a typedef for QList<QWidget *>. See the section on QWidgetList below for details.
For example, if you have code like
QWidgetList *list = QApplication::topLevelWidgets();
QWidgetListIt it(*list);
QWidget *widget;
while ((widget = it.current())) {
if (widget->inherits("MainWindow"))
((MainWindow *)widget)->updateRecentFileItems();
++it;
}
delete list;
you can rewrite it as
QWidgetList list = QApplication::topLevelWidgets();
for (int i = 0; i < list.size(); ++i) {
if (MainWindow *mainWin = qobject_cast<MainWindow>(list.at(i)))
mainWin->updateRecentFileItems();
}
The QAquaStyle class first appeared in Qt 3.0, when the Qt/Mac port was first released. It emulated Apple's "Aqua" theme. In Qt 3.1, QAquaStyle was obsoleted by QMacStyle, which uses Appearance Manager to perform its drawing.
The QAquaStyle class is no longer provided in Qt 4. Use QMacStyle instead.
QAsciiCache<T> has been renamed Q3AsciiCache<T> and moved to the Qt3Support library. It has been replaced by QCache<QByteArray, T>.
For details, read the section on QCache<T>, mentally substituting QByteArray for QString.
QAsciiDict<T> and QAsciiDictIterator<T> have been renamed Q3AsciiDict<T> and Q3AsciiDictIterator<T> and moved to the Qt3Support library. They have been replaced by the more modern QHash<Key, T> and QMultiHash<Key, T> classes and their associated iterator classes.
When porting old code that uses Q3AsciiDict<T> to Qt 4, there are four classes that you can use:
For details, read the section on QDict<T>, mentally substituting QByteArray for QString.
The QAsyncIO class was used internally in Qt 2.x in conjunction with QImageConsumer. It was obsoleted in Qt 3.0. If you use this mechanism in your application, please submit a report to the Task Tracker on the Trolltech website and we will try to find a satisfactory substitute.
The undocumented QBackInsertIterator class has been removed from the Qt library. If you need it in your application, feel free to copy the source code from the Qt 3 <qtl.h> header file.
In Qt 3, QBitArray inherited from QByteArray. In Qt 4, QBitArray is a totally independent class. This makes very little difference to the user, except that the new QBitArray doesn't provide any of QByteArray's byte-based API anymore. These calls will result in a compile-time error, except calls to QBitArray::truncate(), whose parameter was a number of bytes in Qt 3 and a number of bits in Qt 4.
QBitArray was an explicitly shared class in Qt 3. See Explicit Sharing for more information.
The QBitVal class has been renamed QBitRef.
The QButton class has been replaced by QAbstractButton in Qt 4. Classes like QPushButton and QRadioButton inherit from QAbstractButton. As a help when porting older Qt applications, the Qt3Support library contains a Q3Button class implemented in terms of the new QAbstractButton.
If you used the QButton class as a base class for your own button type and want to port your code to the newer QAbstractButton, you need to be aware that QAbstractButton has no equivalent for the Q3Button::drawButton(QPainter *) virtual function. The solution is to reimplement QWidget::paintEvent() in your QAbstractButton subclass as follows:
void MyButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
drawButton(&painter);
}
| Q3Button function | QAbstractButton equivalent |
|---|---|
| Q3Button::autoResize() | Call QWidget:setFixedSize(QWidget::sizeHint()) whenever you change the contents. |
| Q3Button::isExclusiveToggle() | Use QAbstractButton::group() or QAbstractButton::autoExclusive() instead. |
| Q3Button::pixmap() const | QAbstractButton::icon() |
| Q3Button::setAutoResize() | N/A |
| Q3Button::setPixmap(const QPixmap &) | QAbstractButton::setIcon(const QIcon &) |
| Q3Button::setState(ToggleState) | See remark below |
| Q3Button::setToggleType(ToggleType) | See remark below |
| Q3Button::state() | See remark below |
| Q3Button::stateChanged(int) | See remark below |
| Q3Button::toggleType() | See remark below |
Remarks:
See Virtual Functions for a list of QButton virtual member functions in Qt 3 that aren't virtual in Qt 4.
See Properties for a list of QButton properties in Qt 3 that have changed in Qt 4.
The QButtonGroup class has been completely redesigned in Qt 4. For compatibility, the old QButtonGroup class has been renamed Q3ButtonGroup and has been moved to Qt3Support. Likewise, the QHButtonGroup and QVButtonGroup convenience subclasses have been renamed Q3HButtonGroup and Q3VButtonGroup and moved to the Qt3Support library.
The old QButtonGroup, as well as Q3ButtonGroup, can be used in two ways:
Unlike Q3ButtonGroup, the new QButtonGroup doesn't inherit QWidget. It is very similar to a "hidden Q3ButtonGroup".
If you use a Q3ButtonGroup, Q3HButtonGroup, or Q3VButtonGroup as a widget and want to port to Qt 4, you can replace it with QGroupBox. In Qt 4, radio buttons with the same parent are automatically part of an exclusive group, so you normally don't need to do anything else. See also the section on QGroupBox below.
See Virtual Functions for a list of QButtonGroup virtual member functions in Qt 3 that are no longer virtual in Qt 4.
In Qt 3, QByteArray was simply a typedef for QMemArray<char>. In Qt 4, QByteArray is a class in its own right, with a higher-level API in the style of QString.
Here are the main issues to be aware of when porting to Qt 4:
For example, if you have code like
QByteArray ba(64);
you can rewrite it as
QByteArray ba(64, '\0');
ba.at(0) = 'X';
will no longer compile. Instead, use QByteArray::operator[]:
ba[0] = 'X';
QByteArray was an explicitly shared class in Qt 3. See Explicit Sharing for more information.
QCache<T> has been renamed Q3Cache<T> and moved to Qt3Support. The new QCache class has a different API, and takes different template parameters: QCache<Key, T>.
When porting to Qt 4, QCache<QString, T> is the obvious substitute for Q3Cache<T>. The following table summarizes the API differences.
| Q3Cache<T> function | QCache<QString, T> equivalent |
|---|---|
| Q3Cache::Q3Cache(int maxCost, int size, bool caseSensitive) | See remark below |
| Q3Cache::autoDelete() | N/A |
| Q3Cache::count() | QCache::count() or QCache::size() (equivalent) |
| Q3Cache::setAutoDelete() | See remark below |
| Q3Cache::size() | N/A |
| Q3Cache::statistics() | N/A |
| Q3Cache::operator=() | See remark below |
Remarks:
if (!cache.insert(key, object))
delete object;
becomes
cache.insert(key, object);
Q3Cache<QWidget> cache;
cache.insert(widget->name(), widget);
...
QWidget *foo = cache.take("foo");
if (foo)
foo->show();
becomes
typedef QWidget *QWidgetPtr;
QCache<QString, QWidgetPtr> cache;
cache.insert(widget->name(), new QWidgetPtr(widget));
...
QWidgetPtr *ptr = cache.take("foo");
if (ptr) {
QWidget *foo = *ptr;
delete ptr;
foo->show();
}
An alternative is to stick to using Q3Cache.
QCacheIterator<T> has been renamed Q3CacheIterator<T> and moved to the Qt3Support library. The new QCache class doesn't offer any iterator types.
The canvas module classes have been renamed and moved to the Qt3Support library.
| Qt 3 class name | Compatibility class in Qt 4 |
|---|---|
| QCanvas | Q3Canvas |
| QCanvasEllipse | Q3CanvasEllipse |
| QCanvasItem | Q3CanvasItem |
| QCanvasItemList | Q3CanvasItemList |
| QCanvasLine | Q3CanvasLine |
| QCanvasPixmap | Q3CanvasPixmap |
| QCanvasPixmapArray | Q3CanvasPixmapArray |
| QCanvasPolygon | Q3CanvasPolygon |
| QCanvasPolygonalItem | Q3CanvasPolygonalItem |
| QCanvasRectangle | Q3CanvasRectangle |
| QCanvasSpline | Q3CanvasSpline |
| QCanvasSprite | Q3CanvasSprite |
| QCanvasText | Q3CanvasText |
| QCanvasView | Q3CanvasView |
Qt 4.1 is expected to provide a replacement module for these classes, based on Qt 4's powerful new 2D paint system.
In Qt 4, QColor is a value type like QPoint or QRect. Graphics system-specific code has been implemented in QColorMap.
The numBitPlanes() function has been replaced by QColorMap::depth().
In Qt 3, a QPalette consisted of three QColorGroup objects. In Qt 4, the (rarely used) QColorGroup abstraction has been eliminated. For source compatibility, a QColorGroup class is available when QT3_SUPPORT is defined.
The new QPalette still works in terms of color groups, specified through enum values (QPalette::Active, QPalette::Disabled, and QPalette::Inactive). It also has the concept of a current color group, which you can set using QPalette::setCurrentColorGroup().
The QPalette object returned by QWidget::palette() returns a QPalette initialized with the correct current color group for the widget. This means that if you had code like
painter.setBrush(colorGroup().brush(QColorGroup::Background));
you can simply replace colorGroup() with palette():
painter.setBrush(palette().brush(QPalette::Background));
The QColorDrag class has been renamed Q3ColorDrag and moved to the Qt3Support library. In Qt 4, use QMimeData instead and call QMimeData::setColor() to set the color.
In Qt 3, the list box used to display the contents of a QComboBox widget could be accessed by using the listBox() function. In Qt 4, the standard list box is provided by a QListView widget, and can be accessed with the view() function.
See Virtual Functions for a list of QComboBox virtual member functions in Qt 3 that are no longer virtual in Qt 4.
In Qt 3, QCString inherited from QByteArray. The main drawback of this approach is that the user had the responsibility of ensuring that the string is '\0'-terminated. Another important issue was that conversions between QCString and QByteArray often gave confusing results. (See the Achtung! Binary and Character Data article in Qt Quarterly for an overview of the pitfalls.)
Qt 4 solves that problem by merging the QByteArray and QCString classes into one class called QByteArray. Most functions that were in QCString previously have been moved to QByteArray. The '\0' issue is handled by having QByteArray allocate one extra byte that it always sets to '\0'. For example:
QByteArray ba("Hello");
ba.size(); // returns 5 (the '\0' is not counted)
ba.length(); // returns 5
ba.data()[5]; // returns '\0'
The Qt3Support library contains a class called Q3CString that inherits from the new QByteArray class and that extends it to provide an API that is as close to the old QCString class as possible. Note that the following functions aren't provided by Q3CString:
The following functions have lost their last parameter, which specified whether the search was case sensitive or not:
In both cases, the solution is to convert the QCString to a QString and use the corresponding QString functions instead.
Also be aware that QCString::size() (inherited from QByteArray) used to return the size of the character data including the '\0'-terminator, whereas the new QByteArray::size() is just a synonym for QByteArray::length(). This brings QByteArray in line with QString.
When porting to Qt 4, occurrences of QCString should be replaced with QByteArray or QString. The following table summarizes the API differences between the Q3CString class and the Qt 4 QByteArray and QString classes:
| Q3CString function | Qt 4 equivalent |
|---|---|
| Q3CString::Q3CString(const char *, uint) | See remark below |
| Q3CString::Q3CString(int) | QByteArray::QByteArray(int, char) |
| Q3CString::leftJustify() | QString::leftJustified() |
| Q3CString::length() | QByteArray::length() or QByteArray::size() (equivalent) |
| Q3CString::lower() | QByteArray::toLower() |
| Q3CString::rightJustify() | QString::rightJustified() |
| Q3CString::setExpand() | See remark below |
| Q3CString::simplifyWhiteSpace() | QByteArray::simplified() |
| Q3CString::sprintf() | QString::sprintf() |
| Q3CString::stripWhiteSpace() | QByteArray::trimmed() |
| Q3CString::toDouble() | QString::toDouble() |
| Q3CString::toFloat() | QString::toFloat() |
| Q3CString::toInt() | QString::toInt() |
| Q3CString::toLong() | QString::toLong() |
| Q3CString::toShort() | QString::toShort() |
| Q3CString::toUInt() | QString::toUInt() |
| Q3CString::toULong() | QString::toULong() |
| Q3CString::toUShort() | QString::toUShort() |
| Q3CString::upper() | QByteArray::toUpper() |
Remarks:
For example, if you have code like
QCString str1("Hello", 4); // "Hel"
QCString str2("Hello world!", n);
you can rewrite it as
QByteArray str1("Hello", 3);
QByteArray str2("Hello world!");
str2.truncate(n - 1);
For example, if you have code like
QCString str("Hello world");
str.setExpand(16, '\n'); // "Hello world \n"
you can rewrite it as
QByteArray str("Hello world");
while (str.size() < 16)
str += ' ';
str += '\n';
Since the old QCString class inherited from QByteArray, everything that is said in the QByteArray section applies for QCString as well.
The QDataBrowser class has been renamed Q3DataBrowser and moved to the Qt3Support library. It is expected that Qt 4.1 will offer a replacement class. In the meantime, you can use Q3DataBrowser for creating data-aware forms or you can roll your own.
See QtSql Module for an overview of the new SQL classes.
The QDataPump class was used internally in Qt 2.x in conjunction with QImageConsumer. It was obsoleted in Qt 3.0.
If you use this mechanism in your application, please submit a
report to the \l{Task Tracker} on the Trolltech
website and we will try to find a satisfactory substitute.
The QDataSink class was used internally in Qt 2.x in conjunction with QImageConsumer. It was obsoleted in Qt 3.0.
If you use this mechanism in your application, please submit a
report to the \l{Task Tracker} on the Trolltech
website and we will try to find a satisfactory substitute.
The QDataSource class was used internally in Qt 2.x in conjunction with QImageConsumer. It was obsoleted in Qt 3.0.
If you use this mechanism in your application, please submit a
report to the \l{Task Tracker} on the Trolltech
website and we will try to find a satisfactory substitute.
The QDataTable class has been renamed Q3DataTable and moved to the Qt3Support library. It is expected that Qt 4.1 will offer a replacement class. In the meantime, you can use Q3DataTable for creating data-aware forms or you can roll your own.
See QtSql Module for an overview of the new SQL classes.
The QDataView class has been renamed Q3DataView and moved to the Qt3Support library. It is expected that Qt 4.1 will offer a replacement class. In the meantime, you can use Q3DataTable for creating data-aware forms or you can roll your own.
See QtSql Module for an overview of the new SQL classes.
The QDateEdit class in Qt 4 is a convenience class based on QDateTimeEdit. The old class has been renamed Q3DateEdit and moved to the Qt3Support library.
See Virtual Functions for a list of QDateEdit virtual member functions in Qt 3 that are no longer virtual in Qt 4.
The QDateTimeEditBase class has been renamed Q3DateTimeEditBase and moved to Qt3Support. Use QDateTimeEdit or QAbstractSpinBox instead.
The old QDateTimeEdit class has been renamed Q3DateTimeEditBase and moved to Qt3Support. The new QDateTimeEdit in Qt 4 has been rewritten from scratch to provide a more flexible and powerful API.
See Virtual Functions for a list of QDateTimeEdit virtual member functions in Qt 3 that are no longer virtual in Qt 4.
The QDeepCopy<T> class in Qt 3 provided a means of ensuring that implicitly shared and explicitly shared classes referenced unique data. This was necessary because the reference counting in Qt's container classes was done in a thread-unsafe manner.
With Qt 4, QDeepCopy<T> has been renamed Q3DeepCopy<T> and moved to the Qt3Support library. Removing it from existing code is straightforward.
For example, if you have code like
QString str1 = "I am a string";
QDeepCopy<QString> str2 = str1;
QString str3 = QDeepCopy<QString>(str2);
you can rewrite it as
QString str1 = "I am a string";
QString str2 = str1;
QString str3 = str2;
See Virtual Functions for a list of QComboBox virtual member functions in Qt 3 that are no longer virtual in Qt 4.
See Properties for a list of QDial properties in Qt 3 that have changed in Qt 4.
QDict<T> has been renamed Q3Dict<T> and moved to Qt3Support. It has been replaced by the more modern QHash<Key, T> and QMultiHash<Key, T> classes.
When porting old code that uses QDict<T> to Qt 4, there are four classes that you can use:
| Qt 4 class | When to use it |
|---|---|
| QMultiHash<QString, T *> | Since Q3Dict<T> is pointer-based and allows duplicate keys, this is usually the most straightforward conversion. |
| QMultiHash<QString, T> | If type T is an assignable data type, you can use T as the value type rather than T *. This often leads to nicer code. |
| QHash<QString, T *> | If you don't use duplicate keys, you can use QHash instead of QMultiHash. QMultiHash inherits from QHash. |
| QHash<QString, T> |
The APIs of Q3Dict<T> and QMultiHash<QString, T *> are quite similar. The main issue is that Q3Dict supports auto-delete whereas QMultiHash doesn't.
The following table summarizes the API differences between the two classes:
| Q3Dict function | QMultiHash equivalent |
|---|---|
| Q3Dict::Q3Dict(int size, bool caseSensitive) | See remarks below |
| Q3Dict::autoDelete() | N/A |
| Q3Dict::count() | QMultiHash::count() or QMultiHash::size() (equivalent) |
| Q3Dict::find(const QString &) | QMultiHash::value(const QString &) |
| Q3Dict::remove(const QString &) | QMultiHash::take(const QString &) |
| Q3Dict::resize(uint) | QMultiHash::reserve(int) |
| Q3Dict::setAutoDelete() | See discussion below |
| Q3Dict::size() | QMultiHash::capacity() |
| Q3Dict::statistics() | N/A |
| Q3Dict::operator[](const QString &) | See remark below |
Remarks:
If you use Q3Dict's auto-delete feature (by calling Q3Dict::setAutoDelete(true)), you need to do some more work. You have two options: Either you call delete yourself whenever you remove an item from the container, or you use QMultiHash<QString, T> instead of QMultiHash<QString, T *> (i.e. store values directly instead of pointers to values). Here, we'll see when to call delete.
The following table summarizes the idioms that you need to watch out for if you want to call delete yourself.
| Q3Dict idiom | QMultiHash idiom |
|---|---|
dict.replace(key, value); | delete hash.take(key);
hash.insert(key, value);
|
dict.remove(key, value); | delete hash.take(key); |
dict.clear(); (also called from Q3Dict's destructor) | while (!hash.isEmpty()) {
T *value = *hash.begin();
dict.erase(hash.begin());
delete value;
}
In 99% of cases, the following idiom also works: qDeleteAll(hash);
hash.clear();
However, it may lead to crashes if hash is referenced from the value type's destructor, because hash contains dangling pointers until clear() is called. |
Be aware that Q3Dict's destructor automatically calls clear(). If you have a Q3Dict data member in a custom class and use the auto-delete feature, you will need to call delete on all the items in the container from your class destructor to avoid a memory leak.
Finally, QDictIterator<T> (renamed Q3DictIterator<T>) must also be ported. There are no fewer than four iterator classes that can be used as a replacement: QHash::const_iterator, QHash::iterator, QHashIterator, and QMutableHashIterator. The most straightforward class to use when porting is QHashIterator<QString, T *>. The following table summarizes the API differences:
| Q3DictIterator functions | Qt 4 equivalent |
|---|---|
| Q3DictIterator::count() | QHash::count() or QHash::size() |
| Q3DictIterator::current() | QHashIterator::value() |
| Q3DictIterator::currentKey() | QHashIterator::key() |
| Q3DictIterator::isEmpty() | QHash::isEmpty() |
| Q3DictIterator::toFirst() | QHashIterator::toFront() |
| Q3DictIterator::operator()() | QHashIterator::value() |
| Q3DictIterator::operator*() | QHashIterator::value() |
| Q3DictIterator::operator++() | See remark below |
Be aware that QHashIterator has a different way of iterating than Q3DictIterator. A typical loop with Q3DictIterator looks like this:
Q3DictIterator<QWidget> i(dict);
while (i.current() != 0) {
do_something(i.currentKey(), i.current());
++i;
}
Here's the equivalent QHashIterator loop:
QHashIterator<QString, QWidget *> i(hash);
while (i.hasNext()) {
i.next(); // must come first
do_something(i.key(), i.value());
}
See Java-style iterators for details.
The following functions used to have a boolean acceptAbsPath parameter that defaulted to true:
In Qt 3, if acceptAbsPath is true, a file name starting with '/' is be returned without change; if acceptAbsPath is false, an absolute path is prepended to the file name. For example:
| Current directory | File name | acceptAbsPath | File path |
|---|---|---|---|
| /home/tsmith | index.html | true | /home/tsmith/index.html |
| false | /home/tsmith/index.html | ||
| /home/tsmith | /index.html | true | /index.html |
| false | /home/tsmith/index.html |
In Qt 4, this parameter is no longer available. If you use it in your code, you can check that QDir::isRelativePath() returns false instead.
For example, if you have code like
QDir dir("/home/tsmith");
QString path = dir.filePath(fileName, false);
you can rewrite it as
QDir dir("/home/tsmith");
QString path;
if (dir.isRelativePath(fileName))
path = dir.filePath(fileName);
else
path = fileName;
QDir::encodedEntryList() has been removed.
fileInfoList(), entryInfoList(), and drives() now return a QList<QFileInfo> and not a QPtrList<QFileInfo> *. Code using these methods will not work with the Qt3Support library and must be adapted instead.
See Virtual Functions for a list of QDir virtual member functions in Qt 3 that are no longer virtual in Qt 4.
QDir::match() now always matches case insensitively.
QDir::homeDirPath() has been removed. Use QDir::home() instead, and extract the path separately.
Qt 3 used its own implementation of the DNS protocol and provided a low-level QDns class. Qt 4's QHostInfo class uses the system's gethostbyname() function from a thread instead.
The old QDns class has been renamed Q3Dns and moved to the Qt3Support library. The new QHostInfo class has a radically different API: It consists mainly of two static functions, one of which is blocking (QHostInfo::fromName()), the other non-blocking (QHostInfo::lookupHost()). See the QHostInfo class documentation for details.
The QDockArea class has been renamed Q3DockArea and moved to the Qt3Support library. In Qt 4, QMainWindow handles the dock and toolbar areas itself. See the QMainWindow documentation for details.
The old QDockWindow class has been renamed Q3DockWindow and moved to the Qt3Support library. In Qt 4, there is a new QDockWidget class with a different API. See the class documentation for details.
See Virtual Functions for a list of QDockWidget virtual member functions in Qt 3 that are no longer virtual in Qt 4.
The QDragObject class has been renamed Q3DragObject and moved to the Qt3Support library. In Qt 4, it has been replaced by the QMimeData class. See the class documentation for details.
The QDropSite class has been renamed Q3DropSite and moved to the Qt3Support library.
The QDropSite class has been obsolete ever since Qt 2.0. The only thing it does is call QWidget::setAcceptDrops(true).
For example, if you have code like
class MyWidget : public QWidget, public QDropSite
{
public:
MyWidget(const QWidget *paren