Tutorial :Qt UI for existing C++ project


I have already written a C++ program and I would like to write a GUI for it. I realize Qt is a wonderful tool, however, Qt has it's own classes, which make me quite confused. eg: instead of String, Qt has a class named QString..

I am wondering if I can mix C++ code and Qt code in C++?


Yes you can intermix Qt and STL very easily.

The GUI takes QStrings but will silently create these form std::string or char*, QStrings returned from Qt can be converted with toStdString() or toAscii().

Qt includes a set of collection classes but you don't have to use them.

Qt does a good job of looking like modern C++, there are a few 'extras' to handle the signal and slot event mechanism but these are more a complication to the build environment than to the code.

edit:thanks bill


I wont recommend mixing std c++ code and Qt code together.

Let's say, your c++ code is the logic then the Qt code , should be just for GUI. Using a MVC/MVP patterns or likewise, separate your logic and UI.

Thus,it would be easier in the future to be able to run your code with/without using a UI.


in short, yes you can! for example, a QString is essentially a string and has a toAscii() method, so you can easily use Qt for your projects. Although you MIGHT run into some problems integrating Qt into visual studio (assuming you are using it), you will be able to mix c++ code and Qt code(which is in c++)


yeah! Qt is something you can use in your C++ code. Qt is mainly for GUI but it is good classes for Socket, Containers, Network, ... You are free to use STL and other normal classes or the ones Qt offers you. The biggest point is that if you use Qt in your code before the main Compile you should do a meta compile to make it ready for you compiler. One thing you should notice is that Qt is not standard it means although it supports Windows, Linux and Mac OS, it won't be usable on other platforms.


They can be used together.

C++ has a long history and the STL implementation and support have evolved with the language. I think that the ex-Trolltech team decided that they didn't have enough control over the quality and availability of STL implementations to make the Qt library depend on it. The result is that Qt has its own QTL, QtAlgorithms header and QString.

They have taken care to make them compatible with the STL:

  • most containers have a similar interface to the STL ones (e.g: iterator access, push_back(), empty(), etc)
  • Qt containers can be converted to/from STL containers (e.g: QVector::toStdVector, QList::fromStdList)
  • Qt containers can be used with STL algorithms
  • QString can be converted to/from std::string and std::wstring with toStd(W)String / fromStd(W)String

Personally, I like QString more than std::string, it is easier to use and more powerful.


Yes you can.

I actually made a Qt-GUI-wrapper for a set of C based functions. No problems with the Qt integration, only the low level C (which leaks memory...).

See here: http://code.google.com/p/qhocr/

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »