Generate Visual Studio project for a QBS project in Qt Creator

As most Qt users we love Qt Creator for the daily use. But sometimes we need to fall back to Visual Studio for various reasons. Most often to get the best debugger available on Windows today.

QBS supports the generation of project files. It supports XCode and various Visual Studio versions. Unfortunately Qt Creator has no built in solution to generate our project files.

But we have various options:

  • Add “Custom Process Steps” to our “Build Steps” to our project
    • Only exists in the .user file of your project and easily gets lost
    • Runs with every build, unless disabled
  • Add an “External Tool” to Qt Creator
    • Run on demand for the current project

I will show you both options, so you can decide what fits your needs.

Now I have to show you another thing. QBS generates a lot of filter folders to the visual studio project. Look at this project, that has only a main.cpp file.

QBS generates a lot of filter folders.

Quelle: HicknHack Software GmbH

There are no options to change the solution generation of QBS. So I tried to delete all the *.filters files. Now all files are placed next to each other in each project.

Project without the filters

Quelle: HicknHack Software GmbH

Please also note that the subfolder structure of your files is never represented inside Visual Studio. For debugging purposes this is good enough.

Now back to the solution generation.

Custom Build Steps

To get custom build steps into your project proceed with the following steps.

Open your QBS project in Qt Creator and go to the Projects mode. Your Kit has to be Visual Studio based. Add two “Custom Process Steps”.

The first step should be configured like this:

  • Command: Browse for the qbs.exe inside your Qt Creator bin directory.
  • Arguments: generate -g visualstudio2017 -f "%{CurrentProject:NativeFilePath}" -d "%{buildDir}\msvc" --settings-dir "%{Session:NativePath}\qbs\1.12.0" config:%{CurrentBuild:Name}
    • These work with the latest Qt Creator 4.7.0.
    • You need to adapt this for your Visual Studio version and QBS version.

The second step, removes the undesired *.filters files:

  • Command: %ComSpec%
  • Arguments: /c del %{buildDir}\msvc\*.filters

It should look like this:

QBS custom build steps

Quelle: HicknHack Software GmbH

With this the Visual Studio project is now generated with every build. But this takes time and is unnecessary, as QBS takes care of updating the Visual Studio project anyways.

External Tool

I wrote a .bat file for this external tool (qbsvs2017gen.bat). Download the file and store it somewhere convenient.

To use it, drill down the menu of Qt Creator – Tools > External > Configure…

In the dialog you add a new tool. I named mine QBS Generate VS2017. And configured the tool like this:

  • Description: Generates a Visual Studio 2017 project for current QBS project
  • Executable: Browse for the saved batch file.
  • Arguments: "%{CurrentProject:NativeFilePath}" "%{CurrentProject:BuildPath}\vs" "C:\Qt\Tools\QtCreator\bin\qbs.exe" "%{Session:NativePath}\qbs" "%{CurrentBuild:Name}" "%{CurrentKit:Id}"

The batch file expects the following arguments:

  1. Full path to your project file
  2. Target directory where the visual studio project is created
  3. Full path to the qbs.exe (It’s inside you Qt Creator bin directory)
  4. The QBS settings base path
  5. “Debug” / “Release” build name (No switching in Visual Studio allowed)
  6. UUID of the Kit QtCreator uses (The QBS profile will be derived from that)

Mine looked like this when I was done:

QBS Qt Creator external tools configuration

Quelle: HicknHack Software GmbH

Now you can simply invoke this external tool and generate the Visual Studio project files for your current project. You may also assign a keyboard hot key to the tool if you like.

Here is the code of the batch file:

@echo off
set OLD_CD=%CD%

:: Extract Arguments
set PROJECT=%~1
set TARGET_DIR=%~2
set QBS=%~3
set QBS_SETTINGS_DIR=%~4
set CONFIG=%~5
set KIT=%~6

:: Load VC-Vars
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 >nul

:: Extract QBS Version
FOR /F "tokens=* USEBACKQ" %%F IN (`%QBS% --version`) DO (SET QBS_VERSION=%%F)

:: Extract QBS Profile
FOR /F "tokens=2 delims=: USEBACKQ" %%F IN (`call %QBS% config --settings-dir "%QBS_SETTINGS_DIR%\%QBS_VERSION%" "preferences.qtcreator.kit.%KIT%"`) DO (
    SET RAW_PROFILE=%%~F
)
set PROFILE2=%RAW_PROFILE:"=%
set PROFILE=%PROFILE2: =%

echo QBS version %QBS_VERSION%
echo Project %PROJECT%
echo Target %TARGET_DIR%
echo Profile %PROFILE%

:: Invoke QBS generator
echo "%QBS%" generate -g visualstudio2017 -f "%PROJECT%" -d "%TARGET_DIR%" --settings-dir "%QBS_SETTINGS_DIR%\%QBS_VERSION%" profile:%PROFILE% config:%CONFIG%
call "%QBS%" generate -g visualstudio2017 -f "%PROJECT%" -d "%TARGET_DIR%" --settings-dir "%QBS_SETTINGS_DIR%\%QBS_VERSION%" profile:%PROFILE% config:%CONFIG% >nul

:: Remove .filters files
del "%TARGET_DIR%\*.filters"

cd /d "%OLD_CD%"

Changelog:

  • 2018-07-24 Added Support for QBS profiles to allow Qt based applications to use Visual Studio as well.
←zurück

© 2012 – 2024 HicknHack Software GmbH | Impressum | Datenschutzerklärung