Problem in Visual Studio 2019: "cannot open source file ui_*.h"
-
wrote on 1 Jul 2020, 12:45 last edited by
Hi! I am new to C++, Qt, and Visual Studio. I have made a few small applications in Qt creator for practice. When I use Visual Studio 2019 and create a new Qt Widgets Application the mainWindow class's header file looks something like this:
#pragma once #include <QtWidgets/QMainWindow> #include "ui_mainWindow.h" class mainWindow : public QMainWindow { Q_OBJECT public: mainWindow(QWidget *parent = Q_NULLPTR); private: Ui::mainWindowClass ui; };
The Visual Studio tells me that there is a problem (shown below as comments) with these two lines in the above code:
#include "ui_mainWindow.h" // Problem: cannot open source file "ui_mainWindow.h" Ui::mainWindowClass ui; // name followed by '::' must be a class or namespace name
This happens every time I add a new form/window. Strangely enough, after I close the solution and reopen it then this problem usually disappears. I searched a lot for this problem on google as well as on Qt Forum and found no proper description and especially no way to fix this problem. From all my search this is all that I could understand:
- This problem is because Qt designer uses qmake while Visual Studio uses CMake
- ui_*.h files are automatically generated during build,
- ui_*.h files are how *.ui form files are translated into C++ files for Visual Studio
Till the time Visual Studio is unable to understand these two commands, the autocomplete feature doesn't work which is a big pain while programming. I am the type of person who likes to keep everything working smoothly and I would go to any extent to fix this problem permanently, but I don't know how to get it done. If you can then please help me fix this problem. Thanks :)
-
wrote on 2 Jul 2020, 10:11 last edited by VRonin 7 Feb 2020, 10:13
It's an issue relating to the VS Tools addin and its usage of uic. I normally fix it by changing back and forth between the 32bit and 64bit version of the project (using the Qt Project properties context menu option). It's annoying but it's berable once you get used to it.
This problem is because Qt designer uses qmake while Visual Studio uses CMake
VS does not use CMake. Qt Creator can use both qmake and CMake
ui_*.h files are automatically generated during build,
Correct but the problem arises from the timing of its creation
-
It's an issue relating to the VS Tools addin and its usage of uic. I normally fix it by changing back and forth between the 32bit and 64bit version of the project (using the Qt Project properties context menu option). It's annoying but it's berable once you get used to it.
This problem is because Qt designer uses qmake while Visual Studio uses CMake
VS does not use CMake. Qt Creator can use both qmake and CMake
ui_*.h files are automatically generated during build,
Correct but the problem arises from the timing of its creation
-
wrote on 21 Dec 2020, 03:25 last edited by
My solution: since header files are generated at $(IntDir)uic\ (see Qt User Interface Compiler -> Output Directory) I've added that to C/C++ -> General -> Additional Include Directories.
-
My solution: since header files are generated at $(IntDir)uic\ (see Qt User Interface Compiler -> Output Directory) I've added that to C/C++ -> General -> Additional Include Directories.
wrote on 21 May 2022, 09:37 last edited by@Marcio-Barcellos Thank you! After days of troubleshooting this finally worked:
$(IntDir)uic\ (see Qt User Interface Compiler -> Output Directory) I've added that to C/C++ -> General -> Additional Include Directories.
Im a novice so I dont know what its actually doing but thank you for getting me through this first hurdle!