Results 1 to 9 of 9

Thread: How to use STL in Qt

  1. #1
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to use STL in Qt

    Hi,

    I am unable to find a proper way to use STL in my Qt application. Using Qt containers is currently not an option.

    I tried modifying .pro file by adding:
    • CONFIG += stl

    • INCLUDEPATH += /path/to/stl (in my case /usr/include/c++/4.4)


    This gives me an error:
    /usr/include/c++/4.4/string:40: fatal error: bits/c++config.h: No such file or directory

    I'm able to compile an ordinary C++ STL application so I assume I misconfigured something in Qt. Qt is built with STL support.

    Thank you,

    Adi

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: How to use STL in Qt

    If Qt is built with STL support than it should work by default, you don't need to add anything in .pro file or anywhere else.

    But you can't use std::string instead QString, you will need to convert it to QString to use it in widgets (or any other Qt library function that takes a QString)
    Check this example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <string>
    4. int main(int argc, char** argv) {
    5. QApplication a(argc, argv);
    6. std::string hello = "Hello World!";
    7.  
    8. QLabel l;
    9. l.setText(QString::fromStdString(hello));
    10. l.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    11
    Thanked 16 Times in 16 Posts

    Default Re: How to use STL in Qt

    Try to use this:

    Qt Code:
    1. INCLUDEPATH += <stlpath>
    2. DEPENDPATH += <stlpath>
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to use STL in Qt

    There is no need for such things. STL should be available out of the box. If this compiles without Qt:
    Qt Code:
    1. #include <string>
    2.  
    3. int main() { return 0;}
    To copy to clipboard, switch view to plain text mode 
    Then it should compile with Qt without any special treatment.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to use STL in Qt

    Thank you for your response.

    Just tried to compile the basic Qt + STL example (as above) and it works but unfortunatelly I'm still experiencing the same problems. What I have is a quite complex Qt project which links to other non-Qt code written in standard C++ and STL.

    My development structure consists of following folders:

    Development
    |
    -------Qt project folder
    |
    -------Some other folder
    |
    --------- STL related code folder


    So problem occurs when header with STL related code is included in Qt app. Important to notice is that header is NOT in the same folder as Qt related files. It seems that Qt doesn't recognize STL from such scope:

    In file included from qtapp.h:12,
    stlRelatedCode.h:17: fatal error: string: No such file or directory


    I'm assuming that it could be something with given folder structure. I'll try with moving STL code in the same folder but that will really mess up the code structure :/

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to use STL in Qt

    Just tried to compile the basic Qt + STL example (as above) and it works
    This means, that your compiler knows where STL headers are, becuase the example includes an stl header.
    So STL location seems NOT to be your problem.
    What do you mean by "STL related code"?
    It seems that you mean by that code that uses STL, rather than STL header.
    If so, your problem seems to be that you project configurations doesn't have the correct paths to all the headers you are using (but which are NOT STL headers)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to use STL in Qt

    Yes, I was thinking on code that is just using STL library (i.e. std::string) and not on the STL headers.

    That STL code is packed as .a lib so all I need to do to use is just set

    INCLUDEPATH += /path/to/lib/headers
    LIBS += /path/to/lib

    which I've already done.

    Can't really find the source of the problem :/

    But nonetheless thank you for cooperating

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How to use STL in Qt

    There is no magic here.
    It looks like your '/path/to/lib/headers' is wrong, even though you think it is right.
    There is no reason why your compiler can find some headers and not others, apart from it not having the correct path to them.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to use STL in Qt

    It seems that the whole Eclipse project was somehow messed up as it compiles successfully as soon as I created a new project from the start with same settings.

    Thank you all for your help.

    Adi

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
  •  
Qt is a trademark of The Qt Company.