Skip to content

Commit 8fc1acb

Browse files
committed
improve qml cmake build
1 parent a7a08da commit 8fc1acb

File tree

14 files changed

+87
-90
lines changed

14 files changed

+87
-90
lines changed

CMakeLists.txt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ set(SIMPLE_MAP_VIEW_QT_LIBRARIES Qt::Core Qt::Positioning Qt::Network)
2323

2424
if(SIMPLE_MAP_VIEW_BUILD_QML)
2525

26-
list(APPEND SIMPLE_MAP_VIEW_DEFINITIONS SIMPLE_MAP_VIEW_USE_QML SIMPLE_MAP_VIEW_QML_URI="${SIMPLE_MAP_VIEW_QML_URI}")
26+
list(APPEND SIMPLE_MAP_VIEW_DEFINITIONS SIMPLE_MAP_VIEW_USE_QML)
2727
list(APPEND SIMPLE_MAP_VIEW_QT_COMPONENTS Quick)
2828
list(APPEND SIMPLE_MAP_VIEW_QT_LIBRARIES Qt::Quick)
2929

@@ -40,7 +40,7 @@ file(GLOB_RECURSE SIMPLE_MAP_VIEW_HEADERS include/*.h)
4040
file(GLOB_RECURSE SIMPLE_MAP_VIEW_SOURCES src/*.cpp)
4141

4242
if(SIMPLE_MAP_VIEW_BUILD_TESTS OR SIMPLE_MAP_VIEW_BUILD_PYTHON_BINDINGS)
43-
43+
4444
set(CMAKE_AUTOMOC ON)
4545
set(CMAKE_AUTORCC ON)
4646
set(CMAKE_AUTOUIC ON)
@@ -52,7 +52,26 @@ if(SIMPLE_MAP_VIEW_BUILD_TESTS OR SIMPLE_MAP_VIEW_BUILD_PYTHON_BINDINGS)
5252

5353
endif()
5454

55-
add_library(SimpleMapView STATIC ${SIMPLE_MAP_VIEW_HEADERS} ${SIMPLE_MAP_VIEW_SOURCES})
55+
if(SIMPLE_MAP_VIEW_BUILD_QML)
56+
57+
qt_add_library(SimpleMapView STATIC)
58+
qt_add_qml_module(
59+
SimpleMapView
60+
URI ${SIMPLE_MAP_VIEW_QML_URI}
61+
VERSION ${PROJECT_VERSION}
62+
SOURCES ${SIMPLE_MAP_VIEW_HEADERS} ${SIMPLE_MAP_VIEW_SOURCES}
63+
DEPENDENCIES
64+
QtQuick
65+
QtPositioning
66+
OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/com/github/ozguronsoy/SimpleMapView
67+
)
68+
69+
else()
70+
71+
add_library(SimpleMapView STATIC ${SIMPLE_MAP_VIEW_HEADERS} ${SIMPLE_MAP_VIEW_SOURCES})
72+
73+
endif()
74+
5675
target_compile_definitions(SimpleMapView PUBLIC ${SIMPLE_MAP_VIEW_DEFINITIONS})
5776
target_include_directories(
5877
SimpleMapView
@@ -69,4 +88,4 @@ endif()
6988
if(SIMPLE_MAP_VIEW_BUILD_TESTS)
7089
enable_testing()
7190
add_subdirectory(tests)
72-
endif()
91+
endif()

README.md

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ CMake:
3434
find_package(Qt6 REQUIRED COMPONENTS Core)
3535
qt_standard_project_setup()
3636
37-
set(SIMPLE_MAP_VIEW_USE_QML ON) # if QML app
3837
add_subdirectory(dependencies/SimpleMapView)
3938
4039
set(CMAKE_AUTORCC ON)
@@ -51,8 +50,6 @@ target_link_libraries(mytarget PUBLIC
5150

5251
qmake:
5352
```
54-
SIMPLE_MAP_VIEW_ENABLE_RESOURCES = 1 # optional
55-
SIMPLE_MAP_VIEW_USE_QML = 1 # if QML app
5653
include(dependencies/SimpleMapView/SimpleMapView.pro)
5754
```
5855

@@ -227,38 +224,15 @@ To use the QML component, you need to enable it in your build system first.
227224

228225
CMake:
229226
```cmake
230-
set(SIMPLE_MAP_VIEW_USE_QML ON)
227+
set(SIMPLE_MAP_VIEW_BUILD_QML ON)
231228
```
232229

233230
qmake:
234231
```
235-
SIMPLE_MAP_VIEW_USE_QML = 1
232+
SIMPLE_MAP_VIEW_BUILD_QML = 1
236233
```
237234

238-
Then in your ``main.cpp`` file, you need to register the QML components after creating the ``app`` instance.
239-
240-
Sample main code:
241-
```c++
242-
#include <QGuiApplication>
243-
#include <QQmlApplicationEngine>
244-
#include "SimpleMapView.h"
245-
246-
int main(int argc, char *argv[])
247-
{
248-
QGuiApplication app(argc, argv);
249-
250-
SimpleMapView::registerQmlTypes();
251-
252-
QQmlApplicationEngine engine;
253-
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/yourapp/main.qml")));
254-
if (engine.rootObjects().isEmpty())
255-
return -1;
256-
257-
return app.exec();
258-
}
259-
```
260-
261-
After that you can import and use the QML components in your ``.qml`` files.
235+
After that you can import and use the QML components in your ``QML`` files.
262236

263237
```qml
264238
import QtQuick
@@ -297,11 +271,16 @@ Window {
297271
}
298272
299273
Component.onCompleted: {
300-
map.addMarker(map.center)
274+
var marker = map.addMarker(map.center)
275+
var textItem = SimpleMapViewQmlHelpers.findChild(marker, "MapText")
276+
if (textItem) {
277+
textItem.setText("Marker!")
278+
}
301279
}
302280
}
303281
}
304282
283+
305284
```
306285

307286

@@ -352,4 +331,4 @@ mapView->setTileServer("path/to/offline-tiles");
352331
// mapView->setTileServer(":/SimpleMapView/Tiles");
353332
```
354333

355-
``downloadTiles`` method also generates a ``qrc`` file so one can use the resource system. However, this is not recommended for large maps, as it increases compile time and can significantly bloat the executable.
334+
``downloadTiles`` method also generates a ``qrc`` file so one can use the resource system. However, this is not recommended for large maps, as it increases compile time and can significantly bloat the executable.

SimpleMapView.pro

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@ DEFINES += SIMPLE_MAP_VIEW_VERSION_MAJOR=$$SIMPLE_MAP_VIEW_VERSION_MAJOR
99
DEFINES += SIMPLE_MAP_VIEW_VERSION_MINOR=$$SIMPLE_MAP_VIEW_VERSION_MINOR
1010
DEFINES += SIMPLE_MAP_VIEW_VERSION_PATCH=$$SIMPLE_MAP_VIEW_VERSION_PATCH
1111

12-
isEmpty(SIMPLE_MAP_VIEW_USE_QML) {
12+
isEmpty(SIMPLE_MAP_VIEW_BUILD_QML) {
1313

1414
QT *= gui widgets
1515

1616
} else {
1717

1818
QT *= quick
1919
DEFINES += SIMPLE_MAP_VIEW_USE_QML
20-
DEFINES += SIMPLE_MAP_VIEW_QML_URI=\\\"$$SIMPLE_MAP_VIEW_QML_URI\\\"
2120

21+
QML_IMPORT_NAME = $$SIMPLE_MAP_VIEW_QML_URI
22+
QML_DESIGNER_IMPORT_PATH = $$SIMPLE_MAP_VIEW_QML_URI
23+
QML_IMPORT_MAJOR_VERSION = $$SIMPLE_MAP_VIEW_VERSION_MAJOR
2224
}
2325

2426
INCLUDEPATH += $$PWD/include
2527

2628
HEADERS += $$files($$PWD/include/*.h) $$files($$PWD/include/SimpleMapView/*.h)
2729
SOURCES += $$files($$PWD/src/*.cpp) $$files($$PWD/src/SimpleMapView/*.cpp)
28-
29-
!isEmpty(SIMPLE_MAP_VIEW_ENABLE_RESOURCES) {
30-
31-
RESOURCES += $$PWD/Resources.qrc
32-
33-
}
34-

include/SimpleMapView.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ class SimpleMapView : public SimpleMapViewBase
7373
explicit SimpleMapView(SimpleMapViewBase* parent = nullptr);
7474
~SimpleMapView() = default;
7575

76-
77-
#ifdef SIMPLE_MAP_VIEW_USE_QML
78-
79-
/** Registers the QML types on run-time. Call this after creating the app instance. */
80-
static void registerQmlTypes();
81-
82-
#endif
83-
8476
public slots:
8577
/** Gets the minimum zoom level. */
8678
int minZoomLevel() const;
@@ -301,4 +293,4 @@ public slots:
301293
static constexpr unsigned int DOWNLOAD_MAX_CONCURRENT_REQUEST_COUNT = 10;
302294
};
303295

304-
#endif
296+
#endif

include/SimpleMapView/MapEllipse.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class MapEllipse : public MapItem
1616
Q_PROPERTY(MapSize size READ size WRITE setSize NOTIFY sizeChanged);
1717
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged);
1818

19+
#ifdef SIMPLE_MAP_VIEW_USE_QML
20+
QML_ELEMENT;
21+
#endif
22+
1923
public:
2024
explicit MapEllipse(QObject* parent = nullptr);
2125

@@ -69,4 +73,4 @@ class MapEllipse : public MapItem
6973
QColor m_backgroundColor;
7074
};
7175

72-
#endif
76+
#endif

include/SimpleMapView/MapImage.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class MapImage : public MapRect
1313
Q_PROPERTY(QString image WRITE setImage NOTIFY imageChanged);
1414
Q_PROPERTY(Qt::AspectRatioMode aspectRatioMode READ aspectRatioMode WRITE setAspectRatioMode NOTIFY imageChanged);
1515

16+
#ifdef SIMPLE_MAP_VIEW_USE_QML
17+
QML_ELEMENT;
18+
#endif
19+
1620
public:
1721
explicit MapImage(QObject* parent = nullptr);
1822

@@ -41,4 +45,4 @@ class MapImage : public MapRect
4145
Qt::AspectRatioMode m_aspectRatioMode;
4246
};
4347

44-
#endif
48+
#endif

include/SimpleMapView/MapItem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ class MapItem : public QObject
5858
QPen m_pen;
5959
};
6060

61-
#endif
61+
#endif

include/SimpleMapView/MapLines.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class MapLines : public MapItem
1212
Q_OBJECT;
1313
Q_PROPERTY(QVector<MapPoint> points READ points WRITE setPoints);
1414

15+
#ifdef SIMPLE_MAP_VIEW_USE_QML
16+
QML_ELEMENT;
17+
#endif
18+
1519
public:
1620
explicit MapLines(QObject* parent = nullptr);
1721

@@ -32,4 +36,4 @@ class MapLines : public MapItem
3236
QVector<MapPoint> m_points;
3337
};
3438

35-
#endif
39+
#endif

include/SimpleMapView/MapPolygon.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class MapPolygon : public MapLines
1212
Q_OBJECT;
1313
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged);
1414

15+
#ifdef SIMPLE_MAP_VIEW_USE_QML
16+
QML_ELEMENT;
17+
#endif
18+
1519
public:
1620
explicit MapPolygon(QObject* parent = nullptr);
1721

@@ -29,4 +33,4 @@ class MapPolygon : public MapLines
2933
QColor m_backgroundColor;
3034
};
3135

32-
#endif
36+
#endif

include/SimpleMapView/MapRect.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class MapRect : public MapEllipse
1212
{
1313
Q_OBJECT;
1414

15+
#ifdef SIMPLE_MAP_VIEW_USE_QML
16+
QML_ELEMENT;
17+
#endif
18+
1519
public:
1620
explicit MapRect(QObject* parent = nullptr);
1721

@@ -37,4 +41,4 @@ class MapRect : public MapEllipse
3741
std::array<qreal, 4> m_borderRadii; // top-left, top-right, bottom-right, bottom-left
3842
};
3943

40-
#endif
44+
#endif

0 commit comments

Comments
 (0)