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
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):
Once installed:
main(int, char**)
function + a default object (widget, QML window, ...) are automatically created.moc
and uic
, to name only the more common.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:
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:
%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).$(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).QtRoot
environment variable to their respective Qt
folder; then the macro you have to define will be $(QtRoot)\$(QtVersion)\msvc2019_64
.moc
, uic
and other tools with default parameters (The calls may be different for debug and for release).WinDeployQtExe
to call "$(QTDIR)\bin\windeployqt.exe" --debug
/"$(QTDIR)\bin\windeployqt.exe" --release
respectively.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".
Back to the project properties, VC++ Directories:
$(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).$(QTDIR)\lib
as a library directory.Under General, make sure C++ Language standard is ISO C++ 17
(required for Qt6).
Under C++ > Command line, add the additional option /Zc:__cplusplus
.
Under Linker > Input, add the necessary Qt libraries (e.g. $(QtCoreLib);$(QtGuiLib);$(QtWidgetsLib)
) to Additional dependencies
For every header ui, qrc, ts, ... file you need to compile, go to their properties:
Custom build tool
..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).
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.