I have been looking for a graphing API for QT and eventually found one that suits my needs called QWT ( Qt Widgets for Technical Applications). I ran into some issues while setting it up but finally got it running so here is the resulting tutorial on how to set-up QWT in QT creator (for Linux 12.0.4).
Now we can begin, first step is to download the source files from here. The version used for this tutorial is QWT 6.1. In the terminal, navigate to the location of the downloaded .tar.bz2 file, then type the following commands:
$ tar -xjvf qwt-6.1-rc3.tar.bz2 $ cd qwt-6.1-rc3 $ qmake qwt.pro $ make $ make install
Launch QT creator and then Go to File > New File or Project. Select a QT Gui Application give the project a name e.g. “FirstQwtProject”.You could leave the other settings in the wizard as they are or change them to suit your own projects. I have kept the 'FirstQwtProject.pro' ( this will vary depending on the Project Name you have chosen) and 'main.cpp' but deleted the remaining files (MainWindow class) as they would not be need for this simple introduction. To run QWT programs in QT creator we need to let the IDE know where to find the QWT libraries.
So open the .pro file associated with the project and append the following lines at the end of the file. This should be included in every QWT Project you create but remember to change the include Path "/usr/local/qwt-6.1.0-rc3/.." to the location of the QWT install directory on your PC.
CONFIG += qwt INCLUDEPATH +="/usr/local/qwt-6.1.0-rc3/include" LIBS += -L/usr/local/qwt-6.1.0-rc3/lib -lqwt
Now go to the 'main.cpp' file and type in the following lines of code and press Ctrl+R to run it.
#include <QApplication> #include <qwt_plot.h> #include <qwt_plot_curve.h> #include <qwt_plot_grid.h> #include <qwt_symbol.h> #include <qwt_legend.h> int main(int argc, char *argv[]) { QApplication a(argc, argv); QwtPlot plot; plot.setTitle( "Plot Demo" ); plot.setCanvasBackground( Qt::white ); plot.setAxisScale( QwtPlot::yLeft, 0.0, 10.0); plot.insertLegend( new QwtLegend() ); QwtPlotGrid *grid = new QwtPlotGrid(); grid->attach( &plot ); QwtPlotCurve *curve = new QwtPlotCurve(); curve->setTitle( "Pixel Count" ); curve->setPen( Qt::blue, 4 ), curve->setRenderHint( QwtPlotItem::RenderAntialiased, true ); QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) ); curve->setSymbol( symbol ); QPolygonF points; points << QPointF( 0.0, 4.4 ) << QPointF( 1.0, 3.0 ) << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 ) << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 ); curve->setSamples( points ); curve->attach( &plot ); plot.resize( 600, 400 ); plot.show(); return a.exec(); }
This is what you should see.
Conclusion
QWT Libraries are one of many options for plotting graphs in QT. In the next tutorial, I will show you how to plot histograms calculated via OpenCV with QWT.
Hey Stephen,
ReplyDeleteWhen I download the qwt lib and run make command. I get this error
cd src/ && ( test -f Makefile || /usr/bin/qmake /home/zia/qwt-6.1-rc3/src/src.pro -o Makefile ) && make -f Makefile
make[1]: Entering directory `/home/zia/qwt-6.1-rc3/src'
compiling qwt_abstract_scale_draw.cpp
In file included from qwt_abstract_scale_draw.h:13:0,
from qwt_abstract_scale_draw.cpp:10:
qwt_global.h:13:21: fatal error: qglobal.h: No such file or directory
compilation terminated.
make[1]: *** [obj/qwt_abstract_scale_draw.o] Error 1
make[1]: Leaving directory `/home/zia/qwt-6.1-rc3/src'
make: *** [sub-src-make_first-ordered] Error 2
Can u help me on this.. I have installed the qt creator from the ubuntu software center...Any pointers?
Regards,
Zia
Hi Zia,
DeleteYou need to install the Qt4 development libraries via the command line. Please have a look at this link.
http://oestudyard.blogspot.co.uk/2010/10/install-qwt-in-ubuntu.html
Please let me know if that was helpful.
Hey,
DeleteIt does not work for me.. :(
But I have some doubts on First step... I couldn't find the files in my HOME folder that contains dependences.. This can be a problem ?
Hi Zia,
DeleteThe actual problem is shown on this line :
qwt_global.h:13:21: fatal error: qglobal.h: No such file or directory
The system could not find the Qt4 include "qglobal.h"
I would advice you manually download the Qt library from http://qt-project.org/downloads#qt-lib (preferably version 4.8) and install it.
Hey Stephen,
DeleteInstalling Qt 4.8 works for me.. Before this I was using Qt 5.0.2..
I tried 3 version of qwt like 5.2, 6.0 and 6.1.. These 3 of them working with QT 4.8.4... Also ran this on Raspberry pi Board. It works well there too.. Thanks a lot for your help and support.. :)
Do you know how I can use qwt in the QtDesigner ? I know there is a plugin to add but I don't find the good way to make it work.
ReplyDeleteHi Camille,
DeleteI haven't used qwt in QtDesigner yet. I found this tutorial - http://geekanddo.wordpress.com/2012/03/13/qt-using-qwt-in-qt-designer-on-windows-10/
Let me know if this works for you...
It's very useful.Thanks!
ReplyDeleteMy environment is ubuntu 12.04
I use "qt-everywhere-opensource-src-4.8.5" to generate MAKEFILE for "qwt-6.1.0" .
Following your instruction, it's works!
There will be error if i use qt-4.7.1 :
In file included from qwt_plot_glcanvas.cpp:10:
qwt_plot_glcanvas.h:15: fatal error: qgl.h: No such file or directory
compilation terminated.
Hello, CE. I just tried this on my Qt Creator on Ubuntu. Fully okay and it works! Works like charm. I want charting like this (Qwt, TeeChart) since many months ago now I get it. You don't know how glad I am. Thank you.
ReplyDeleteThanks a lot Stephen,very helpfull information!!! can say only that qwt6.0.2 on Windows work in Release-compiled mode.
ReplyDeleteHey stephen,
ReplyDeleteI tried this example but i have this error: 'qwt_plot.h' no such file or directory
And i added qwt_plot.h in my header files? Do you have an idea?
Thanks
Hi BoluBeyi,
DeleteI haven't tried this on windows yet, but try the last post on this link
http://www.qtcentre.org/threads/49449-qwt_plot-h-No-such-file-or-directory
It didn't work :( Can i draw this example with visual 2010 c++?
DeleteCan you send email this project? I need a such example, it doesn't matter qwt, c++ console... I want to draw line graph according to numbers in integer array
DeleteGreat..It worked like a charm wiht QT4.8.4 but with newer version of QT it is creating problem..
ReplyDeleteHi,
ReplyDeletei am able to build this with QT creater in OpenSUSE linux environment. When I run, it gives some segmentation fault. Debug mode: Signal name SIGSEGV.
Please help me to figure it out.
Thanks
Hi,
ReplyDeletei am able to compile successfully the above code using QT creater with OpenSUSE linux environment. But, When I run in QT, it gives some error segmentation fault. Debug mode: Signal name SIGSEGV.
Please help me to figure it out.
Thanks
There are a number of problems that can cause this error. You have to use the debugger to find out which line is causing the crash. You can watch this video to see how to debug using qt creator
Deletehttps://www.youtube.com/watch?v=B7UsWtyhXh4
Thanks:
ReplyDeleteI have been using the linux environment, where I could qmake the project and make. Where I found it is not linking with the library.
$./proj1
./proj1: error while loading shared libraries: libqwt.so.6: cannot open shared object file: No such file or directory.
I feel that library is not linking whereas i have this file under my /user/localqwt-6.1.2/lib
kuga@linux-2tst:/usr/local/qwt-6.1.2/lib> ls
libqwt.so libqwt.so.6 libqwt.so.6.1 libqwt.so.6.1.2
Somehow proj1 can not linking. I dono y?
I set the lib path into my pro file as;
LIBS += -L/usr/local/qwt-6.1.2/lib -lqwt
Do you have any idea, why this is not linking?
I fixed now after setting the LD_LIBRARY_PATH.
ReplyDeleteThanks
Hi Stephen,
ReplyDeleteDid you use any widgets using QWT?
Thanks