0%

How to Build and Use user-defined QT Library

Tutorial

build qt library

QObject class

1
2
3
4
5
6
7
8
class  SHARED_EXPORT CameraGrabber : public QObject
{
Q_OBJECT
signals :
void sendFrameTrash(int nCameraID, int nTotalTrashFrame);
public:
explicit CameraGrabber(QObject *parent = 0);
}

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON) # for qt SLOTS

add_library(${CAMERA_GRABBER_TARGET}
CaptureProcess.h
CaptureProcess.cpp
FindGrabber.h
FindGrabber.cpp
CameraGrabber.h
CameraGrabber.cpp
)

qt5_use_modules(${CAMERA_GRABBER_TARGET} Core)

target_link_libraries (${CAMERA_GRABBER_TARGET}
${CONGIF_TARGET}
${SAPERA_LIBRARIES}
)

build and get CameraGrabber.h,CameraGrabber.lib,CameraGrabber.dll and mocs_compilation.cpp.

use qt library

MainWindow.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Sensor/CameraGrabber/CameraGrabber.h" 

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent=0);
~MainWindow();

private:
CameraGrabber *graber = nullptr;
}

MainWindow.cpp

1
2
3
4
5
#include "mainwindow.h"
#include "ui_mainwindow.h"

// moc cpp files for QObject
#include "build/src/car/Sensor/CameraGrabber/MySensorCameraGrabber_autogen/mocs_compilation.cpp"

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON) # for qt project

#==============================================================
# cpp,ui,resource files
#==============================================================
# cpp files
#aux_source_directory(. SRC_LIST)
set(SRC_LIST
main.cpp
mainwindow.h
mainwindow.cpp
)

# ui files
qt5_wrap_ui(ui_FILES mainwindow.ui)

# resource files
qt5_add_resources(qrc_FILES resource.qrc)

message( [Main] ${SRC_LIST} )
message( [Main] ${ui_FILES} )
message( [Main] ${qrc_FILES} )

#==============================================================
# generate and link target for point_cloud_viewer
#==============================================================
set(TARGET_NAME aa_qt_main)
add_executable(${TARGET_NAME} ${SRC_LIST} ${ui_FILES} ${qrc_FILES})

# link qt libraries
qt5_use_modules(${TARGET_NAME} Core Widgets OpenGL Xml Gui Sql)

# link vtk and pcl libraries
target_link_libraries(${TARGET_NAME}
${CONFIG_TARGET}
${UTIL_TARGET}
${MODEL_TARGET}
${DETECTION_TARGET}
${DATABASE_TARGET}

# sensor
${CODER_LINKER_TARGET}
${RFID_TARGET}
${CAMERA_GRABBER_TARGET}
${LIDAR_GRABBER_TARGET}

${GTEST_LIBRARIES}
${BOOST_THREAD_LIBRARY}
${GLOG_LIBRARIES}

${Boost_LIBRARIES}
${OpenCV_LIBS}
${PCL_LIBRARIES}
)

tips

If we reference qt dll which use QObject,when we include header in MainWindow.h,we must also include mocs_compilation.cpp in MainWindow.cpp. Otherwise error will occur:

error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const staticMetaObject

Reference

History

  • 20180409: created.