add example and explanation for use of CMake's FetchCOnten-API

This commit is contained in:
jkriege2 2024-01-17 20:42:12 +01:00
parent 9e5737624c
commit f7cbb1dea8
9 changed files with 276 additions and 12 deletions

View File

@ -83,7 +83,7 @@ The [Screenshots-page](./screenshots/) contains several screenshots, partly take
[![EXAMPLES-Page](./screenshots/screenshotsbanner.png)](./screenshots/README.md)
## Building
## Building Using CMake
[![Lates Release](https://img.shields.io/github/v/release/jkriege2/JKQtPlotter)](https://github.com/jkriege2/JKQtPlotter/releases)
@ -110,6 +110,27 @@ or on a Qt-version agnostic way via:
```
See https://jkriege2.github.io/JKQtPlotter/page_buildinstructions__c_m_a_k_e.html for details.
## Usage via CMake's FetchConten-API
In addition to the method described above (i.e. build and install the library and then use it), you can also use JKQTPlotter via CMake's [FetchContent-API](https://cmake.org/cmake/help/latest/module/FetchContent.html).
For this method, you need to add these lines to your CMake project:
```
include(FetchContent) # once in the project to include the module
# ... now declare JKQTPlotter5/6
FetchContent_Declare(JKQTPlotter${QT_VERSION_MAJOR}
GIT_REPOSITORY https://github.com/jkriege2/JKQtPlotter.git
# GIT_TAG v5.0.0)
# ... finally make JKQTPlotter5/6 available
FetchContent_MakeAvailable(JKQTPlotter${QT_VERSION_MAJOR})
```
These declare JKQTPlotter and make it available in your project. Afterwards you should be able to link against it, using
```
target_link_libraries(${PROJECT_NAME} JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})
```
## Stargazers over time
[![Stargazers over time](https://starchart.cc/jkriege2/JKQtPlotter.svg)](https://starchart.cc/jkriege2/JKQtPlotter)

View File

@ -31,6 +31,15 @@ environment:
matrix:
- QTABI: gcc_64
COMPILER: GCC
QTVER: 6.4
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
CMAKE_GENERATOR: "Unix Makefiles"
INSTALL_QMAKE5: true
TEST_FETCHCONTENT: true
- QTABI: gcc_64
COMPILER: GCC
QTVER: 5.15
@ -38,6 +47,7 @@ environment:
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
CMAKE_GENERATOR: "Unix Makefiles"
INSTALL_QMAKE5: true
TEST_FETCHCONTENT: false
- QTABI: msvc2019_64
COMPILER: MSVC
@ -45,6 +55,7 @@ environment:
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_GENERATOR: "Visual Studio 16 2019"
CMAKE_BUILDFLAGS: /verbosity:minimal /maxcpucount
TEST_FETCHCONTENT: false
- QTABI: msvc2019_64
COMPILER: MSVC
@ -52,6 +63,7 @@ environment:
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_GENERATOR: "Visual Studio 16 2019"
CMAKE_BUILDFLAGS: /verbosity:minimal /maxcpucount
TEST_FETCHCONTENT: false
- QTABI: gcc_64
COMPILER: GCC
@ -59,6 +71,7 @@ environment:
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
CMAKE_GENERATOR: "Unix Makefiles"
INSTALL_QMAKE5: true
TEST_FETCHCONTENT: false
- QTABI: gcc_64
COMPILER: GCC
@ -66,12 +79,14 @@ environment:
APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu2004
CMAKE_GENERATOR: "Unix Makefiles"
INSTALL_QMAKE5: true
TEST_FETCHCONTENT: false
- QTABI: macos
COMPILER: CLANG
QTVER: 6.4
APPVEYOR_BUILD_WORKER_IMAGE: macos-monterey
CMAKE_GENERATOR: "Unix Makefiles"
TEST_FETCHCONTENT: false
for:
@ -203,37 +218,49 @@ for:
- sh: cd $APPVEYOR_BUILD_FOLDER
- sh: mkdir build
- sh: mkdir install
- sh: cd build
- sh: |
if [ "$USE_CMAKE" = true ]; then
if [ "$USE_CMAKE" = true ] && [ "$TEST_FETCHCONTENT" = false ]; then
cd build
echo --- Run CMake Configure -----------------------------------------------------------------------------
cmake --version
cmake -G "$CMAKE_GENERATOR" "-DCMAKE_PREFIX_PATH=$QTDIR;$CIMG_INCLUDE_DIR" "-DCMAKE_INSTALL_PREFIX=$APPVEYOR_BUILD_FOLDER/install" ..
echo --- Build using CMake -------------------------------------------------------------------------------
cmake --build . --config "$CONFIGURATION" -j$(getconf _NPROCESSORS_ONLN) -- $CMAKE_BUILDFLAGS
fi
if [ "$USE_CMAKE" = true ] && [ "$TEST_FETCHCONTENT" = true ]; then
cd examples/cmake_fetchcontent_example
mkdir build
cd build
echo --- Run CMake Configure with FetchCOntent -----------------------------------------------------------
cmake --version
cmake -G "$CMAKE_GENERATOR" "-DCMAKE_PREFIX_PATH=$QTDIR;$CIMG_INCLUDE_DIR" "-DCMAKE_INSTALL_PREFIX=$APPVEYOR_BUILD_FOLDER/install" ..
echo --- Build using CMake with FetchCOntent -------------------------------------------------------------
cmake --build . --config "$CONFIGURATION" -j$(getconf _NPROCESSORS_ONLN) -- $CMAKE_BUILDFLAGS
fi
- sh: |
if [ "$USE_CMAKE" = false ]; then
cd build
echo --- Run QMake Configure -----------------------------------------------------------------------------
$QTDIR/bin/qmake -v
$QTDIR/bin/qmake -makefile -o Makefile "CONFIG+=%CONFIGURATION%" ../JKQtPlotterAppveyorBuild.pro
echo --- Build for QMake ---------------------------------------------------------------------------------
make -j$(getconf _NPROCESSORS_ONLN)
fi
- sh: echo == INSTALL JKQtPlotter ==========================================================================
- sh: cd $APPVEYOR_BUILD_FOLDER
- sh: cd build
- sh:
- sh: |
if [ "$USE_CMAKE" = true ]; then
if [ "$USE_CMAKE" = true ] && [ "$TEST_FETCHCONTENT" = false ]; then
echo == INSTALL JKQtPlotter ==========================================================================
cd $APPVEYOR_BUILD_FOLDER
cd build
echo --- Install using CMake -------------------------------------------------------------------------------
cmake --install . --config "$CONFIGURATION"
fi
after_build:
- sh: echo == TEST USE JKQtPlotter CMAKE BUILD ============================================================
- sh: |
if [ "$USE_CMAKE" = true ]; then
if [ "$USE_CMAKE" = true ] && [ "$TEST_FETCHCONTENT" = false ]; then
echo == TEST USE JKQtPlotter CMAKE BUILD ============================================================
cd $APPVEYOR_BUILD_FOLDER
cd examples/cmake_link_example
mkdir build

View File

@ -179,4 +179,25 @@ Then you can again build the example either using the generated builf files (e.g
$ cmake --build . --config "Debug" --target install
\endcode
\section page_buildinstructions_CMAKE_FETCHCONTENT Using JKQTPlotter with CMake's FetchContent-API
In addition to the method described above (i.e. build and install the library and then use it), you can also use JKQTPlotter via CMake's <a href="https://cmake.org/cmake/help/latest/module/FetchContent.html">FetchContent-API</a>. Also have a look at <a href="https://www.foonathan.net/2022/06/cmake-fetchcontent/">this blog post</a> for a detailed explanation. Also see \ref JKQTCMakeFetchContentExample for a detailed example.
For this method, you need to add these lines to your CMake project:
\code{.cmake}
include(FetchContent) # once in the project to include the module
# ... now declare JKQTPlotter5/6
FetchContent_Declare(JKQTPlotter${QT_VERSION_MAJOR}
GIT_REPOSITORY https://github.com/jkriege2/JKQtPlotter.git
# GIT_TAG v5.0.0)
# ... finally make JKQTPlotter5/6 available
FetchContent_MakeAvailable(JKQTPlotter${QT_VERSION_MAJOR})
\endcode
These declare JKQTPlotter and make it available in your project. Afterwards you should be able to link against it, using
\code{.cmake}
target_link_libraries(${PROJECT_NAME} JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})
\endcode
*/

View File

@ -288,7 +288,7 @@ All test-projects are Qt-projects that use qmake to build. You can load them int
</table>
\section jkqtp_extut_jkqtfastplotter Examples for JKQTFastPlotter
\section jkqtp_extut_jkqtfastplotter DEPRECATED: Examples for JKQTFastPlotter
<table>
<tr><th> Screenshot <th> Description <th> Notes

View File

@ -1,6 +1,6 @@
/*!
\defgroup jkqtfastplotter JKQTFastPlotter: Speed-Optimized Plotter class
\defgroup jkqtfastplotter DEPRECATED: JKQTFastPlotter: Speed-Optimized Plotter class
\deprecated The class JKQTFastPlotter and all its support classes are deprecated and will be removed in future versions.

View File

@ -0,0 +1,54 @@
# set minimum required CMake-Version
cmake_minimum_required(VERSION 3.23)
# set Project name
project(simpletest_cmake LANGUAGES CXX)
# some basic configurations
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Configure project for usage of Qt5/Qt6
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Gui Widgets PrintSupport Svg Xml OpenGl REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Widgets PrintSupport Svg Xml OpenGL REQUIRED)
# include JKQTPlotter via FetchContent-API:
# ... first load the FetchContent-API:
include(FetchContent) # once in the project to include the module
# ... now declare JKQTPlotter5/6
FetchContent_Declare(JKQTPlotter${QT_VERSION_MAJOR}
GIT_REPOSITORY https://github.com/jkriege2/JKQtPlotter.git
# GIT_TAG v5.0.0)
# ... finally make JKQTPlotter5/6 available
FetchContent_MakeAvailable(JKQTPlotter${QT_VERSION_MAJOR})
# For Visual Studio, we need to set some additional compiler options
if(MSVC)
add_compile_options(/EHsc)
# To enable M_PI, M_E,...
add_definitions(/D_USE_MATH_DEFINES)
# To Prevent Errors with min() and max()
add_definitions(/DNOMINMAX)
# To fix error: C2338: va_start argument must not
# have reference type and must not be parenthesized
add_definitions(/D_CRT_NO_VA_START_VALIDATION)
endif()
# add the example executable
add_executable(${PROJECT_NAME} WIN32 simpletest.cpp)
# ... link against Qt5/6 and JKQTPlotterLib
# (you could use JKQTPlotterSharedLib if you don't want to link againast the
# static version, but against the shared/DLL version).
target_link_libraries(${PROJECT_NAME} PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::PrintSupport Qt${QT_VERSION_MAJOR}::Svg Qt${QT_VERSION_MAJOR}::Xml)
# ... link against JKQTPlotter: As the Targets contain the Qt-Version-Number in their names, we can
# link against 'JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR}' and it works
# for Qt5 AND Qt6 ...
# if you have a speific Qt-Version, you can also write e.g. 'JKQTPlotter6::JKQTPlotter6'
target_link_libraries(${PROJECT_NAME} PUBLIC JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})
# Installation
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

View File

@ -0,0 +1,87 @@
# Example (JKQTPlotter): CMake FetchContent Example {#JKQTCMakeFetchContentExample}
This project (see [`cmake_fetchcontent_example`](https://github.com/jkriege2/JKQtPlotter/tree/master/examples/cmake_fetchcontent_example) demonstrates how to link against JKQTPlotter using CMake's [FetchContent-API](https://cmake.org/cmake/help/latest/module/FetchContent.html). Also have a look at [this blog post](https://www.foonathan.net/2022/06/cmake-fetchcontent/) for a detailed explanation. See https://jkriege2.github.io/JKQtPlotter/page_buildinstructions__c_m_a_k_e.html for details on how to build JKQTPlotter with CMake
This example uses very simple code, which simply displays a plotter and shows some data. The important part of this example is the ´CMakeLists.txt`-file:
```
# set minimum required CMake-Version
cmake_minimum_required(VERSION 3.23)
# set Project name
project(simpletest_cmake LANGUAGES CXX)
# some basic configurations
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Configure project for usage of Qt5/Qt6
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Gui Widgets PrintSupport Svg Xml OpenGl REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Widgets PrintSupport Svg Xml OpenGL REQUIRED)
# include JKQTPlotter via FetchContent-API:
# ... first load the FetchContent-API:
include(FetchContent) # once in the project to include the module
# ... now declare JKQTPlotter5/6
FetchContent_Declare(JKQTPlotter${QT_VERSION_MAJOR}
GIT_REPOSITORY https://github.com/jkriege2/JKQtPlotter.git
# GIT_TAG v5.0.0)
# ... finally make JKQTPlotter5/6 available
FetchContent_MakeAvailable(JKQTPlotter${QT_VERSION_MAJOR})
# For Visual Studio, we need to set some additional compiler options
if(MSVC)
add_compile_options(/EHsc)
# To enable M_PI, M_E,...
add_definitions(/D_USE_MATH_DEFINES)
# To Prevent Errors with min() and max()
add_definitions(/DNOMINMAX)
# To fix error: C2338: va_start argument must not
# have reference type and must not be parenthesized
add_definitions(/D_CRT_NO_VA_START_VALIDATION)
endif()
# add the example executable
add_executable(${PROJECT_NAME} WIN32 simpletest.cpp)
# ... link against Qt5/6 and JKQTPlotterLib
# (you could use JKQTPlotterSharedLib if you don't want to link againast the
# static version, but against the shared/DLL version).
target_link_libraries(${PROJECT_NAME} PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::PrintSupport Qt${QT_VERSION_MAJOR}::Svg Qt${QT_VERSION_MAJOR}::Xml)
# ... link against JKQTPlotter: As the Targets contain the Qt-Version-Number in their names, we can
# link against 'JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR}' and it works
# for Qt5 AND Qt6 ...
# if you have a speific Qt-Version, you can also write e.g. 'JKQTPlotter6::JKQTPlotter6'
target_link_libraries(${PROJECT_NAME} PUBLIC JKQTPlotter${QT_VERSION_MAJOR}::JKQTPlotter${QT_VERSION_MAJOR})
# Installation
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
```
The important steps here are
```
include(FetchContent) # once in the project to include the module
# ... now declare JKQTPlotter5/6
FetchContent_Declare(JKQTPlotter${QT_VERSION_MAJOR}
GIT_REPOSITORY https://github.com/jkriege2/JKQtPlotter.git
# GIT_TAG v5.0.0)
# ... finally make JKQTPlotter5/6 available
FetchContent_MakeAvailable(JKQTPlotter${QT_VERSION_MAJOR})
```
where JKQTPlotter is first declared to the FetchContent-API and then loaded.
To build this example, you first need to make a subdirectory `build` and then call CMake form that subdirectory:
```.sh
$ mkdir build
$ cd build
$ cmake .. -G "<GENERATOR>" "-DCMAKE_PREFIX_PATH=<path_to_your_qt_sources>"
cmake --build . --config "Debug"
```
In the configure step above, you need to specify the directory `<path_to_your_qt_sources>` of yout Qt installation and the `<GENERATOR>` appropriate for you use-case.

View File

@ -0,0 +1,54 @@
/** \example jkqtplotter_simpletest.cpp
* A very basic example for the usage of JKQTPlotter, using CMake
*
* \ref JKQTCMakeLinkExample
*/
#include <QApplication>
#include "jkqtplotter/jkqtplotter.h"
#include "jkqtplotter/graphs/jkqtpscatter.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
JKQTPlotter plot;
JKQTPDatastore* ds=plot.getDatastore();
// 2. now we create data for a simple plot (a sine curve)
QVector<double> X, Y;
const int Ndata=100;
for (int i=0; i<Ndata; i++) {
const double x=double(i)/double(Ndata)*8.0*M_PI;
X<<x;
Y<<sin(x);
}
// 3. make data available to JKQTPlotter by adding it to the internal datastore.
// Note: In this step the data is copied (of not specified otherwise), so you can
// reuse X and Y afterwards!
// the variables columnX and columnY will contain the internal column ID of the newly
// created columns with names "x" and "y" and the (copied) data from X and Y.
size_t columnX=ds->addCopiedColumn(X, "x");
size_t columnY=ds->addCopiedColumn(Y, "y");
// 4. create a graph in the plot, which plots the dataset X/Y:
JKQTPXYLineGraph* graph1=new JKQTPXYLineGraph(&plot);
graph1->setXColumn(columnX);
graph1->setYColumn(columnY);
graph1->setTitle(QObject::tr("sine graph"));
// 5. add the graph to the plot, so it is actually displayed
plot.addGraph(graph1);
// 6. autoscale the plot so the graph is contained
plot.zoomToFit();
// show plotter and make it a decent size
plot.show();
plot.resize(600,400);
return app.exec();
}

View File

@ -1,6 +1,6 @@
# Example (JKQTPlotter): CMake Example {#JKQTCMakeLinkExample}
This project (see [`cmake_link_example`](https://github.com/jkriege2/JKQtPlotter/tree/master/examples/cmake_link_example) demonstrates how to link against JKQTPlotter using CMake. See http://jkriege2.github.io/JKQtPlotter/page_buildinstructions.html for details on how to build JKQTPlotter with CMake
This project (see [`cmake_link_example`](https://github.com/jkriege2/JKQtPlotter/tree/master/examples/cmake_link_example) demonstrates how to link against JKQTPlotter using CMake. See https://jkriege2.github.io/JKQtPlotter/page_buildinstructions__c_m_a_k_e.html for details on how to build JKQTPlotter with CMake
This example uses very simple code, which simply displays a plotter and shows some data. The important part of this example is the ´CMakeLists.txt`-file:
```