Wednesday 30 January 2013

Getting started with QWT

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.

21 comments:

  1. Hey Stephen,

    When 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

    ReplyDelete
    Replies
    1. Hi Zia,

      You 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.

      Delete
    2. Hey,

      It 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 ?

      Delete
    3. Hi Zia,

      The 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.

      Delete
    4. Hey Stephen,

      Installing 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.. :)

      Delete
  2. 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.

    ReplyDelete
    Replies
    1. Hi Camille,
      I 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...

      Delete
  3. It's very useful.Thanks!

    My 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. Thanks a lot Stephen,very helpfull information!!! can say only that qwt6.0.2 on Windows work in Release-compiled mode.

    ReplyDelete
  6. Hey stephen,

    I 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

    ReplyDelete
    Replies
    1. Hi BoluBeyi,

      I 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

      Delete
    2. It didn't work :( Can i draw this example with visual 2010 c++?

      Delete
    3. Can 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

      Delete
  7. Great..It worked like a charm wiht QT4.8.4 but with newer version of QT it is creating problem..

    ReplyDelete
  8. Hi,
    i 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

    ReplyDelete
  9. Hi,
    i 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

    ReplyDelete
    Replies
    1. 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
      https://www.youtube.com/watch?v=B7UsWtyhXh4

      Delete
  10. Thanks:
    I 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?

    ReplyDelete
  11. I fixed now after setting the LD_LIBRARY_PATH.

    Thanks

    ReplyDelete
  12. Hi Stephen,
    Did you use any widgets using QWT?

    Thanks

    ReplyDelete