Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, December 2006
Porting from Qt 2 to 4


Message 1 in thread

Porting from Qt 2 to 4
======================

This is a little guide how to port from Qt2 to 4. In detail it was "Qt 
2.1.0-beta2" to "Qt 4.2.2".

The project I used for this test has about 35 own classes and about 50 
files. Note: The project I used did not used any GUI files like ".ui" 
files from the QDesigner.

Note: It seems to be that Unix/Linux is much more fault tolerance then 
Windows Systems. You can have memory leeks on a Unix/Linux box and it 
works, but not on Windows (Crashes, Bluescreen on Windows < NT/XP).

0. Do backup first. (Source and EXE/DLLs etc.!)
    It is important, so you can look how the "original" should work.
1. Read the "Porting to Qt 4" guide from Trolltech at
    http://doc.trolltech.com/4.2/porting4.html
2. Used "qt3to4.exe" for automatically change some names.
    See generated "portinglog.txt" for details, what is changed.
3. Create a new ".pro" file with "qmake -project".


new.pro:
--------
...
QT += qt3support

TEMPLATE  = app
LANGUAGE  = C++
CONFIG    += console
.......
------

The console in CONFIG is needed for debugging like QDebug and console 
print outs with cout << "Some error" and things like that. Sometimes 
very helpfull.

4. Then, try to compile with
   qmake
   make release    // or whatever you need, also make debug etc.

5. Error. Do some changes.
Maybe you have to change the occurency where you source code appears in 
the .pro file. Idea: compile easy things first, then the difficult 
things. So, move arround your source, that it compiles things you know 
it may work first. -- If something does not work after a minute, take an 
other file.
Take also a look in the old docu from Trolltech. It can be very 
important to understand, how things worked in history.
The oldest is Qt2.3 http://doc.trolltech.com/2.3/index.html
But note: Our very old code uses Qt2.1.0-beta2 from the year 2000(!).


Namespaces
==========
Maybe you have to use
   std::cout << "myFunc(): Start..." << std::endl;
indead of:
   cout << "myFunc(): Start..." << endl;
Or
   using namespace std;
at the beginning of the file.


Old:     AlignCenter,     Key_Escape, etc.
New: Qt::AlignCenter, Qt::Key_Escape, etc.


QMouseEvent* event;
   Old:  if(event->button()==Qt::LeftButton) {
   New:  if(event->button()==QMouseEvent::LeftButton) {



In myglobal.h or things like that:
----------------------------------

Old:
* error: `cout' undeclared (first use this function)
* ... warning: #warning This file includes at least one deprecated or 
antiquated header. Please consider using one of the 32 headers found in 
section 17.4.1.2 of the C++ standard. Examples include substituting the 
<X> header for the <X.h> header for C++ includes, or <iostream> instead 
of the deprecated header <iostream.h>. To disable this warning use 
-Wno-deprecated.

New:
   #ifdef I_USE_WINDOWS
     #include <ostream>  // OLD: #include <ostream.h>
     #include <istream>  // OLD: #include <istream.h>
     #include <iostream> // error: `cout' undeclared (first use this 
function)
using namespace std;  // error: `cout' undeclared (first use this function)
   #endif




Strange Errors
==============

error: ISO C++ forbids declaration of `Q3PtrList' with no typ
error: expected `;' before '<' token

   Q3PtrList<my> myList;

This strange compiler (g++) message means: "my> myList" is not known.

   Do a #include <Q3PtrList>.



Old:
   QApsSettings() { myList.setAutoDelete(true); }

   Qt 2.3:
     mylist.setAutoDelete( TRUE );
     // delete items when they are removed
     // See: http://doc.trolltech.com/2.3/qlist.html

New:
   QApsSettings() { }  // setAutoDelete Not needed.




QList
=====
Change every QList to Q3PtrList. This is not done by the 
converter(qt3to4.exe) from Trolltech.

   QList -> Q3PtrList

Old:
   QList<my> myList;
New:
   Q3PtrList<my> myList;


NOTE:
   declare:
      Q3PtrList<my> myList;  // [Qt2] QList<my> myList;
   then comment:
      // myList.setAutoDelete( true );  // [Qt4] not used anymore




QRegExp
=======
Maybe you have to include
   #include <QRegExp>


QString string;
QRegExp regSection("\\[[^]]*\\]");  // match a "[section]"

Old Qt2:
   i=regSection.match(string, i, &el);
      (See http://doc.trolltech.com/3.0/qregexp.html#match)
New Qt4:
   i=regSection.search(string, i);

Old Qt2:
   my1 = string.mid(i+1, el-2);
   i+=el;
New Qt4:
   my1 = string.mid(i+1, regSection.matchedLength()-2 );

Note you can use matchedLength() instead of the &el things.

Maybe print out some values to the console for checking/debugging with:
   #include <QDebug>
   cout << "createGUI(): start..." << endl;
   ...
   qDebug("GUI. created.");




QString
=======

error: invalid conversion from `QChar*' to `char' (g++)
-------------------------------------------------

Old Qt2:
   QString str1 = "";
   QString str2 = "";
   str1 = str2.data();

New Qt4:
   QString str1 = "";
   QString str2 = "";
   str1 = str2;


QLabel
======
Add namespace "QTt::"
->setAlignment(Qt::AlignHCenter|Qt::AlignTop); // Qt2.3: AlignHCenter -


QSplit
======
Seems to work differently.
Just try other things, look in examples, etc.

7. After a while of debugging and changeing source code, you should 
recompile everything.
Do:
   make clean
   make release   // or make debug

But note: Maybe you have to change things in the Makefile.release 
directly, so, do a backup before you need to recompile everything.

8. Still some minor problems exist. Some new Q-Things beheave different.

Porting from 2 to 4 is possible. Check also archive of qt-interest.

Hope this helps.

-- 
 [ signature omitted ]