qcustomplot 绘图 1

周末,闲读,翻翻qcustomplot 例子。

以前用qchart, 实在无法接收这个类的低效。打开qchart, 仿佛在推一个滞重的铁门。qcustomplot 感觉轻快灵动。

1. 准备CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(plot_0 VERSION 0.1 LANGUAGES CXX)

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

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets PrintSupport)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets PrintSupport)

set(PROJECT_SOURCES
        main.cpp
        window.cpp
        window.h
        ../plot/qcustomplot.h
        ../plot/qcustomplot.cpp
)

qt_add_executable(plot_0
    MANUAL_FINALIZATION
    ${PROJECT_SOURCES}
)

target_link_libraries(plot_0 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::PrintSupport)

set_target_properties(plot_0 PROPERTIES
    WIN32_EXECUTABLE TRUE
)

install(TARGETS plot_0
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

 

绘制一个基本图:

   QWidget *w = new QWidget;
   QCustomPlot *plot = new QCustomPlot(w);
   ... 
   //设置窗口显示,属于qt 日常操作
   QVBoxLayout *layout = new QVBoxLayout(w);
   layout->setSpacing(6);
   layout->setContentsMargins(11, 11, 11, 11);
   layout->addWidget(plot);
   ...

    plot->addGraph();
    plot->graph(0)->setData(x, y);

    plot->xAxis->setLabel("x");
    plot->yAxis->setLabel("y");

    plot->xAxis->setRange(-1, 1);
    plot->yAxis->setRange(0, 1);

 

posted on 2023-06-17 11:25  金凯旋  阅读(39)  评论(0编辑  收藏  举报

导航