Monday, October 10, 2011

Fixing Qt Warning: Unescaped backslashes are deprecated!

You might have hit this warning while you are trying to include source files located under sub directories to a Qt project.

Fix to solve this warning is very simple!

This warning occurs whenever you try to use backslash (\) as path separator. To avoid this warning just use forward slash (/) as your path separator, instead of backslash (\). Qt uses Unix style path separator (/) irrespective of the platform.

Example: If you want to include parent directory use ../ instead of ..\

Following Qt project file example explains the problem & solution in detail.

Consider following Qt project file. Here we have used backslash (\) as our path separator.
# Sample.pro
TEMPLATE = app
CONFIG  += console
TARGET   =
DEPENDPATH  += .
INCLUDEPATH += ..\include    \
               ..\..\include
unix:LIBS   += -L/usr/local/lib
LIBS        += -lgtest
DEFINES     += USE_GTEST
QT          += network

# Input Files
HEADERS +=  LoggerTester.h      \
            ..\src\Logger.h
SOURCES +=  LoggerTester.cpp    \
            ..\src\Logger.cpp   \
            Main.cpp

As expected above project file results into a lot of Qt warning saying: "Unescaped backslashes are deprecated."

The same Qt project file after changing the path separator:
# Sample.pro
TEMPLATE = app
CONFIG  += console
TARGET   =
DEPENDPATH  += .
INCLUDEPATH += ../include    \
               ../../include
unix:LIBS   += -L/usr/local/lib
LIBS        += -lgtest
DEFINES     += USE_GTEST
QT          += network

# Input Files
HEADERS +=  LoggerTester.h      \
            ../src/Logger.h
SOURCES +=  LoggerTester.cpp    \
            ../src/Logger.cpp   \
            Main.cpp

As expected new project file does not create any Qt warnings.

Thats all! Please leave a comment if this post helped you.

Technorati tags: , .

2 comments: