Important: Please read the Qt Code of Conduct -

Problem in Visual Studio 2019: "cannot open source file ui_*.h"

  • 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
        mainWindow(QWidget *parent = Q_NULLPTR);
        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 :)

  • 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

  • @VRonin Thanks! Changing between 32bit and 64bit versions seems to do the trick faster than what I was doing before.

  • 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.

Log in to reply