在 Qt 6 中找不到 QtCore/QTextCodec

Hao*_*Xie 10 c++ qt qt6

升级到Qt 6.0后,编译器告诉我

qzxing/src/QZXing.cpp:16: error: 'QtCore/QTextCodec' file not found
qzxing/src/QZXing.cpp:16:10: fatal error: 'QtCore/QTextCodec' file not found
#include <QtCore/QTextCodec>
         ^~~~~~~~~~~~~~~~~~~
qzxing/src/QZXing.cpp:16:10: note: did not find header 'QTextCodec' in framework 'QtCore' (loaded from '/Applications/Qt/6.0.0/clang_64/lib')
Run Code Online (Sandbox Code Playgroud)

根据Qt的文档,可以通过添加QT += core5compat. 然而,编译器告诉我“QT 中的未知模块:core5compat”。

如何解决这个问题呢?

Hao*_*Xie 13

  1. 确保您已安装“Qt 5 兼容性模块”。
  2. 添加QT += core5compat到.pro 文件中。
  3. 替换#include <QtCore/QTextCodec>#include <QTextCodec>

Qt安装程序

  • 由于 QTextCodec 现在位于兼容性库中,是否有替代类? (3认同)

eyl*_*esc 7

QTextCodec 类已移至 core5compat 子模块,因此将其添加到 .pro 中还不够,但您必须将导入更正为:

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    #include <QtCore/QTextCodec>
#else
    #include <QtCore5Compat/QTextCodec>
#endif
Run Code Online (Sandbox Code Playgroud)

或者简单地

#include <QTextCodec>
Run Code Online (Sandbox Code Playgroud)

另一方面,您必须安装此模块,因为它不是默认提供的,为此您必须使用维护工具

  • 由于 QTextCodec 现在位于兼容性库中,是否有替代类? (2认同)
  • @albert QStringConverter 是 QTextCodec 的替换类。 (2认同)