Precompiled header for moc_* in Visual Studio with Qt add-in - c++

I try to make moc.exe to add a precompiled header include into generated moc_* files in Visual Studio with Qt plugin.
I tried to add a PRECOMPILED_HEADER=myheader.h (also with quotes) line into project's preprocessor definitions, that didn't work.
I also tried to add -DPRECOMPILED_HEADER=myheader.h as a parameter for moc.exe in the project *.vcxproj file.
Can anyone help?

Open Qt project settings (from context menu of vcxproj in VS) there you'll have moc options, in there you can use something like: -b stdafx.h
Note in VS2019 the integration changed and you have the Qt options integrated into the default options dialog, there you have Qt Meta-Object Compiler and an Force Include option, here you should not need the -b flag (but, you'll need to test that if this is the case). For more info you can check the documentation here.

Adding -fstdafx.h -f../%(Identity) to the moc.exe command line works for me.

Related

Qt:Where to find the .pro file when developing with visual studio

I want to add sound to my program so I need to add header file “QSound”.
To add the header file, I need to modify the .pro file.
The problem is, I use visual studio to develop projects, so I can't find the .pro file.
I am so confused.
Thanks for your help!
If you're using Qt VS Tools, use Create Basic .pro File from extensions menu. Note that .pro and .vcxproj files might not be in sync so if possible use Qt Creator to configure your project in the .pro file and then open the project in visual studio via Open Qt Project File
Note that if you are using MS Visual Studio and Qt, and you need to add a specific qt module to your project, you do not need to have a .pro file just for that. That would be the way to go when not using VS.
Instead, in Visual Studio select your project, select menu "Project / Properties", there you should have an entry "Qt Project Settings", and there a section "General", there a setting "Qt Modules" which probably already contains "core" and some entries.
Add your module to this line. Remember to do that for all your configurations and platforms.

QT Visual Studio plugin fails automatic moc with non-standard header extensions

I am currently trying to port a very large project to QT. Many of the headers in this project use the file extension .hh . I would ideally like to avoid having to rename all of these files, but it appears to be causing some issues for the QT Visual Studio plugin. I am using Visual Studio 2012 and QT 5.5.
I have set up a trivial project to test this problem. If i define a class in a .h file that inherits from QObject and contains the Q_OBJECT macro the Custom Build instructions are automatically added to the files properties and moc'd files are automatically generated. If i rename the file to have a .hh extension no Custom Build instructions are generated and no moc'd files are generated.
I have attempted adding hh to the "Filter" property of the folder/filter containing my header files. This seems to result in the Custom Build instructions being applied to the headers, the correct files being generated by MOC, but not automatically included into the solution. This results the build failing with linker errors.
I would be surprised if this wasn't configurable some how. Has anyone had any success with custom file extensions before?

QCustomPlot in MSVS2012

I succeeded in building a program in visual studio 2012 that runs QT without the plugin. I included the QT include folder and manually added the libraries to the resources. And it compiled and ran.
Now I wanted to use QCustomPlot in the same way, without the QtAddin plugin. I tried multiple projects, where I added QCustomPlot's .h and .cpp and tried to compile, but it gives me tons of linker problems(http://pastebin.com/SWVXktBk). Can someone point me in the right direction?
Windows 7 x64, QT 5.4.1,
After hours, I found it.
As UldisK pointed, I had to moc the header.
I followed everything in here: http://ldmartin68.com/QTSetup4VSNET.html
In windows cmd
moc qcustomplot.h > moc_qcustomplot.cpp
then simply add the generated file to the project, and voila worked.
In the end you should have in the project:
the original qcustomplot.h
the original qcustomplot.cpp
the generated moc_qcustomplot.cpp

Using Qt with Visual Studio without add-in

I recently started using Qt library and I've got a question.
Is this possible to use Qt with Visual Studio without special add-in?
I want to just design the UI using qt designer and do the rest in VS Express.
How do I do that?
Thanks.
Yes you can, if you would prefer not to use the QtVSAddin it is very easy to use Qt with VS Express without the VS add-in and without having to do any of the uic or moc steps manually. Let QMake (installed with Qt but not part of the QtVSAddin) create your VS project file and do all your project setup in a qmake project file. Whenever you make a change like adding/removing a form or source, modify the qmake project file and regenerate the VS project. Don't modify the VS project file at all, treat it only as a temporary item. QMake will add the rules automatically to the VS project file to rerun uic and moc, you don't need to do anything if you're just modifying source code or forms.
For configuration management purposes I find this a much cleaner approach to use this workflow as you treat the VS project file as only a temporary item (they tend to diff badly and are a pain to maintain in version control).
A couple snippets to help you out:
In your qmake project file ensure you add the following line into it so that VS project files are generated when running on Windows (qmake defaults to generating a makefile).
your_qmake_proj.pro
win32: TEMPLATE = vcapp
Additionally, it's convenient to have a batch file to rerun qmake so you don't have to bring up a command prompt and set environment up (or change directory to your project in a command prompt that already has the environment setup). If you haven't set the various Qt environment variables with Windows (or prefer not to) make sure to add them to your batch file.
makevcproj.bat
set QTDIR=C:\Qt\x.y.z
set PATH=%PATH%;%QTDIR%\bin
set QMAKESPEC=win32-msvcXXXX
qmake your_qmake_proj.pro
pause
CMake is also an answer and it does work with express versions of Visual Studio. I mean if you use the Qt support in CMake you can develop Qt projects in Visual Studio (like I have done for years) without the Qt Addon. I install the addon just for the debug expansion that comes in the same package.
It is certainly possible, but without the add-in you will need to UI and MOC the needed files either before you compile the rest within VS, or through pre-compile scripting.
Specifically:
uic generates the headers from .ui files.
and
moc generates the additional implementation files for classes that has Qt macros in it.
The add-in helps you call these smoothly on the required files before compiling the rest.
It's is possible if you create the UI in QtCreator and manually setup VS in a way that generate the UI and MOC files.
But it's too much work and you can use QtCreator which is an amazing light IDE.

Qt ui file does not compile in to C++ header

I've created a Qt Application project in Visual Studio 2010. It contains the following files
Form Files
mainwindow.ui
Header Files
mainwindow.h
Source Files
mainwindow.cpp
It also uses a generated codefile called ui_mainwindow.h. From what I've been able to discern, this file should be regenerated in response to changes made to the .ui file. However if I open the ui file in Designer and make a change, no change appears in the application.
I have to go to Form -> View Code -> Save As in Qt Designer and save the ui_mainwindow.h file manually.
.ui file should have custom build tool defined to use uic on it and produce appropriate ui_*.h file.
Custom build tool command line may look like this:
uic.exe "$(InputPath)" -o "ui_$(InputName).h"
Also, in order to properly detect dependencies and to Clean command remove generated file, add ui_$(InputName).h to Custom Build Tool Outputs.
Don't forget to make the changes for all Configurations and Platforms.
Although, the right way to do this would be to create appropriate .pro file and then generate Visual Studio project with qmake -tp vc project.pro. All compiler and linker settings should be adjusted in .pro file, not in Visual Studio project that should be regenerated after each change of .pro file.
i mean no offense but have you saved the .ui file before running the project?
when i need to edit the ui files, i change them with the designer and when i want to run it, it will recompile/moc/whatever the changed ui file and their .h/.cpp file as well.