整理了使用qt中遇到的问题以及解决方案。这些方案并不总是能解决问题,只是提供一个排查问题的方向。

一、compile

1 无法解析的外部符号

原因分析:
1.类内使用了信号与槽,但未包含 Q_OBJECT 宏,因此需在类内加上Q_OBJECT
2.所需的库.lib文件未包含到项目;编译使用的64bit,但引用的库是32bit的;
3.项目未包含所需Qt模块;
4.类中定义了slot或函数,却没有实现。

2

使用QT同时编译pcl和opencv出现error: field ‘pa ram_k_’ has incomplete type ‘flann::SearchParams’
错误原因:pcl和opencv中均包含flann库。而我在引用库时习惯性把整个库包含进来,导致出现两个名称相同的flann库,于是产生了这个错误。
解决方案:不要把整个大库包含进来,只包含需要的部分。此处我使用了pcl的flann库,然后包含opencv中需要的那部分。

3

现象使用CMake编译和运行qt的程序,运行时遇到了提示:

Unknown property font-color 

错误原因:Qt样式表中设置字体颜色应该使用color属性,而不是font-color

解决方案:运行命令grep -rnw './' -e 'font-color',查看当前目录及其子目录中搜索包含font-color的所有文件以及行号。结果在*.ui文件中。打开对应的*.ui文件,把font-color替换为color

4 Qt 中文乱码问题

Windows系统下直接用VS ide的话,默认的编码格式是GBK。用VS ide保存为UTF-8,很多人很多人保存成了UTF-8带BOM。Linux系统下默认的编码都是 UTF-8不带bom。因此为了不出现乱码,保存时最好设置为UTF-8不带bom的编码格式。在使用qtcreator时,需要把behavior->文件编码->UTF8 BOM设置为总是删除

5

I use CMakeLists.txt to configure compilation. It occurred during compilation: undefined reference to 'QVTKWidget::QVTKWidget'. Solution

# judge whether here is libvtkGUISupportQt
ls /usr/lib/x86_64-linux-gnu/ | grep libvtkGUISupportQt
# if result isn't empty, continue

add /usr/lib/x86_64-linux-gnu/libvtkGUISupportQt-7.1.so to target_link_libraries in CMakeLists.txt.

6

I got error when I compiled qt project

fatal error: qpa/qplatformnativeinterface.h: No such file or directory
   45 | #include <qpa/qplatformnativeinterface.h>

qpa/qplatformnativeinterface.h is a private header file. Run sudo apt-get install qtbase5-private-dev to install it.

7

old code

QWidget *widget = QWidget::find(reinterpret_cast<WId>(cm->window));

error

error: invalid cast from type ‘xcb_window_t’ {aka ‘unsigned int’} to type ‘WId’ {aka ‘long long unsigned int’}

corrected code

WId windowId = static_cast<WId>(cm->window);
QWidget *widget = QWidget::find(windowId);
8

error:

fatal error: QX11Info: No such file or directory

install x11

sudo apt install libqt5x11extras5-dev

changes in CMakeLists.txt

find_package(Qt5 REQUIRED COMPONENTS Core Gui X11Extras)
target_link_libraries(your_target_name PRIVATE Qt5::Core Qt5::Gui Qt5::X11Extras)

changes in code

#include <QX11Info>
9

old code

 xcb_generic_event_t *event = static_cast<xcb_generic_event_t *>(message);
xcb_client_message_event_t *cm = static_cast<xcb_client_message_event_t *>(event);

error

error: invalid static_cast from type ‘xcb_generic_event_t*’ to type ‘xcb_client_message_event_t*

xcb_client_message_event_t is a specific type of XCB event, and it cannot be safely cast directly from the generic event type without checking the event type first. To resolve this, you need to handle the event correctly by checking the event type and then casting it appropriately.

corrected code

xcb_generic_event_t *event = static_cast<xcb_generic_event_t *>(message);
xcb_client_message_event_t *cm = reinterpret_cast<xcb_client_message_event_t *>(event);
10

error:

Unknown CMake command "qt5_wrap_ui"

add code below to CMakeLists.txt

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5 REQUIRED COMPONENTS Widgets)
11

写了一个ros包,用qt制作界面,编译时遇到如下错误。已经检查过了,所用到的头文件和可执行文件都正确写进了CmakeLists.txt文件。

undefined reference to `vtable for MainWindow'

add code below to CMakeLists.txt

### other statements ###
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
### other statements ###
qt5_wrap_cpp(MOC_FILES include/display/mainwindow.h)
add_executable(map_test_node src/map_test_node.cpp ${MOC_FILES} src/display/mainwindow.cpp)
### other statements ###
12

在C++中使用qt并使用cmake构建,编译时遇到错误

"undefined reference to `vtable for MainWindow'"
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED) 
set(SOURCES 
  main.cpp
  myclass.cpp
  myclass.h
)
qt5_wrap_cpp(MOC_FILES myclass.h) # Call qt5_wrap_cpp() on any Qt header files to run moc automatically
add_executable(myApp ${SOURCES} ${MOC_FILES})
target_link_libraries(myApp Qt5::Core Qt5::Gui Qt5::Widgets)

The key points are:

  1. Use qt5_wrap_cpp() to run moc on Qt header files;
  2. This generates moc_.cpp source files;
  3. Add these generated moc files to your sources;
  4. Link against Qt5 libraries;
  5. Set CMAKE_AUTOMOC and CMAKE_AUTOUIC if using newer CMake versions

run

1
qRegisterMetaType<std::string>("std::string");

qRegisterMetaType is a Qt function that registers a new meta type in the Qt meta-object system.

In the case of qRegisterMetaType<std::string>(“std::string”);, you’re registering the C++ standard library type std::string with the meta-object system so that it can be used, for example, as an argument type in signals and slots. By default, only Qt types (like QString) are recognized, so if you want to use a non-Qt type like std::string, you need to register it first.

other