Results 1 to 16 of 16

Thread: Acquire data from database & Plot it to a graph

  1. #1
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Question Acquire data from database & Plot it to a graph

    hi all,

    i am a newbie to this world of QT, and sq lite. I managed to do every thing correctly & tried with some examples also... but got stuck with this problem....

    firstly i connected my form with my database ie. sqlite ...i am able to get data and view it in my required format... but now instead of representing data in reports or in some other format i need to plot data in the forms of graphs just like in the qwt example cpuplot.pro.

    Qt Code:
    1. m_time++;
    2. memmove(m_y, &m_y[1], 99 * sizeof(double));
    3. m_y[99] = 10 * sin((double)m_time / 360 * 3.1415 * 10) + rand() % 80 - 50;
    4.  
    5. for (int i = 0; i < 100; i++)
    6. m_x[i]++;
    7. if (!m_curve)
    8. {
    9. m_curve = new QwtPlotCurve();
    10. m_curve->setPen(QPen(Qt::red));
    11. m_curve->setData(m_x, m_y, 100);
    12. m_curve->attach(myPlot);
    13. }
    14. m_curve->setData(m_x, m_y,100);
    15. myPlot->setAxisScale(QwtPlot::xBottom, m_time, m_time +100); // auto replots
    To copy to clipboard, switch view to plain text mode 

    from the above code i am getting a perfect graph plot by generating random values in the form of a sine wave...

    insted of using this formula
    10 * sin((double)m_time / 360 * 3.1415 * 10) + rand() % 80 - 50;
    i need to get data from database and plot it...

    so can any one help me out..
    Please view the image attached below to get a clear picture...

    thnx in advance.....
    c ya...
    Attached Files Attached Files

  2. #2
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Acquire data from database & Plot it to a graph

    hi all,
    please help me... there is no one with me to guide me out with this problem...


    thnx in advance...

  3. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Acquire data from database & Plot it to a graph

    Read the points from your database and fill them into a QwtData object ( use one the derived classes ). Then pass the data object to the curve.

    Uwe

  4. #4
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Acquire data from database & Plot it to a graph

    hi, uwe thanks for ur reply..
    actually i did in this method...
    but i am getting many errors regarding all the below code... even though i declared all the values i am getting errors stating that i haven't declared nb...
    and its saying that Include dir <QSql> is not found.. even though i have linked correct path ....



    Qt Code:
    1. if( q.exec( "select x, y from data_table order by x;" ) )
    2. {
    3. int nb;
    4. nb = q.size();
    5. x = new double[nb];
    6. y = new double[nb];
    7. for( int i = 0; q.next(); i++ )
    8. {
    9. x[i] = q.value(0).toDouble();
    10. y[i] = q.value(0).toDouble();
    11. }
    12. }
    13. m_curve = new QwtPlotCurve();
    14. m_curve->setPen(QPen(Qt::red));
    15. //setPen(QPen(QColor::fromRgb(255, 255, 0), 3));
    16. m_curve->setData(m_x, m_y, 100);
    17. m_curve->attach(myPlot);
    18. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Hannover, Germany
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Acquire data from database & Plot it to a graph

    and its saying that Include dir <QSql> is not found.. even though i have linked correct path ....
    Did you activate the QtSql module?

  6. #6
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Acquire data from database & Plot it to a graph

    Hi,
    yes i activated my QtSql module?

    i checked my other application which i need to store data in the database..
    i am sure that application is running good with all the values i enter is present in the database..

    thanks in advance

  7. #7
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Acquire data from database & Plot it to a graph

    hi uwe is this how i have to retrive data from database and plot a graph from it. I am trying so much but not able to figure out a proper way to retrive data from database and plot . Pls tell where i am going wrong i have attached the project zip file.


    Thank You
    Attached Files Attached Files

  8. #8
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Acquire data from database & Plot it to a graph

    Hi all,

    I have defined to get data from database to plot in this manner,
    eventually i am facing an error,
    stating like this

    " void QwtPlotCurve::setData(const double *,const double *,int)' : cannot convert parameter 1 from 'double' to 'const double *' "
    So what should i do now to correct this error...
    thanks in advance ..


    This is the function for which i used to generate points from the database...

    My Specification::
    database table name:-- data_table;
    table fields::- x[varchar], y[varchar]


    Qt Code:
    1. void test_plotdb::on_plotButton_clicked()
    2. {
    3. m_sqlModel = new QSqlQueryModel(this);
    4. QSqlQuery q("SELECT x,y FROM data_table1");
    5. if (q.exec())
    6. {
    7. q.first();
    8. do
    9. {
    10. m_curve = new QwtPlotCurve();
    11. m_curve->setPen(QPen(Qt::red));
    12. m_curve->setData(q.value(1).toDouble(),q.value(0).toDouble(), 100);
    13. m_curve->attach(myPlot);
    14.  
    15. }
    16. while (q.next());
    17. }
    18. else
    19. QMessageBox::critical(this, "Failure", "Query failure");
    20. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Acquire data from database & Plot it to a graph

    Hi friends pls help me out with this problem.?? I have one error, which i have posted above but some problem debugging it.


    Thank You

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Acquire data from database & Plot it to a graph

    The QwtPlotCurve::setData() variant you are trying to use expects two arrays of doubles, not plain doubles. You have to first collect all x-es and y-es in a two QLists and then pass them to setData().

  11. #11
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Acquire data from database & Plot it to a graph

    hi jacek,

    thanx for ur reply..

    i tried to do in ur method.. but could not meet to actual requirements

    i am getting the following errors using ur method...
    can u solve.. them please.....


    Qt Code:
    1. m_sqlModel = new QSqlQueryModel(this);
    2. QSqlQuery q("SELECT xval,yval FROM data_table1");
    3. if (q.exec())
    4. {
    5.  
    6. for(int i=q.first();i<q.last();i++)
    7. {
    8.  
    9. QList <Double> x_list;
    10. x_list.append(q.value(i));
    11. QList <Double> y_list;
    12. y_list.append(q.value(i));
    13. m_curve = new QwtPlotCurve();
    14. m_curve->setPen(QPen(Qt::red));
    15. m_curve->setData(x_list.value(i), y_list.value(i), 100);
    16. m_curve->attach(myPlot);
    17. myPlot->replot();
    18. }
    19. }
    20. else
    21. {
    22. QMessageBox::critical(this, "Failure", "Query failure");
    23. }
    To copy to clipboard, switch view to plain text mode 


    These r the errors which i get doing so......

    : error C2065: 'Double' : undeclared identifier
    .\test_plotdb.cpp(86) : error C2133: 'x_list' : unknown size
    .\test_plotdb.cpp(86) : error C2512: 'QList' : no appropriate default constructor available
    .\test_plotdb.cpp(87) : error C2662: 'QList<T>::append' : cannot convert 'this' pointer from 'QList' to 'QList<T> &'
    Reason: cannot convert from 'QList' to 'QList<T>'
    Conversion requires a second user-defined-conversion operator or constructor
    .\test_plotdb.cpp(88) : error C2133: 'y_list' : unknown size
    .\test_plotdb.cpp(88) : error C2512: 'QList' : no appropriate default constructor available
    .\test_plotdb.cpp(89) : error C2662: 'QList<T>::append' : cannot convert 'this' pointer from 'QList' to 'QList<T> &'
    Reason: cannot convert from 'QList' to 'QList<T>'
    Conversion requires a second user-defined-conversion operator or constructor
    .\test_plotdb.cpp(95) : error C2663: 'QList<T>::value' : 2 overloads have no legal conversion for 'this' pointer
    .\test_plotdb.cpp(95) : error C2663: 'QList<T>::value' : 2 overloads have no legal conversion for 'this' pointer
    thanx in advance..

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Acquire data from database & Plot it to a graph

    C++ is case-sensitive --- it should be double not Double.

    Also please explain with your own words what does this part of your code exactly do:
    Qt Code:
    1. for(int i=q.first();i<q.last();i++)
    2. {
    3. QList <Double> x_list;
    4. x_list.append(q.value(i));
    5. QList <Double> y_list;
    6. y_list.append(q.value(i));
    7. m_curve = new QwtPlotCurve();
    8. m_curve->setPen(QPen(Qt::red));
    9. m_curve->setData(x_list.value(i), y_list.value(i), 100);
    10. m_curve->attach(myPlot);
    11. myPlot->replot();
    12. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Acquire data from database & Plot it to a graph

    hi,
    Qt Code:
    1. for(int i=q.first();i<q.last();i++)
    2. {
    3. QList <Double> x_list;
    4. x_list.append(q.value(i));
    5. QList <Double> y_list;
    6. y_list.append(q.value(i));
    7. m_curve = new QwtPlotCurve();
    8. m_curve->setPen(QPen(Qt::red));
    9. m_curve->setData(x_list.value(i), y_list.value(i), 100);
    10. m_curve->attach(myPlot);
    11. myPlot->replot();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Actually from the for loop i am explaining

    taking a incremental value i and initializing that to the first value present in the query

    QSqlQuery q("SELECT xval,yval FROM PlotGraph1");
    this is the query which displays the values present in the database..

    taking a qlist and x_list which takes the x values present in the database...
    assigning the values present in the database to x and y...

    here x_list is being appended with the values of xval (first field) present in the database..
    QList <double> x_list;
    x_list.append(q.value(i));



    similarlly
    here y_list is being appended with the values of yval (second field) present in the database..
    QList <double> y_list;
    y_list.append(q.value(i));


    Next

    m_curve = new QwtPlotCurve();
    m_curve->setPen(QPen(Qt::red));
    m_curve->setData(x_list.value(i), y_list.value(i), 100);
    m_curve->attach(myPlot);
    myPlot->replot();

    now i am initializing a curve as m_curve
    taking red color to plot my curve, i initialized a pen over here..


    m_curve->setData(x_list.value(i), y_list.value(i), 100);
    this actually takes the values present in the x_list and y_list ..

    now
    m_curve->attach(myPlot);
    myPlot->replot();


    the above code will attach my curve to my qwtplot widget ie..(myPlot)
    and plots the points based on the values taken above...


    i kept this code in draw function and keep on calling @ 1sec rate with the timer....


    I think i have explained every thing present in the code....

    if i go wrong any where please correct me so that i will not repeat the mistake again...



    thanks in advance...


    giving or taking, both r the similar form's of getting knowledge...

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Acquire data from database & Plot it to a graph

    Quote Originally Posted by maveric View Post
    Actually from the for loop i am explaining

    taking a incremental value i and initializing that to the first value present in the query
    ...
    this is the query which displays the values present in the database..

    taking a qlist and x_list which takes the x values present in the database...
    assigning the values present in the database to x and y...

    here x_list is being appended with the values of xval (first field) present in the database..
    ...
    similarlly
    here y_list is being appended with the values of yval (second field) present in the database..
    ...
    Next
    ...
    now i am initializing a curve as m_curve
    taking red color to plot my curve, i initialized a pen over here..


    m_curve->setData(x_list.value(i), y_list.value(i), 100);
    this actually takes the values present in the x_list and y_list ..

    now
    ...
    the above code will attach my curve to my qwtplot widget ie..(myPlot)
    and plots the points based on the values taken above...
    Good, but there are few key words missing.

    For each row in the query result you:
    1. create a new empty list called x_list,
    2. append a single value to x_list,
    3. create a new empty list called y_list,
    4. append a single value to y_list,
    5. create and setup a new curve,
    6. pass i-th elements of x_list and y_list and a magic value "100" to QwtPlotCurve::setData,
    7. attach the curve to the plot,
    8. replot the plot.

    and you repeat that loop every second.

    The questions are:
    How many items are there in x_list and _ylist when the flow reaches point #6?
    Where does this 100 come from?
    Are there any steps that you could or should move out of that loop?

  15. #15
    Join Date
    May 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Acquire data from database & Plot it to a graph

    How many items are there in x_list and _ylist when the flow reaches point #6?

    ANS::>>): --- depending up on the last value present in the database ie. currently my database consists of 13 values and i mean to say that x consists of 13 values along with y...



    Q) Where does this 100 come from?

    Actually 100 is the array size which is mentioned in the qwt_data class reference..
    i am not sure about it... as a trail and error method i kept the value as 100 so that my plot would start from the 100th point assuming that there is some delay in order to plot the graph...


    Are there any steps that you could or should move out of that loop?

    i am not sure about ur third question? as i am a new to this world of qt,
    the whole code which i had given u is being from an example... got from google..
    which i modified that according to my requirement...

    so sir, can u please tell me exactly what mistakes i have committed....

    I have attached my program by the name " test_plotdb.cpp", "Connection.h"
    please find them and have a glance at it....



    Thanks for ur reply's


    Always giving or taking is both ways of learning knowledge..
    Attached Files Attached Files

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Acquire data from database & Plot it to a graph

    Quote Originally Posted by maveric View Post
    How many items are there in x_list and _ylist when the flow reaches point #6?

    ANS::>>): --- depending up on the last value present in the database ie. currently my database consists of 13 values and i mean to say that x consists of 13 values along with y...
    No, there's always exactly one item in x_list and y_list. Look at the 1--4 points --- in each iteration you create new empty lists. Please take some C++ tutorial and read about scopes and automatic variables.


    Quote Originally Posted by maveric View Post
    Q) Where does this 100 come from?

    Actually 100 is the array size which is mentioned in the qwt_data class reference..
    i am not sure about it... as a trail and error method i kept the value as 100 so that my plot would start from the 100th point assuming that there is some delay in order to plot the graph...
    The QwtPlotCurve::setData() docs say:
    ...
    Parameters:
    xData pointer to x values
    yData pointer to y values
    size size of xData and yData
    ...
    The third parameter says how many items there are in xData and yData (in your case x_list and y_list respectively), so it can't be simply 100.

    The second problem is that three-parameter version of setData() is for double arrays and you now use QList< double >, so you have to use the two-parameter version.

    Quote Originally Posted by maveric View Post
    i am not sure about ur third question? as i am a new to this world of qt,
    the whole code which i had given u is being from an example... got from google..
    which i modified that according to my requirement...
    The problem is not that you are new to Qt, but that you must learn to understand C++ code. You can't program anything if you don't understand what you write.

    Quote Originally Posted by maveric View Post
    so sir, can u please tell me exactly what mistakes i have committed....
    No, because you won't learn anything. First try to understand what exactly happens in that loop.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.