Results 1 to 16 of 16

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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...

  2. #2
    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?

  3. #3
    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

  4. #4
    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
  •  
Qt is a trademark of The Qt Company.