PDA

View Full Version : Acquire data from database & Plot it to a graph



maveric
15th May 2008, 14:44
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.



m_time++;
memmove(m_y, &m_y[1], 99 * sizeof(double));
m_y[99] = 10 * sin((double)m_time / 360 * 3.1415 * 10) + rand() % 80 - 50;

for (int i = 0; i < 100; i++)
m_x[i]++;
if (!m_curve)
{
m_curve = new QwtPlotCurve();
m_curve->setPen(QPen(Qt::red));
m_curve->setData(m_x, m_y, 100);
m_curve->attach(myPlot);
}
m_curve->setData(m_x, m_y,100);
myPlot->setAxisScale(QwtPlot::xBottom, m_time, m_time +100); // auto replots


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

maveric
16th May 2008, 09:16
hi all,
please help me... there is no one with me to guide me out with this problem...


thnx in advance...

Uwe
16th May 2008, 17:37
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

maveric
23rd May 2008, 06:53
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 ....





QSqlQuery q;
if( q.exec( "select x, y from data_table order by x;" ) )
{
int nb;
nb = q.size();
x = new double[nb];
y = new double[nb];
for( int i = 0; q.next(); i++ )
{
x[i] = q.value(0).toDouble();
y[i] = q.value(0).toDouble();
}
}
m_curve = new QwtPlotCurve();
m_curve->setPen(QPen(Qt::red));
//setPen(QPen(QColor::fromRgb(255, 255, 0), 3));
m_curve->setData(m_x, m_y, 100);
m_curve->attach(myPlot);
}

Weilor
23rd May 2008, 09:30
and its saying that Include dir <QSql> is not found.. even though i have linked correct path ....


Did you activate the QtSql module?

maveric
23rd May 2008, 09:36
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

maveric
23rd May 2008, 14:42
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

maveric
24th May 2008, 13:23
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]




void test_plotdb::on_plotButton_clicked()
{
m_sqlModel = new QSqlQueryModel(this);
QSqlQuery q("SELECT x,y FROM data_table1");
if (q.exec())
{
q.first();
do
{
m_curve = new QwtPlotCurve();
m_curve->setPen(QPen(Qt::red));
m_curve->setData(q.value(1).toDouble(),q.value(0).toDouble( ), 100);
m_curve->attach(myPlot);

}
while (q.next());
}
else
QMessageBox::critical(this, "Failure", "Query failure");
}

maveric
25th May 2008, 09:25
Hi friends pls help me out with this problem.?? I have one error, which i have posted above but some problem debugging it. :confused:


Thank You

jacek
26th May 2008, 03:50
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().

maveric
26th May 2008, 08:01
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.....





m_sqlModel = new QSqlQueryModel(this);
QSqlQuery q("SELECT xval,yval FROM data_table1");
if (q.exec())
{

for(int i=q.first();i<q.last();i++)
{

QList <Double> x_list;
x_list.append(q.value(i));
QList <Double> y_list;
y_list.append(q.value(i));
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();
}
}
else
{
QMessageBox::critical(this, "Failure", "Query failure");
}



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

jacek
26th May 2008, 13:57
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:
for(int i=q.first();i<q.last();i++)
{
QList <Double> x_list;
x_list.append(q.value(i));
QList <Double> y_list;
y_list.append(q.value(i));
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();
}

maveric
26th May 2008, 14:42
hi,


for(int i=q.first();i<q.last();i++)
{
QList <Double> x_list;
x_list.append(q.value(i));
QList <Double> y_list;
y_list.append(q.value(i));
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();
}


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

jacek
26th May 2008, 15:13
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:

create a new empty list called x_list,
append a single value to x_list,
create a new empty list called y_list,
append a single value to y_list,
create and setup a new curve,
pass i-th elements of x_list and y_list and a magic value "100" to QwtPlotCurve::setData,
attach the curve to the plot,
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?

maveric
26th May 2008, 15:36
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..

jacek
27th May 2008, 02:46
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.



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.


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.


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.