Home · All Classes · Annotated · Functions

Porting - Qtopia 2 to Qtopia 4

Overview

Porting an application from Qtopia 2 to Qtopia 4 involves changing the application code corresponding to how the API for Qtopia has changed.

Primarily, porting changes will relate to the changes in the Qt API. Qtopia does not support the Qt 3 Support module, so the task of porting from Qtopia 2 to Qtopia 4 takes more work than porting from Qt 2 to Qt 4, however the key documents remain:

In addition to these documents, the sections below provide some additional tips.

Basic Conversions

If a previously used Qt function is not available and is not listed below or in the above documents, check if it is declared in the relevant header file, but within #ifdef QT3_SUPPORT bracketing.

If it is, check the in-line code for the function for the corresponding Qt 4 code.

For example:

QComboBox::setCurrentItem(int) is in Qt3_SUPPORT, but it is an in-line function that simply calls the renamed function, QComboBox::setCurrentIndex(int).

The table below lists the function conversions for porting to Qtopia 4.

Qtopia 2Qtopia 4
AppLnkQContent and QContentSet
ContextBarQSoftMenuBar
ContextMenuQMenu and QSoftMenuBar::addMenuTo()
DocLnkQContent and QContentSet
FileSelectorQDocumentSelector
Global::decodeBase64(bytearray)QByteArray::fromBase64()
Global::encodeBase64(bytearray)QByteArray::toBase64()
ImageSelectorQImageDocumentSelector
QArrayQList
QCStringQString or QByteArray.

Note: QCopChannel connections use QString

QColorGroup::base()QPalette::color(QPalette::Base)
QDataStream::ds(bytearray, QIODevice::ReadOnly)QDataStream::ds(bytearray) - it is always read only
QDialog::dlg(parent,name,modal)QDialog::dlg(parent,name); QDialog::dlg.setModal(TRUE)
QDir::cleanDirPath()QDir::cleanPath()
QFileInfo::dirPath()QFileInfo::absolutePath()
QFileInfo::extension()QFileInfo::completeSuffix()
QIconViewQListView.
QList::remove()QList::removeAll()
QList<T>QList<T*>
QListBoxQListView
QListIterator<T>QList<T>::iterator; also consider using foreach
QListViewQTreeView if hierarchical, otherwise QListView
QPEToolBarQToolBar
QScrollViewQScrollArea
QValueListQList
Qtopia::escapeMultiLineStringQt::convertFromPlainText()
Qtopia::escapeStringQt::escape()
StorageInfoQFileSystem and QStorageMetaInfo
Sound::soundAlarm()Qtopia::soundAlarm()

Designer and UIC

UIC can convert Qt 2/3 UI files to Qt 4, however, it generates code that uses Qt 3 support classes if the .ui file specifies either a class or a property on a class which is in the Qt 3 Support module.

For example, if QButtonGroup is used, it will generally convert trivially to QButtonGroup which is also in Qt 4, however, QButtonGroup in Qt 3 inherited QGroupBox which in turn inherited QFrame. So if the .ui file specifies QFrame properties on a QButtonGroup, then Qt 3 Support code will be generated.

To port .ui files:

Note: converted .ui files contain an objectName property that matches the widget's name. While there may be a few cases where an objectName should be set, most cases are redundant and just take up space. You may choose to remove this property (in Designer or by editing the .ui file).

QProcess

QProcess is now subclassed from QIODevice, so instead of readStderr() and read|writeStdin() you access output as follows:

    myproc.setInputChannel( QProcess::StandardOutput );
    QTextStream procstream( myproc );
    procStream >> theResults;  // get into a string
    myproc.setInputChannel( QProcess::StandardError );
    procStream >> someError;

There is also readAllStandardError(), and to write to stdin, there are the QIODevice methods, such as myproc.write(<some text>).

The signal wroteToStdin() should be replaced by usage of QIODevice::bytesWritten(int).

QMenu and QActionGroup

Mutually exclusive checkable menu items are implemented with QActionGroup.

Note: addAction() is now a method of QWidget:

     QMenu* menu = new QMenu;
     menu->setCheckable( TRUE );
     QActionGroup *grp = new QActionGroup;
     for (int i = 0; i <= myLastItem; ++i )
     {
         items[i] = new QAction( QString( "option %1" ).arg( i ), grp );
         items[i]->setCheckable( TRUE );
     }
     items[myDefault]->setChecked( TRUE );
     menu->addActions( grp->actions() );

QAction for Regular Menu Items

In the past, you may have used something like the following to create regular menu items:

    a_settings = new QAction( tr("Settings..."),
                              theicon,
                              QString::null, 0, this, 0 );
    connect( a_settings, SIGNAL( activated() ),
             this, SLOT( doSettings() ) );
    a_settings->addTo( menu );

The new sequence looks like this:

    a_settings = new QAction( theicon,
                              tr("Settings..."), this );
    connect( a_settings, SIGNAL( triggered() ),
             this, SLOT( doSettings() ) );
    menu->addAction( a_settings );

Note: the transposition of the first two arguments to the QAction constructor, and the use of the triggered() slot rather than activated().

QImage/QPixmap::smoothScale

Qt 2 code:

        img = img.smoothScale( 5, 5 );

becomes:

        img.scale( 5, 5, Qt::KeepAspectRatio, Qt::SmoothTransformation );

QStyle

In tune with other parts of Qt, query and paint methods are now parameterized by enums instead of having specific names. Search the QStyle documentation for the name you want to query or paint, to find the enum and the method you need.

Qt 2 code:

    QRect r = style().pushButtonContentsRect( this );
    int sx, sy;
    style().getButtonShift(sx, sy);
    int dx = style().menuButtonIndicatorWidth( height() );
    style().drawArrow( p, DownArrow, FALSE, x+w-dx, y+2, dx-4, h-4,
             colorGroup(), isEnabled() );

becomes:

    QStyleOptionButton sob;
    sob.init( this );  // copies this->rect() and this->state, this's StyleFlags
    QRect r = style()->subRect( QStyle::SR_PushButtonContents, &sob );
    int sx = style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal, &sob );
    int sy = style()->pixelMetric( QStyle::PM_ButtonShiftVertical, &sob );
    int dx = style()->pixelMetric( QStyle::PM_MenuButtonIndicator, &sob );
    QStyleOptionButton arrowStyle = sob;
    arrowStyle.rect.setLeft( sob.rect.right() - h) );  // a small square at RHS
    style()->drawPrimitive( QStyle::PE_IndicatorArrowDown, &arrowStyle, p );

QCanvas

There is no QCanvas in Qt 4 (it is in the unavailable Qt 3 Support module), but it is available in Qtopia 4 - instead of including qcanvas.h or Qt/qcanvas.h, use qtopia/canvas/qcanvas.h.

However, note that this class is intended to be obsoleted in Qtopia 4.2 by a new Qt class.

QUuid

QUuid is in Qt 4, so include Qt/quuid.h, not qtopia/quuid.h.

Plug-ins

Qtopia 4 uses the standard Qt 4 mechanisms for plug-ins.

The code below shows the pattern used for Qtopia plug-ins. Replace MyPlugin with the name of the type of plug-in you are writing,such as InputMethod. The pure virtual functions are those functions you want your plug-in to provide and the keys function should return the name of the plug-in implementation. For example, keys() would return Handwriting for the handwriting plug-in, or Handwriting, Fullscreen Handwriting if both plug-ins are provided).

    #include <qplugin.h>
    #include <qfactoryinterface.h>
    #include <qobject.h>

    struct QMyPluginFactoryInterface : public QFactoryInterface
    {
        // pure virtual functions
    };

    Q_DECLARE_INTERFACE(QMyPluginFactoryInterface, "http://trolltech.com/Qtopia/QMyPluginFactoryInterface")

    class QMyPluginPlugin : public QObject, public QMyPluginFactoryInterface
    {
        Q_OBJECT
        Q_INTERFACES(QMyPluginFactoryInterface:QFactoryInterface)
    public:
        QMyPluginPlugin(QObject *parent=0);
        ~QMyPluginPlugin();

        virtual QStringList keys() const = 0;
        // pure virtual functions
    };

Resource

Resources are now found using the Qt 4 QFileEngine mechanism, where in all places where a filename may be used, a special name starting with a colon (":") is used instead to refer to a resource. In Qt this is only used to refer to compiled-in resources, but in Qtopia, they may be actual files in predetermined places in the filesystem, that is, as Resource provided in Qtopia 2.

Qtopia 2Qtopia 4
Resource::loadPixmap("open")QPixmap(":image/open")
Resource::loadIconSet("add")QIcon(":icon/add")
Resource::findPixmap("addressbook/homephone")":image/addressbook/homephone"
Resource::findPixmap("new")":image/new"
<img src="addressbook/home.png"><img src=":icon/addressbook/home">

You can use scripts/porting-resources to automate the changes.

Config

Use QSettings instead.

Where Config files include translations (i.e. keys with []) and you need to read those keys, use QTranslatableSettings instead.

Rather than Config::setGroup(), you should use QSettings::beginGroup()..endGroup() spans.

Note:

You can use scripts/porting-config to automate the changes, however:

Note: clever use of Config::setGroup() can confuse that script.

The settings files are now stored in ~/Settings/Trolltech/ or $QPEDIR/etc/default/Trolltech/.

VScrollView

Use QScrollArea instead.

Keypad mode will work correctly provided you set the focus policy of QScrollArea to Qt::NoFocus.o

For example:

        QScrollArea *sa = new QScrollArea(...);
        sa->setFocusPolicy(Qt::NoFocus);
        QWidget *view = new QWidget;
        sa->setWidget(view);
        sa->setWidgetResizable(true);
        // add children to view.


Copyright © 2006 Trolltech Trademarks
Qtopia 4.1.7