in Code

QString::toStdString, QString::fromStdString, and -no-stl

I use the Qt library in my day-to-day development work. I build my Qt libraries directly from the git repository and they are configured specifically for my projects. On the Mac OS X side, this is the command line I use:

The Windows one is similar:

You will notice that I am explicitly removing a whole bunch of stuff – SVG, WebKit, audio, etc.. The time it takes to build the Qt libraries is not insignificant and since I typically track the git repo, I don’t want to waste time building things I will not be using in my projects. The WebKit code takes an extraordinary amount of time to build, for example. I also don’t want to build things into the libs that I’m not going to be using for my commercial software – such as STL. One of the problems this poses, however, is how to handle build problems with other projects I want to build, for example the GUI for the open-source Cppcheck program.

For the most part the projects I’m interested in don’t use any of the capabilities I’m eliminating from my Qt build, but one option has been problematic for a couple of projects: -no-stl. This is because the projects use the functions QString::toStdString() and QString::fromStdString() which do not exist when you build Qt with the -no-stl option. While STL may be available to the source you are building, it was not compiled into the Qt libs, so this causes an error.

When I try to build Cppcheck using my own Qt build, I get errors like this:

../gui/mainwindow.cpp: In member function ‘Settings MainWindow::GetCppcheckSettings()’: ../gui/mainwindow.cpp:457: error: ‘class QString’ has no member named ‘toStdString’

../gui/threadresult.cpp: In member function ‘virtual void ThreadResult::reportOut(const std::string&)’: ../gui/threadresult.cpp:42: error: ‘fromStdString’ is not a member of ‘QString’

Solutions?

Well the obvious one is for me to just bite the bullet and configure Qt with STL and compile it in. As I mentioned though I don’t want extra code in the binaries I’m shipping and I don’t use STL. Second solution? Build another version of Qt with STL enabled. Not a bad solution since I can just pick the latest stable release and build it. Then I just have to make sure when building other projects that they pick the right Qt version. I’ll likely end up doing this at some point if I keep running into this problem.

I suppose Qt could be modified to always define the two functions regardless of the -no-stl configuration option and do the appropriate thing. This would be the ideal solution.

A final option would be for developers using Qt to realize that people may not have STL in their Qt build and design around it, but that’s not exactly a likely scenario…

If you do find yourself stuck in a situation where you cannot change the Qt library configuration, the two functions can easily be replaced to allow you to build whatever project you’re trying to build.

QString::toStdString() may be replaced by qPrintable() like this:

qPrintable() returns a const char * and is equivalent to str.toLocal8Bit().constData().

QString::fromStdString() may be replaced by QString::fromAscii() like this:

You might be wondering about the use of QString::fromAscii() and how that might work with Unicode. The docs for it state:

Note that, despite the name, this function actually uses the codec defined by QTextCodec::setCodecForCStrings() to convert str to Unicode.

In the Cppcheck GUI, I only had to make changes in about half a dozen places so it would compile properly with my current Qt configuration.

Write a Comment

Comment