我在 Windows 上使用 QT 6.4.2 编写应用程序,但是当我需要使用 QHBoxLayout 时,我得到
error:Unknown type name 'QHBoxLayout'
所以我在QT中打开帮助文档,找到了关于QHBoxLayout的示例,我根据官方文档创建了一个非常简单的新项目:
.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版本和套件设置:
我尝试了很多次clean和re-build,但还是不行。我也找不到导致问题的原因,所以我尝试用QT(6.4.1版本)打开同一个项目MacBook Pro,同一个工程竟然可以在Mac平台上编译成功并生成可执行程序
以下是 MacBook Pro 上的一些图片:
那么QT最新版本的Bug at Windows?我该如何解决这个问题?
正如@HiFile.app - 最好的文件管理器所说,当我将 QHBoxLayout 更改为 QVBoxLayout 时,编译时似乎仍然无法工作,见下图。
现在我的 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");
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();
}
我是通过以下方法安装QT的:
目前只发现QHBoxLayout和QVBoxLayout有问题,其他控件暂时没发现问题
问题出在哪里?
当我使用以下命令时:
qmake.exe ChangeFontSize.pro
会生成Makefile文件。
然后我使用“VS 2022 开发人员命令提示符”运行以下命令:
nmake release
编译也得到同样的错误
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.
这里是图片:
我又新建了一个工程,测试QWidget和QPushButton在Windows平台下是否可以运行。见下图。
但是当我只在main.cpp中新增两行代码(其他地方没有做任何改动),编译就会报错,如下图:
到目前为止,我已经确认使用QHBoxLayout和QVBoxLayout是有问题的