VS2022+QT5.13.2:无法打开 源 文件 “ui_QtGuiApplicationx.h“在解决方案(亲测有效)

在使用VS2022和QT5.13.2开发过程中,遇到编译错误E1696和C1083,原因是缺少ui开头的自动生成头文件。解决方案是右键点击UI文件,将属性中的“项类型”改为QTUserInterfaceCompiler(uic),然后重新编译,头文件会在项目文件的x64Debuguic目录下生成。
部署运行你感兴趣的模型镜像

@[TOC]问题描述
VS2022+QT5.13.2 在工程文件,生成解决方案在时候提示如下错误:
错误(活动) E1696 无法打开 源 文件 “ui_QtWidgetsApplication1.h” QtWidgetsApplication1 E:\test\QtWidgetsApplication1\QtWidgetsApplication1.h 4

错误 C1083 无法打开包括文件: “ui_QtWidgetsApplication1.h”: No such file or directory (编译源文件 QtWidgetsApplication1.cpp) QtWidgetsApplication1 E:\test\QtWidgetsApplication1\QtWidgetsApplication1.h 4

如图所示:
在这里插入图片描述
其表象原因在于ui_QtGuiApplicationx.h不存在。熟悉QT在都知道,这种ui开头的头文件通常都是IDE自动生产的,因此可以初步判断,VS没有自动生成该文件。

@[TOC]解决方案:
1、右键点击对应的ui文件,然后选择属性
在这里插入图片描述
2、在弹出的对话框中,将常规选项卡下的“项类型”改为QT User Interface Compiler(uic)。
然后点击应用,确定。
在这里插入图片描述
3、然后重新编译运行即可。在项目文件下的 x64\Debug\uic文件中就有对应的ui头文件生成了。
在这里插入图片描述

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

[ 84%] Built target test_framework_enhanced_autogen [ 84%] Building CXX object CMakeFiles/test_framework_enhanced.dir/test_framework_enhanced.cpp.obj In file included from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qobject.h:49:0, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qabstractanimation.h:43, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:6, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = std::unique_ptr<QObject>]': C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:603:27: required from 'void QList<T>::append(const T&) [with T = std::unique_ptr<QObject>]' C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:297:55: required from here C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:454:65: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QObject; _Dp = std::default_delete<QObject>]' if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t); ^~~~~~~~ In file included from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/memory:80:0, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/thread:39, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/future:39, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qthread.h:50, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:234, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/bits/unique_ptr.h:388:7: note: declared here unique_ptr(const unique_ptr&) = delete; ^~~~~~~~~~ In file included from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qobject.h:49:0, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qabstractanimation.h:43, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:6, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:455:39: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QObject; _Dp = std::default_delete<QObject>]' else if (QTypeInfo<T>::isComplex) new (n) T(t); ^~~~~~~~~~~~ In file included from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/memory:80:0, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/thread:39, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/future:39, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qthread.h:50, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:234, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/bits/unique_ptr.h:388:7: note: declared here unique_ptr(const unique_ptr&) = delete; ^~~~~~~~~~ In file included from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qobject.h:49:0, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qabstractanimation.h:43, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:6, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:460:35: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QObject; _Dp = std::default_delete<QObject>]' else *reinterpret_cast<T*>(n) = t; ~~~~~~~~~~~~~~~~~~~~~~~~~^~~ In file included from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/memory:80:0, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/thread:39, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/future:39, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qthread.h:50, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:234, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/bits/unique_ptr.h:389:19: note: declared here unique_ptr& operator=(const unique_ptr&) = delete; ^~~~~~~~ In file included from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qobject.h:49:0, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qabstractanimation.h:43, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:6, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h: In instantiation of 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = std::unique_ptr<QObject>]': C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:777:18: required from 'QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = std::unique_ptr<QObject>]' C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:601:37: required from 'void QList<T>::append(const T&) [with T = std::unique_ptr<QObject>]' C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:297:55: required from here C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:481:30: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QObject; _Dp = std::default_delete<QObject>]' current->v = new T(*reinterpret_cast<T*>(src->v)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/memory:80:0, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/thread:39, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/future:39, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qthread.h:50, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:234, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/bits/unique_ptr.h:388:7: note: declared here unique_ptr(const unique_ptr&) = delete; ^~~~~~~~~~ In file included from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qobject.h:49:0, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qabstractanimation.h:43, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:6, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qlist.h:494:17: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QObject; _Dp = std::default_delete<QObject>]' new (current) T(*reinterpret_cast<T*>(src)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/memory:80:0, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/thread:39, from C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/future:39, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/qthread.h:50, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtCore/QtCore:234, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTestDepends:3, from C:/Qt/Qt5.13.2/5.13.2/mingw73_64/include/QtTest/QtTest:3, from C:\Work\PengGuanJun\Work\AumoFIS\AumoFIS\FaultInjectionApp\tests\test_framework_enhanced.cpp:1: C:/Qt/Qt5.13.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/bits/unique_ptr.h:388:7: note: declared here unique_ptr(const unique_ptr&) = delete; ^~~~~~~~~~ mingw32-make.exe[2]: *** [CMakeFiles\test_framework_enhanced.dir\build.make:94: CMakeFiles/test_framework_enhanced.dir/test_framework_enhanced.cpp.obj] Error 1 mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:454: CMakeFiles/test_framework_enhanced.dir/all] Error 2 mingw32-make.exe: *** [Makefile:100: all] Error 2 错误:编译失败 Press any key to continue . . . 修复QTest编译错误\
05-28
<think>好的,我需要帮助用户解决在Qt中使用QList和std::unique_ptr时出现的编译错误。用户提到错误涉及复制构造函数和赋值运算符被删除的问题。首先,我得回忆一下相关的知识,特别是Qt容器和智能指针的交互。用户提供的引用中,引用[1]提到在容器中使用智能指针的情况,比如QVector或QList,这时候用std::unique_ptr可能会有问题,因为Qt的容器可能不支持移动语义。引用[4]展示了一个QList<std::unique_ptr>的例子,但用户在使用时遇到了编译错误,这可能是因为QList的某些操作尝试复制unique_ptr,而这是不允许的。 首先,std::unique_ptr是独占所有权的,不能复制,只能移动。Qt的QList在旧版本(比如Qt5)中可能没有充分支持C++11的移动语义,导致在使用append或emplaceBack等方法时,尝试复制而不是移动unique_ptr,从而触发编译错误。接下来,我需要考虑解决方案。可能的解决方法包括:1.使用移动语义显式转移所有权,比如使用std::move。2.改用支持移动语义的容器,如std::vector。 3.检查Qt的版本,确保其支持C++11或更高标准。 4.使用Qt自身的智能指针,如QScopedPointer,但这可能不如std::unique_ptr灵活。另外,用户提到的错误信息是关于复制构造函数和赋值运算符被删除,这明确指出了问题所在:代码中某处试图复制unique_ptr,而这是不允许的。因此,需要检查所有涉及到QList的操作,确保都是通过移动而非复制来操作unique_ptr。 比如,用户可能在代码中这样写:```cppQList<std::unique_ptr<MyClass>> list; list.append(new MyClass());//错误,因为尝试将原始指针转换为unique_ptr,可能导致复制```正确的做法应该是:```cppQList<std::unique_ptr<MyClass>> list; list.append(std::make_unique<MyClass>());// C++14以上支持,或者使用push_back和std::move``` 或者:```cppautoptr =std::make_unique<MyClass>();list.push_back(std::move(ptr)); //显式移动``` 另外,Qt的emplaceBack方法在C++11之后可能支持就地构造,但需要确保参数正确传递,避免隐式复制。例如,引用[4]中的代码:```cppm_contacts.emplaceBack(newContact("", "")); ```这里可能有问题,因为emplaceBack的参数应该是构造unique_ptr所需的参数,而直接传递new创建的指针可能需要使用std::unique_ptr的构造函数,但emplaceBack可能不会自动转换。正确的写法可能是: ```cppm_contacts.emplaceBack(std::make_unique<Contact>("",""));``` 或者使用push_back和std::move。此外,用户可能需要检查编译器是否启用了C++14或更高标准,因为std::make_unique是C++14引入的。在Qt项目中,通常需要在.pro文件中添加CONFIG+= c++14或者在CMake中设置相应的标准。 总结可能的解决步骤:1.确保使用C++14或更高标准。 2.在向QList添加元素时,使用std::move转移所有权。3.使用emplaceBack或push_back时,确保参数是右值引用。 4.考虑改用std::vector,如果Qt的QList确实存在兼容性问题。另外,需要提醒用户注意Qt对象的所有权问题,尤其是当QObject派生类使用智能指针时,因为Qt的对象树管理可能与智能指针的所有权管理产生冲突。在这种情况下,可能需要避免将QObject子类用std::unique_ptr管理,或者确保在释放时正确处理父对象关系。 最后,生成相关问题,帮助用户深入理解类似问题,比如如何正确使用shared_ptr,配置C++标准,容器选择等。</think>### 解决Qt中QList与std::unique_ptr编译错误的方法 #### 一、核心错误原理 错误根在于`std::unique_ptr`的独占所有权特性与`QList`的默认操作冲突: 1. **不可复制的智能指针** `std::unique_ptr`的拷贝构造函数被显式删除($\text{unique_ptr(const unique_ptr&) = delete}$),而`QList::append()`默认尝试拷贝元素[^1] 2. **移动语义未触发** Qt5的`QList`未完全支持C++11移动语义,导致无法自动调用移动构造函数: $$ \text{错误触发条件} = \exists \text{操作} \in \{ \text{append}, \text{operator=} \} \mid \text{操作尝试拷贝} \text{unique_ptr} $$ #### 二、分步解决方案 ##### 1. 显式使用移动语义 ```cpp QList<std::unique_ptr<QObject>> objectList; auto obj = std::make_unique<QObject>(); objectList.push_back(std::move(obj)); // 必须显式转移所有权 ``` ##### 2. 使用C++14及以上标准 在`CMakeLists.txt`中配置: ```cmake set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` 或在Qt Creator的`.pro`文件中添加: ```qmake CONFIG += c++14 ``` ##### 3. 使用emplaceBack替代append ```cpp QList<std::unique_ptr<Contact>> contacts; contacts.emplaceBack(std::make_unique<Contact>("John", "123456")); // C++14+支持 ``` ##### 4. 改用std::vector(推荐) ```cpp std::vector<std::unique_ptr<QWidget>> widgets; widgets.emplace_back(std::make_unique<QWidget>()); // 无需显式移动 ``` #### 三、错误修正公式 $$ \text{安全系数} = \frac{\text{显式使用std::move次数}}{\text{容器操作总数}} \times [\text{容器类型} \in \{\text{std::vector}\}] $$ 建议保持安全系数 ≥ 0.8 #### 四、代码对比示例 ```cpp // 错误示例:隐式拷贝尝试 QList<std::unique_ptr<QFile>> files; files.append(std::make_unique<QFile>("test.txt")); // Qt5触发错误 // 正确示例:C++14+显式移动 QList<std::unique_ptr<QFile>> files; files.push_back(std::make_unique<QFile>("test.txt")); // Qt5需加std::move ``` #### 五、编译器支持矩阵 | 操作 | Qt5 + C++11 | Qt5 + C++14 | Qt6 + C++17 | |----------------------|-------------|-------------|-------------| | QList::append | × | △ | √ | | QList::emplaceBack | × | √ | √ | | std::vector::emplace | √ | √ | √ | (√=完全支持, △=需显式移动, ×=不支持) #### 六、所有权管理流程图 ```mermaid graph TD A[创建unique_ptr] --> B[存入QList] B --> C{是否使用std::move} C -->|是| D[所有权转移成功] C -->|否| E[触发拷贝构造函数] E --> F[编译错误] ```
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值