Qt Creator编译C++软件时无法找到QtGui头文件求助
Hey there! I’ve run into similar head-scratching issues when working with Qt4 projects in Qt Creator, so let’s walk through the most likely fixes for your problem:
1. Make sure you’re using a Qt4 kit in Qt Creator
Qt5 and later split GUI components into separate modules—QMainWindow moved to QtWidgets instead of staying in QtGui. Since your headers live in /usr/include/qt4/QtGui, you need to confirm your project is tied to a Qt4 kit:
- Open your project in Qt Creator, switch to the Projects tab (left sidebar).
- Under Build & Run, check the active kit. It should list a Qt4 version (like Qt 4.8.7) instead of Qt5/Qt6.
- If no Qt4 kit exists, add one via Tools -> Options -> Kits -> Add, pointing to your Qt4 installation (look for the
qmakebinary in your Qt4 bin directory, usually something like/usr/bin/qmake-qt4).
2. Double-check your project’s .pro file
Your .pro file needs to explicitly include the gui module for Qt4. Open the file and confirm this line is present:
QT += gui
If you accidentally added QT += widgets (which is for Qt5+), that’ll throw things off—stick with gui for Qt4 projects.
3. Manually add the Qt4 include path to build settings
Sometimes Qt Creator doesn’t auto-detect the Qt4 include directory correctly. Let’s add it manually:
- Go to Projects -> Build Settings -> [Your Active Compiler] -> Include paths.
- Click Add and enter
/usr/include/qt4. This tells the compiler to look for subdirectories likeQtGuiwithin this path, which matches your#include <QtGui/QMainWindow>syntax perfectly.
4. Clean and rebuild your project
Old build artifacts or cached config can sometimes cause false "file not found" errors. Do a full reset:
- Click Build -> Clean All to wipe existing build files.
- Then click Build -> Rebuild All to compile everything from scratch.
If none of these work, quickly verify the file actually exists by running ls /usr/include/qt4/QtGui/QMainWindow in the terminal. You might also check file permissions—ensure your user has read access to that directory and its files.
内容的提问来源于stack exchange,提问作者S.EB

