qtdll

How do you include Qt in visual studio ide?


I am very new to c++ I want to be able to use the Qt library, qtcore and qtwidgets to make a gui application. I also want to use the visual studio ide.

I have downloaded the Qt library from the qt installer and it is in my c drive. I have qt version 6.6.0

In the 6.6.0 file I have the following files:

mingw_64 msvc2019_64 msvc2019_arm64 Src

I have added %QTDIR%\lib and %QTDIR%\bin to my path.

Basically what I want is a solution so that when I have #include in my main.cpp file for example.

I have looked on youtube and some posts on stack overflow and tried some of the solutions but have not been able to get what i want to work...

I am on windows 11


Solution

  • Easy way using the Qt VS tools extension

    The extension gets most of the work done. If you are very new to Qt/VS, that is the way to go (but I recommend you read the next section of my answer to get familiar with what is going on):

    1. Go to Visual studio, menu Extensions > Manage Extensions
    2. Search the Visual studio marketplace for Qt Visual Studio Tools

    Once installed:

    1. You will find new project Templates under project type "Qt".
    2. The main(int, char**) function + a default object (widget, QML window, ...) are automatically created.
      In the project properties, you will find a number of menus to more easily manage the parameters of your project.
    3. When adding new elements to your project, you will find new types such as "Qt class" or "Qt Widgets class".
      Selecting these automatically creates invocations to moc and uic, to name only the more common.

    Manual way

    Of course, it is also possible to configure everything manually in Visual Studio, which could be useful if you do not want to rely on an extension or if you work for a company that will not allow installing the Qt extension.
    The obvious downside is that there is much more configuration work to do than just selecting the correct items:

    1. Optional but strongly recommended: To save yourself some time later, open View > Other Windows > Properties Manager.
      Add new property sheets (you'll need 1 for debug, 1 for release) for your project to store, in user macros:

      • QtVersion (6.6.0 in your case)
        When you upgrade to a later version of Qt, you will only have to change this variable to impact everything else).
      • The path to Qt installation (equivalent to your %QTDIR% but I find it more convenient to have it created there than in the environment variables for e.g. sharing your VS configuration to other people).
        Do not forget to include $(QtVersion) in the variable definition otherwise the above would turn out useless; it should be something like C:\Qt\$(QtVersion)\msvc2019_64 (or msvc2022_64 for the newest versions of Qt, at time of writing).
        If people of your team have installed Qt on different paths on their respective computers, you will most likely need to have everyone configure a QtRoot environment variable to their respective Qt folder; then the macro you have to define will be $(QtRoot)\$(QtVersion)\msvc2019_64.
      • A variable to store calls to moc, uic and other tools with default parameters (The calls may be different for debug and for release).
        On top of the more common tools cited above, you will need to create WinDeployQtExe to call "$(QTDIR)\bin\windeployqt.exe" --debug/"$(QTDIR)\bin\windeployqt.exe" --release respectively
      • A variable for each of the .lib file your are going to use (e.g. Qt6Cored.lib for debug, Qt6Core.lib for release).

      If you do all that, the rest of the configuration can be applied for all configurations at once. If you divide your solution into several sub-projects, you can reuse the same property sheets for all of them (in the property manager, instead of "Add new property sheet" select "Add existing property sheet".

    2. Back to the project properties, VC++ Directories:

      • Add $(QTDIR)\include as an include directory (or %QTDIR%\include if you have skipped the above optional step; for the rest of my answer, I will assume you have applied it).
      • Add $(QTDIR)\lib as a library directory.
    3. Under General, make sure C++ Language standard is ISO C++ 17 (required for Qt6).

    4. Under C++ > Command line, add the additional option /Zc:__cplusplus.

    5. Under Linker > Input, add the necessary Qt libraries (e.g. $(QtCoreLib);$(QtGuiLib);$(QtWidgetsLib)) to Additional dependencies

    6. For every header ui, qrc, ts, ... file you need to compile, go to their properties:

      1. Change Item type to Custom build tool.
      2. Fill the command line (e.g. to moc a .h file: $(Qtmoc) %(Fullpath) -o ".\GeneratedFiles\$(Configuration)\moc_%(Filename).cpp"), description, output list and additional dependencies (e.g. $(QTDIR)\bin\moc.exe;).

      Do not forget to do the above after selecting All configurations and All platforms.
      After the files are compiled, right-click on your project, select Add > Existing items and go select all the files that were compiled by custom build tool. Feel free to store the file in a new filter (Right click, Add > New filter).

    7. Under Build events > Post-build event add the 2 command lines to get DLLs copied to your output dir:

    SET "VCINSTALLDIR=$(VCInstallDir)"
    $(WinDeployQtExe) <put the --options suitable to your project here> "$(OutDir)$(TargetFileName)"
    

    In both solutions, there will still be a few things you will need to do manually.
    For instance, on my project, I have to compile the PSQL driver dll once and for all, then copy it in the sqldrivers subfolder of my outdir by hand.