Qt项目无法包含UI头文件问题求助(QtCreator4.5)
Hey there, let's work through that UI header file error you're facing in your Qt project! I've run into this exact issue before, so here's what to check step by step:
1. Double-check your .pro file configuration
Qt uses the uic tool to generate ui_*.h header files from your .ui forms—but this only happens if you've added your .ui file to the FORMS variable in your .pro file. Make sure your .pro includes a line like:
FORMS += your_form_name.ui
Replace your_form_name.ui with the actual name of your UI file (e.g., gotocell.ui if you're following the gotocell1 example). Without this line, qmake won't tell uic to generate the header, so your compiler will never find it.
2. Verify your #include statement matches the generated header
The generated header file name follows the pattern ui_<your_form_name>.h. So if your UI file is gotocell.ui, the header will be ui_gotocell.h. Double-check that your main.cpp uses the correct name:
#include "ui_gotocell.h"
Note: If you're on a case-sensitive OS (like Linux), make sure the capitalization matches exactly—ui_GotoCell.h won't work if the generated file is ui_gotocell.h.
3. Re-run qmake and clean your build directory
Sometimes old build artifacts or an outdated Makefile can cause this issue. Here's how to refresh things in Qt Creator 4.5:
- Go to the Build menu → select Run qmake (this ensures qmake updates the build rules to include your UI file).
- Next, go to Build → Clean All to delete any old generated files.
- Finally, rebuild your project with Build → Build All.
This forces Qt to regenerate the UI header file from scratch and ensures the compiler can find it in the build directory.
4. Check your build directory include path
Qt Creator usually automatically adds the build directory (where ui_*.h files are generated) to your compiler's include path. But if you've customized your project's build settings, you might need to double-check:
- Open the Projects tab in Qt Creator.
- Under Build & Run, look at your active build configuration's Build directory.
- Ensure this directory is listed in your
.profile'sINCLUDEPATHif you've manually set it:INCLUDEPATH += $$PWD/your_build_directory_path
(Replace your_build_directory_path with the actual path from your project settings.)
Quick comparison to the gotocell1 example
Take a look at the gotocell1 project's .pro file—you'll see it explicitly lists the UI file in FORMS, and the main source file includes the corresponding ui_*.h header. Matching that structure should resolve your issue.
内容的提问来源于stack exchange,提问作者jwm

