我在编写 App 的 Windows 上使用 QT 6.4.2,但是当我需要使用 QHBoxLayout 时,我得到

error:Unknown type name 'QHBoxLayout'

于是我打开QT的帮助文档,里面找到了QHBoxLayout的sample,我按照官方文档新建一个工程,很简单:

.pro 文件是

QT       += core gui widgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp

HEADERS +=

FORMS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp 是

#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget *window = new QWidget;
    QPushButton *button1 = new QPushButton("One");
    QPushButton *button2 = new QPushButton("Two");
    QPushButton *button3 = new QPushButton("Three");
    QPushButton *button4 = new QPushButton("Four");
    QPushButton *button5 = new QPushButton("Five");
    QHBoxLayout *layout = new QHBoxLayout(window);
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);
    window->show();
    return a.exec();
}

下面是 Windows 上的一些图片:

在此处输入图像描述

在此处输入图像描述

轮廓

下面是 Windows QT 版本和套件设置:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I try to clean and re-build many times, but it still can't work. I also can't find out what reason cause the problem, so I try to open the same project with QT(6.4.1 version) at MacBook Pro, The same project can surprisingly be compiled successfully and generate executable programs on the Mac platform.

Below is some picture at MacBook Pro:

在此处输入图像描述

在此处输入图像描述

So is that QT latest version's Bug at Windows? How can I solve this problem?

First update

As @HiFile.app - best file manager said, when I change QHBoxLayout to QVBoxLayout, it seems still can't work when compile, see below picture.

在此处输入图像描述

Now my main.cpp file is :

#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget *window = new QWidget;
    QPushButton *button1 = new QPushButton("One");
    QPushButton *button2 = new QPushButton("Two");
    QPushButton *button3 = new QPushButton("Three");
    QPushButton *button4 = new QPushButton("Four");
    QPushButton *button5 = new QPushButton("Five");
    QVBoxLayout *layout = new QVBoxLayout(window);
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);
    window->show();
    return a.exec();
}

I installed QT through the following method:

在此处输入图像描述

Currently, I have only found issues with QHBoxLayout and QVBoxLayout, and have not discovered any issues with other controls for the time being.

Where did the problem occur?

Second update

When I use below command:

qmake.exe ChangeFontSize.pro

will generate Makefile file.

then I use "Developer Command Prompt for VS 2022" to run below command:

nmake release

for compile also get same error

C:\my-win10-document\code\ChangeFontSize>nmake release

Microsoft (R) 程序维护实用工具 14.33.31630.0 版
版权所有 (C) Microsoft Corporation。  保留所有权利。

        C:\my-win10-document\software---install\VS--install\VC\Tools\MSVC\14.33.31629\bin\HostX86\x86\nmake.exe -f Makefile.Release

Microsoft (R) 程序维护实用工具 14.33.31630.0
版权所有 (C) Microsoft Corporation。  保留所有权利。

        C:\my-win10-document\software---install\QT--install\6.4.2\mingw_64\bin\uic.exe mainwindow.ui -o ui_mainwindow.h
        g++ -c -fno-keep-inline-dllexport -O2 -std=gnu++1z -Wall -Wextra -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I../../software---install/QT--install/6.4.2/mingw_64/include -I../../software---install/QT--install/6.4.2/mingw_64/include/QtWidgets -I../../software---install/QT--install/6.4.2/mingw_64/include/QtGui -I../../software---install/QT--install/6.4.2/mingw_64/include/QtCore -Irelease -I. -I/include -I../../software---install/QT--install/6.4.2/mingw_64/mkspecs/win32-g++ -o release\main.o main.cpp
        g++ -c -fno-keep-inline-dllexport -O2 -std=gnu++1z -Wall -Wextra -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I../../software---install/QT--install/6.4.2/mingw_64/include -I../../software---install/QT--install/6.4.2/mingw_64/include/QtWidgets -I../../software---install/QT--install/6.4.2/mingw_64/include/QtGui -I../../software---install/QT--install/6.4.2/mingw_64/include/QtCore -Irelease -I. -I/include -I../../software---install/QT--install/6.4.2/mingw_64/mkspecs/win32-g++ -o release\mainwindow.o mainwindow.cpp
mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
mainwindow.cpp:29:6: error: 'QHBoxLayout' was not declared in this scope; did you mean 'QLayout'?
   29 |      QHBoxLayout *hLayout = new QHBoxLayout;
      |      ^~~~~~~~~~~
      |      QLayout
mainwindow.cpp:29:19: error: 'hLayout' was not declared in this scope; did you mean 'QLayout'?
   29 |      QHBoxLayout *hLayout = new QHBoxLayout;
      |                   ^~~~~~~
      |                   QLayout
mainwindow.cpp:29:33: error: 'QHBoxLayout' does not name a type; did you mean 'QLayout'?
   29 |      QHBoxLayout *hLayout = new QHBoxLayout;
      |                                 ^~~~~~~~~~~
      |                                 QLayout
mainwindow.cpp:34:6: error: 'QVBoxLayout' was not declared in this scope; did you mean 'QLayout'?
   34 |      QVBoxLayout *vLayout = new QVBoxLayout;
      |      ^~~~~~~~~~~
      |      QLayout
mainwindow.cpp:34:19: error: 'vLayout' was not declared in this scope; did you mean 'QLayout'?
   34 |      QVBoxLayout *vLayout = new QVBoxLayout;
      |                   ^~~~~~~
      |                   QLayout
mainwindow.cpp:34:33: error: 'QVBoxLayout' does not name a type; did you mean 'QLayout'?
   34 |      QVBoxLayout *vLayout = new QVBoxLayout;
      |                                 ^~~~~~~~~~~
      |                                 QLayout
NMAKE : fatal error U1077: “C:\my-win10-document\software---install\QT--install\Tools\mingw1120_64\bin\g++.EXE”: 返回代码“0x1
Stop.
NMAKE : fatal error U1077: “C:\my-win10-document\software---install\VS--install\VC\Tools\MSVC\14.33.31629\bin\HostX86\x86\nmake.exe”: 返回代码“0x2
Stop.

Here is picture:

在此处输入图像描述

Third update

I created a new project again for test QWidget and QPushButton in Windows platform that can work. See below picture.

在此处输入图像描述

But when I only new add two line code in main.cpp (No changes have been made elsewhere), then compile will get error, see below picture:

在此处输入图像描述

So far, I have confirmed that using QHBoxLayout and QVBoxLayout is problematic.


我自己重装QT解决了问题,现在可以正常使用了。

在此处输入图像描述


This is a very strange bug, I have never encountered such a problem - and I am using Qt 6.4.2 too for a widget application without any issues. So maybe a stupid suggestion: did you try to re-run qmake for your project? Btw. the About box for your QtCreator does show the version with which your QtCreator was built and does not show your actual Qt framework version. Are you sure you are using 6.4.2? Check Projects settings to see the version and check Preferences->Kits if your paths are correct.

@HiFile.app-bestfilemanager I have try to clean and re-rebuild many times,but still doesn't work. I'm sure I use QT 6.4.2 at windows(See above picture). I will update my question that show Preferences->Kits if my paths are correct.

@HiFile.app-bestfilemanager 你能找出问题出在哪里吗?请帮我解决这个问题。

恐怕,我不知道可能出了什么问题。您可以尝试其他 Qt 版本(例如最新的 LTS 版本 6.2.4)或 MSVC 版本(但需要安装 MS 构建工具)。我从未在 Windows 上使用过 MinGW 工具,所以我没有使用它们的经验......

问题仅在于QHBoxLayout?QHBox...如果你改成会发生什么QVBox...?那行得通吗?(我不是建议你重新定义你的布局,我只是想诊断问题)

哦,我以为这是你已经尝试过的东西。否则我会建议将其作为第一步。

@HiFile.app-bestfilemanager 我应该先尝试重新安装 QT。

微信小程序

微信扫一扫体验

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部