
CONFIG += qwt  #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <qwt_plot.h> #include <qwt_plot_grid.h> #include <qwt_legend.h> #include <qwt_plot_curve.h> #include <qwt_symbol.h> #include <qwt_plot_magnifier.h> #include <qwt_plot_panner.h> #include <qwt_plot_picker.h> #include <qwt_picker_machine.h> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; QwtPlot *d_plot; void setPlot(); QwtPlotGrid *grid; void setPlotGrid(); QwtPlotCurve *curve; QwtSymbol *symbol; void setCurveParameters(); //     double pointArray[5][2]; QPolygonF points; void addPointsToCurveAndShow(); QwtPlotMagnifier *magnifier; void enableMagnifier(); QwtPlotPanner *d_panner; void enableMovingOnPlot(); QwtPlotPicker *d_picker; void enablePicker(); }; #endif // MAINWINDOW_H  #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //        setPlot(); //    setPlotGrid(); //  setCurveParameters(); addPointsToCurveAndShow(); //   /  enableMagnifier(); //      enableMovingOnPlot(); //        //      enablePicker(); } void MainWindow::setPlot() { // (this) -      // #include <qwt_plot.h> d_plot = new QwtPlot( this ); setCentralWidget(d_plot); //      d_plot->setTitle( "Qwt demonstration" ); //  d_plot->setCanvasBackground( Qt::white ); //   //    d_plot->setAxisTitle(QwtPlot::yLeft, "Y"); d_plot->setAxisTitle(QwtPlot::xBottom, "X"); d_plot->insertLegend( new QwtLegend() ); } void MainWindow::setPlotGrid() { // #include <qwt_plot_grid.h> grid = new QwtPlotGrid(); grid->setMajorPen(QPen( Qt::gray, 2 )); //     grid->attach( d_plot ); //      } void MainWindow::setCurveParameters() { //#include <qwt_plot_curve.h> curve = new QwtPlotCurve(); curve->setTitle( "Demo Curve" ); curve->setPen( Qt::blue, 6 ); //     curve->setRenderHint ( QwtPlotItem::RenderAntialiased, true ); //  //   // #include <qwt_symbol.h> symbol = new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) ); curve->setSymbol( symbol ); } void MainWindow::addPointsToCurveAndShow() { //       //     ,   //    for (int i = 0; i < 5; i++) { pointArray[i][0] = 1.0 + 0.5*i; pointArray[i][1] = 1.0 + 0.5*i; points << QPointF( pointArray[i][0], pointArray[i][1]); } curve->setSamples( points ); //      curve->attach( d_plot ); //     } void MainWindow::enableMagnifier() { // #include <qwt_plot_magnifier.h> magnifier = new QwtPlotMagnifier(d_plot->canvas()); // ,  / magnifier->setMouseButton(Qt::MidButton); } void MainWindow::enableMovingOnPlot() { // #include <qwt_plot_panner.h> d_panner = new QwtPlotPanner( d_plot->canvas() ); // ,   d_panner->setMouseButton( Qt::RightButton ); } void MainWindow::enablePicker() { // #include <qwt_plot_picker.h> //   d_picker = new QwtPlotPicker( QwtPlot::xBottom, QwtPlot::yLeft, //    QwtPlotPicker::CrossRubberBand, //    QwtPicker::AlwaysOn, //   d_plot->canvas() ); //    //    d_picker->setRubberBandPen( QColor( Qt::red ) ); //     d_picker->setTrackerPen( QColor( Qt::black ) ); //     d_picker->setStateMachine( new QwtPickerDragPointMachine() ); } MainWindow::~MainWindow() { delete ui; }  void setStatusBar();  void MainWindow::setStatusBar() { #ifndef QT_NO_STATUSBAR ( void )statusBar(); #endif }  setStatusBar();  private Q_SLOTS: void click_on_canvas( const QPoint &pos );  void MainWindow::click_on_canvas( const QPoint &pos ) { //     double x = d_plot->invTransform(QwtPlot::xBottom, pos.x()); double y = d_plot->invTransform(QwtPlot::yLeft, pos.y()); QString info = "x= " + QString::number(x) + "; y = " + QString::number(y); //      statusBar()->showMessage(info); }  //     d_picker,    d_plot! connect( d_picker, SIGNAL( appended( const QPoint & ) ), SLOT( click_on_canvas( const QPoint & ) ) ); 
 QToolBar *toolBar;  void setToolBar ();  void MainWindow::setToolBar() { toolBar = new QToolBar( this ); addToolBar( toolBar ); }  setToolBar();  #include <QToolButton>  QToolButton *toolButton; void addCorrectionButton();  void MainWindow::addCorrectionButton() { toolButton = new QToolButton( toolBar ); toolButton->setText( "Change x" ); toolButton->setCheckable( true ); toolBar->addWidget( toolButton ); //      }  addCorrectionButton();  #include <QHBoxLayout> #include <qwt_counter.h>  QWidget *hBox; QHBoxLayout *layout; QwtCounter *cntDamp; void addQwtCounter();  void MainWindow::addQwtCounter() { //     cntDamp = new QwtCounter(); cntDamp->setRange( -50, 50 ); //        //          cntDamp->setSingleStep( 1.0 ); cntDamp->setValue( 0 ); //   cntDamp->setEnabled(true); //      //   hBox = new QWidget(); // "",      //     QWidget. layout = new QHBoxLayout( hBox ); //      layout->addWidget(cntDamp); //           layout->addWidget( new QWidget(hBox) , 10 ); // spacer //        ( void )toolBar->addWidget( hBox ); }  addQwtCounter(); 
 void setPlotCorrection( double coeff );  double changeXValue;  void MainWindow::setPlotCorrection( double coeff ) { changeXValue = coeff; }  changeXValue = 0.0; connect( cntDamp, SIGNAL( valueChanged( double ) ), SLOT( setPlotCorrection( double ) ) );  void changeX();  void MainWindow::changeX() { //    points.clear(); for (int i = 0; i < 5; i++) { pointArray[i][0] += changeXValue; points << QPointF( pointArray[i][0], pointArray[i][1]); } curve->setSamples( points ); //      d_plot->replot(); }  connect( toolButton, SIGNAL(toggled(bool)), SLOT( changeX() ) ); Source: https://habr.com/ru/post/211867/
All Articles