Unknown type name 'QHBoxLayout'

341 Views Asked by At

I'm using QT 6.4.2 at Windows that write App, but when I need to use QHBoxLayout, I get

error:Unknown type name 'QHBoxLayout'

so I open help document in QT, that found sample about QHBoxLayout, I according to official document create a new project that is very simple:

.pro file is

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 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");
    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();
}

Below is some picture at Windows:

enter image description here

enter image description here

pro file

Below is Windows QT version and kit setting:

enter image description here

enter image description here

enter image description here

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:

enter image description here

enter image description here

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.

enter image description here

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:

enter image description here

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:

enter image description here

Third update

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

enter image description here

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:

enter image description here

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

1

There are 1 best solutions below

2
Tom On

I solved the problem by reinstalling QT by myself, and now it can be used normally.

enter image description here