📜 ⬆️ ⬇️

Qwt library: how to plot a function on a plane?

Screenshot of the simplest program demonstrating the use of Qwt widgets
For five years, I have not written applications with the GUI, because the proposal head. department to prepare for his lecture program, building some graphics, at first I was somewhat upset.

As a real programmer, I decided to find the easiest (and useful for self-development) path.
Namely, this task turned out to be a good reason to get acquainted with Qt and the Qwt library. At the same time, I learned that applications with GUI, can turn out to be cross-platform, and their code is no less elegant than that of applications with a “Unix-style interface”.

So, Qwt is a library of widgets for programming applications that have a technical focus. It contains a set of widgets that represent all kinds of sliders and disk “dialers”, widgets for the construction of histograms. But here I will tell you how to use Qwt to build the most ordinary two-dimensional graphs of functions of the form y = f (x) .
')
Further in this article


The simplest example and its compilation


For this note, I have prepared a very small example that can be used as a starting point when studying Qwt.

The download link (308 KB) can be obtained from the tgz-archive

You can compile these binaries from source, with the following commands:

Linux:

Windows:

Please note that in the source directory is the qwt.prf file from the original qwt.prf . Since I use the static version of the library, the QwtBuild variable QwtBuild assigned an empty value. In addition, in the win32 section, the value of the QwtBase variable QwtBase changed to the path to the directory in which I have installed the library.

Go through the source


In Qwt, everything is simple. There is a drawing area widget - QwtPlot . Several curves can be associated with it - QwtPlotCurve objects. Actually, that's all.

Class Description (QwtBeginner.h file)

In my example, the application window is an instance of the QwtBeginner class, which is a descendant of the QWidget class.

It includes two QwtPlot drawing QwtPlot : function graphs will display function graphs on derPlot , and derivatives of them graphs on derPlot .

In total, four curves will be constructed: the sine function and the cubic function ( sinFunCurve , cubFunCurve ) and their derivatives ( sinDerCurve , cubDerCurve ).

The sinButton and cubButton buttons will turn on and off the display of the corresponding graphs, and sinToggled() and cubToggled() slots will be attached to the signals of which.
class QwtBeginner : public QWidget<br>{<br> Q_OBJECT<br><br> public :<br> QwtBeginner(QWidget *parent = 0);<br><br> private :<br> QwtPlot *funPlot, *derPlot;<br> QwtPlotCurve *sinFunCurve, *sinDerCurve;<br> QwtPlotCurve *cubFunCurve, *cubDerCurve;<br> QPushButton *sinButton, *cubButton;<br><br> private slots:<br> void sinToggled( bool checked );<br> void cubToggled( bool checked );<br>};


Class implementation (QwtBeginner.cpp file)

In the class constructor
QwtBeginner::QwtBeginner(QWidget *parent)<br> : QWidget(parent)<br>{

drawing areas are created and their headers are set:
// Create plots <br> funPlot = new QwtPlot;<br> derPlot = new QwtPlot;<br><br> funPlot->setTitle( "Function" );<br> derPlot->setTitle( "Derivative" );

Let's set “feathers” with which we will draw curves:
// Create curves and attach them to plots <br> QPen sinPen = QPen(Qt::red);<br> QPen cubPen = QPen(Qt::blue);

Create curves, require them to be smoothed ( RenderAntialiased ), point to “feathers” ( setPen() ), and tie the curves to the corresponding drawing areas ( attach() ).
sinFunCurve = new QwtPlotCurve;<br> sinFunCurve->setRenderHint(QwtPlotItem::RenderAntialiased);<br> sinFunCurve->setPen(sinPen);<br> sinFunCurve->attach(funPlot);<br><br> sinDerCurve = new QwtPlotCurve;<br> sinDerCurve->setRenderHint(QwtPlotItem::RenderAntialiased);<br> sinDerCurve->setPen(sinPen);<br> sinDerCurve->attach(derPlot);<br><br> cubFunCurve = new QwtPlotCurve;<br> cubFunCurve->setRenderHint(QwtPlotItem::RenderAntialiased);<br> cubFunCurve->setPen(cubPen);<br> cubFunCurve->attach(funPlot);<br><br> cubDerCurve = new QwtPlotCurve;<br> cubDerCurve->setRenderHint(QwtPlotItem::RenderAntialiased);<br> cubDerCurve->setPen(cubPen);<br> cubDerCurve->attach(derPlot);

Fill the data arrays that we want to display:
// Set data <br> const int N = 20;<br> double x[N+1];<br> double sinFunData[N+1], sinDerData[N+1];<br> double cubFunData[N+1], cubDerData[N+1];<br> for ( int i = 0; i <= N; ++i)<br> {<br> const double pi = 4.0 * atan(1.0);<br> double L = 2;<br> double h = L / N;<br><br> x[i] = -L/2 + i * h;<br> sinFunData[i] = sin(x[i] * pi);<br> sinDerData[i] = cos(x[i] * pi) * pi;<br> cubFunData[i] = x[i] * x[i] * x[i];<br> cubDerData[i] = 3 * x[i] * x[i];<br> }

and link this data with the curves:
sinFunCurve->setData(x, sinFunData, N+1);<br> sinDerCurve->setData(x, sinDerData, N+1);<br> cubFunCurve->setData(x, cubFunData, N+1);<br> cubDerCurve->setData(x, cubDerData, N+1);

To begin, hide the curves:
// Hide curves <br> sinFunCurve->setVisible( false );<br> sinDerCurve->setVisible( false );<br><br> cubFunCurve->setVisible( false );<br> cubDerCurve->setVisible( false );

Create buttons and associate signals with slots:
// Create buttons <br> sinButton = new QPushButton( "Sinus" ),<br> cubButton = new QPushButton( "Cubic function" ),<br><br> sinButton->setCheckable( true );<br> cubButton->setCheckable( true );<br><br> // Connect signals <br> connect(sinButton, SIGNAL(toggled( bool )), this , SLOT(sinToggled( bool )));<br> connect(cubButton, SIGNAL(toggled( bool )), this , SLOT(cubToggled( bool )));

Place the widgets on the form and open the window in full screen:
// Set layouts <br> QHBoxLayout *plotsLayout = new QHBoxLayout;<br> plotsLayout->setSpacing(10);<br> plotsLayout->addWidget(funPlot);<br> plotsLayout->addWidget(derPlot);<br><br> QHBoxLayout *buttonsLayout = new QHBoxLayout ;<br> buttonsLayout->addWidget(sinButton);<br> buttonsLayout->addWidget(cubButton);<br><br> QVBoxLayout *widgetLayout = new QVBoxLayout;<br> widgetLayout->addLayout(plotsLayout);<br> widgetLayout->addLayout(buttonsLayout);<br><br> setLayout(widgetLayout);<br> showMaximized();<br>}

Finally, the implementation of slots:
void QwtBeginner::sinToggled( bool checked )<br>{<br> sinFunCurve->setVisible( checked );<br> sinDerCurve->setVisible( checked );<br> funPlot->replot();<br> derPlot->replot();<br>} <br><br> void QwtBeginner::cubToggled( bool checked )<br>{<br> cubFunCurve->setVisible( checked );<br> cubDerCurve->setVisible( checked );<br> funPlot->replot();<br> derPlot->replot();<br>}

Notice that we not only have to change the visibility of the curves ( setVisible() ), but also redraw the drawing areas ( replot() ).

Main () function (main.cpp file)

Everything is traditional here:
int main( int argc, char *argv[])<br>{<br> QApplication app(argc, argv);<br> QwtBeginner *wnd = new QwtBeginner;<br> wnd->show();<br> return app.exec();<br>} <br><br> * This source code was highlighted with Source Code Highlighter .


For beginners: compiling and installing Qwt


Archives tar.bz2 and zip are available for download from the official website. At the time of this writing, the latest version is 5.2.0.

So, download and unpack the archive in any temporary directory.

I have made the following changes to the qwtconfig.pri file.

Now you can proceed to the assembly and installation.

Windows


Linux


Conclusion


Qwt is a convenient cross-platform tool for plotting functions of one variable. It provides a simple interface and eliminates the need to take care of details, such as scaling and labels on the axes.

It should also be noted that the examples/ source subdirectory contains 15 more examples, among which are examples/simple_plot , which served as the basis for my program.

I would appreciate criticism, comments and additions, as long as I am not a great Qt specialist.

Thanks for attention!

PS

Similar tools.

Source: https://habr.com/ru/post/82614/


All Articles