tags:

views:

295

answers:

6

Does it make sense to use Qt for increasing the productivity in an MFC app, without actually using the Qt user interface system?

I am currently looking or a good productivity library for my MFC based application, with useful container classes, string algorithmus, threading classes, I/O classes and so on. The Qt API is very nice in my opinion. However, since I don't want to switch my UI to Qt (just too much effort), I am wondering whether Qt can be used well in a MFC app without any Qt UI.

Thanks in advance for your opinions.

Fabian

A: 

If you only want it for the collection classes why not just use std:: library?

Martin Beckett
Well, I am also looking into things like threading. Qt (and Boost) have useful functionality that std doesn't provide.
Fabian
Qt is much more than std::
Eli Bendersky
The OP originally said they wanted Qt to replace the MFC collection classes
Martin Beckett
+1  A: 

The Mumble project uses Qt for the client and server, with the server not having any UI code at all, still using the rest of the Qt API extensively.

Otto Allmendinger
+6  A: 

Qt is divided into several modules (QtGui being one of them). You can hand pick which modules are used by your application by linking only against the libraries you need.

I cannot answer whether Qt will be interopable with MFC. But at the very least, QString offers conversion to std::string and char*/wchar, which should help you quite a bit.

The Qt documentation provides an overview over the modules.

As daniel pointed out below, you have to be aware of the event loop. It is possible however to use the event loop without the GUI module. You can call processEvents on QCoreApplication to process all queued events and then return. There is one caveat with deferred deletions, but the documentation describes the workaround.

Storm
This is not entirely true - you won't be able to use every class in Qt without its event loop. See @deus-ex-machina399 answer: http://stackoverflow.com/questions/1962103/can-i-use-qt-as-c-library-without-using-its-ui-framework/1962260#1962260
Idan K
Thanks daniel, I incorporated your concerns into my answer.
Storm
+5  A: 

Sure, you can use QT toolkit without using it's GUI library.

Depending on your needs, you might want to consider boost libraries which provides a sane set of APIs that helps for many things. I personally use it for doing network sockets in a multi-platform way, but there is a lot more in it.

Latifer
+1 for the boost mention.
just somebody
+2  A: 

yes you can, you have just to exclude QtGui Module from your project (.pro) because it included by default.

 QT -= gui

like this the Core Module only is used.

Ayoub
+3  A: 

There are some utility classes that you can use but there is a very important caveat. Qt depends very heavily on its event loop. The event loop is started by calling QApplication::exec(). Now many Qt classes depend on the signals and slots mechanism is Qt. Signals and slots are totally dependent on the event loop to function correctly.

This is totally true for the GUI modules but is also true of some of the other modules. One can expect every class derived from QObject to use signals and slots and will therefore be unusable without the event loop.

doron
Hmm, good to know. Is it theoretically possible to process the Qt event loop non-blocking within my MFC app? Or will this cause more problems than benefits?
Fabian
@Fabian: It will be very complicated and messy, and I think the main event loop classes QApplication and QCoreApplication assume they represent the entire state of the program. What you should do is miss out on signals and slots completely, which is safer and more sane. Even QtNetwork can be used without it; just use blocking calls.
blwy10
+1 none of the other answers addressed the event loop - which is key in Qt.
Idan K