PDA

View Full Version : Using Qt in a C++ library



Luc4
29th June 2010, 11:45
Hi! I was wondering if it is possible to use Qt classes from a C++ standard library project I'm working on. This project is a C++ standard static library, and I tried including the Qt headers and linking the libraries. I added the preprocessor definitions as well. Unfortunately it seems not to work yet. Am I trying to do something not possible?
Thanks.

Vit Stepanek
29th June 2010, 12:14
That could be a hard task, could you do it the opposite way? Calling your std cpp project from the new qt application? That works fine.

SixDegrees
29th June 2010, 13:51
There's nothing hard about this. You simply include your required Qt headers in your library, resolve any metadata in the usual way by running your code through the MOC, then linking against the appropriate Qt libraries in your build.

Note that you are creating a dependency on Qt for whoever uses your library. This may or may not be a problem. Also, Qt's metadata paradigm will complicate the user's build, as all those tools needed to manage it will also become dependencies.

Vit Stepanek
29th June 2010, 14:55
Yes, it is possible, but it's not the easier way. Why to do it by hand when it's done by few clicks in at least 3 different IDEs. No complications then...?

Luc4
29th June 2010, 20:04
Unfortunately it's quite complicated I suppose to get the project into another created using Qt creator. The project I'm working on it's very very big and I have to insert it into another project I was developing. I will try to avoid using Qt then, and otherwise I will try this way, since as far as I can understand, the other way is not simple as well.

It would be quite interesting anyway to be able to use Qt anywhere I need... I already tried including the headers and the libraries, but what do you mean by "resolve any metadata in the usual way by running your code through the MOC"? I need to add the Qt libraries to this project in VS2008, as it seems to be very very hard to compile it with mingw, and moreover I need it for Windows CE also. So, I added headers and libraries, and I set the preprocessor definitions I copied from other working Qt VS2008 projects. What else is needed?

Thanks to everyone!

Vit Stepanek
29th June 2010, 20:28
but what do you mean by "resolve any metadata in the usual way by running your code through the MOC"?

The Qt code needs to be precompiled. This should be possible to automate using prebuild events in Visual Studio.
Do you need to mix the Qt and non-Qt code? Isn't it suitable to create a dll for Qt for you? Then you'd get rid of the mixing.
Hope you'll solve that and let us know how!

Luc4
29th June 2010, 21:48
What do you mean by "mix the Qt and non-Qt code"? This static library contains standard C++ code plus platform specific code for many platforms (macos, Linux, Windows etc...). I suppose this is a mix right? So yes, I need to mix and I would like to add some parts in Qt so that I don't have to write something specific for each platform.

SixDegrees
29th June 2010, 23:14
What do you mean by "mix the Qt and non-Qt code"? This static library contains standard C++ code plus platform specific code for many platforms (macos, Linux, Windows etc...). I suppose this is a mix right? So yes, I need to mix and I would like to add some parts in Qt so that I don't have to write something specific for each platform.

Again: the problem is that Qt is more than a simple collection of headers and libraries. It makes extensive use of macros and labels which are used to generate psuedo-code which in turn is processed by Qt's metaobject compiler to produce compilable C++ code, which only then is run thrugh the C++ compiler. You need all these tools working together in concert in order to produce a working application.

And anyone attempting to use your library will almost certainly need to have a working Qt distribution so they can make use of these additional tools, as well.

Why is inserting portions of one project into another project difficult in the first place? This suggests that the existing project isn't organized in a way that permits easy reuse in the first place; if so, that problem will have to be overcome no matter what approach you take. We've produced scores of reusable, Qt-dependent components on several projects; it's simply a matter of sequestering the components into properly structured libraries. But you can't divorce yourself from dependencies on the additional tools Qt uses to generate compilable code; you're much better off using Qt's automated tools to manage this inescapable dependency.