Results 1 to 8 of 8

Thread: Using Qt in a C++ library

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Using Qt in a C++ library

    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.

  2. #2
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qt in a C++ library

    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Using Qt in a C++ library

    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.

  4. #4
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qt in a C++ library

    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...?

  5. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using Qt in a C++ library

    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!

  6. #6
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qt in a C++ library

    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!

  7. #7
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using Qt in a C++ library

    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.

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Using Qt in a C++ library

    Quote Originally Posted by Luc4 View Post
    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.

Similar Threads

  1. Replies: 4
    Last Post: 18th December 2009, 19:55
  2. KDE library and QT
    By SimbadSubZero in forum KDE Forum
    Replies: 1
    Last Post: 2nd December 2009, 22:17
  3. Replies: 1
    Last Post: 13th August 2009, 17:32
  4. It's possible 2 Qt's library?
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 26th October 2008, 15:51
  5. Qt3 library in Qt4 app
    By Opilki_Inside in forum Qt Programming
    Replies: 3
    Last Post: 24th May 2006, 17:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.