PDA

View Full Version : Is that any problem in my this function?



HelloDan
2nd April 2009, 17:11
// draw plotter line using QPainter function drawPolyline
void GenePlotter::drawPlotterLine(QPainter *painter)
{
QRect rect(Margin, Margin,
width() - 2 * Margin, height() - 2 * Margin);
if (!rect.isValid())
return;

painter->setClipRect(rect.adjusted(+1, +1, -1, -1));

//draw best lines
painter->setPen(QPen(Qt::red,1.0));
const QPointF* point=bestPoints.data(); //bestPoints is a vector
painter->drawPolyline(point,bestPoints.size());



//draw average line
painter->setPen(QPen(Qt::blue,1.0)); //X is the same
point=avgPoints.data(); //avgPoints is also a vector
painter->drawPolyline(point,avgPoints.size());



Is that any problem in the above code? That's something wrong, But I cann't tell. It fails to draw the average line.

Thanks

HelloDan
2nd April 2009, 18:33
I'm sure that the data in the vector is OK. but I can not draw line.

spirit
2nd April 2009, 18:35
can you show us code where you use this method?

talk2amulya
2nd April 2009, 18:46
what is the size of avgPoints?

HelloDan
2nd April 2009, 18:56
void GenePlotter::refreshPixmap()
{
pixmap = QPixmap(size());
pixmap.fill(this, 0, 0);

QPainter painter(&pixmap);
painter.initFrom(this);
drawGrid(&painter); // This line works perfectly good

if(couter!=0)
drawPlotterLine(&painter); // here can't draw line

update();
}

HelloDan
2nd April 2009, 18:59
what is the size of avgPoints?

In fact, the size will increase when the app runs, for example the size will increase from 1 to 100. And I will call the function to redraw the pixmap for 100 times.

spirit
2nd April 2009, 19:00
stupid question, but maybe couter == 0? :)

HelloDan
2nd April 2009, 19:05
// perform one generaion evolution and update the points of the plotter
void GenePlotter::execOnce()
{
geneGroup.performEvolution();
QPointF point;
point.setX(couter);
point.setY(((geneGroup.getGroup())+GeneGroup::popS ize)->getfitness());
bestPoints.push_back(point);

point.setY(geneGroup.getAvg());
// x are the same of these two point.
avgPoints.push_back(point);

refreshPixmap();
}


void GenePlotter::oneStep()
{
++couter;
if(couter<generation)
{
execOnce();
}
}


void GenePlotter::runAll()
{
settingButton->setEnabled(false);
oneStepButton->setEnabled(false);
runAllButton->setEnabled(false);
qApp->processEvents();
if(couter==0)
++couter;

while(couter<generation)
{
++couter;
execOnce();
repaint();
}
settingButton->setEnabled(true);
oneStepButton->setEnabled(true);
runAllButton->setEnabled(true);

}




More code

spirit
2nd April 2009, 19:07
if you do this


void GenePlotter::refreshPixmap()
{
pixmap = QPixmap(size());
pixmap.fill(this, 0, 0);

QPainter painter(&pixmap);
painter.initFrom(this);
drawGrid(&painter); // This line works perfectly good
drawPlotterLine(&painter); // here can't draw line

update();
}

is something changed?

HelloDan
2nd April 2009, 19:10
stupid question, but maybe couter == 0? :)

No, the couter will increase. I make it draw only couter >0 ;



// In the construtor function
connect(oneStepButton,SIGNAL(clicked()),this,SLOT( oneStep()));
connect(runAllButton,SIGNAL(clicked()),this,SLOT(r unAll()));

HelloDan
2nd April 2009, 19:15
to #9

Fail, the same

HelloDan
2nd April 2009, 19:18
// draw plotter line using QPainter function drawPolyline
void GenePlotter::drawPlotterLine(QPainter *painter)
{
QRect rect(Margin, Margin,
width() - 2 * Margin, height() - 2 * Margin);
if (!rect.isValid())
return;

painter->setClipRect(rect.adjusted(+1, +1, -1, -1));

//draw best lines
painter->setPen(QPen(Qt::red,1.0));
const QPointF* point=bestPoints.data(); //bestPoints is a vector
painter->drawPolyline(point,bestPoints.size());



//draw average line
painter->setPen(QPen(Qt::blue,1.0)); //X is the same
point=avgPoints.data(); //avgPoints is also a vector
painter->drawPolyline(point,avgPoints.size());



Is that any problem in the above code? That's something wrong, But I cann't tell. It fails to draw the average line.

Thanks

I am sure that the points are in the rectangle

spirit
2nd April 2009, 19:23
if you comment all code in GenePlotter::drawPlotterLine and draw some other shape, circle for example, in this case is something changed?

talk2amulya
2nd April 2009, 20:07
why dont you debug the whole issue..you'll get to the core of the whole problem and thus would be workin in the right direction, its really hard to point out anything like that..just random guesses

HelloDan
3rd April 2009, 03:19
if you comment all code in GenePlotter::drawPlotterLine and draw some other shape, circle for example, in this case is something changed?




QLineF line(QPointF(10,430),QPointF(89,520));
painter->drawLine(line);
// I draw a line to test, but the line didn't turn up too.

HelloDan
3rd April 2009, 03:28
why dont you debug the whole issue..you'll get to the core of the whole problem and thus would be workin in the right direction, its really hard to point out anything like that..just random guesses

Thanks! But in fact I don't know how to debug a progam in a console. For My major is not computer relative, I cann't get help from classmates around me. I just have interest in C++. I debug my app only use a QmessageBox to popup to show some info. I don't know how to use gdb and how to debug. Can you give me some URL about how to debug it ? thanks again!

spirit
3rd April 2009, 07:50
try to cahnge coloro of line in code above, for example use Qt::red.
or if you have possibility to attach all you code then it would be nice. :)

HelloDan
3rd April 2009, 07:57
try to cahnge coloro of line in code above, for example use Qt::red.
or if you have possibility to attach all you code then it would be nice. :)

Thanks! Now I find the problem. I miss to tranform the coordinate points. the points are outside the dialog.

Thanks You all!

Can you show me some technology to debug a program. I found the problem using a QMessage. It's troublesome bussiness. If I know how to debug a program. Maybe it will reduce a lot of trouble.

Thank you!

spirit
3rd April 2009, 08:01
read this (http://doc.trolltech.com/4.5/debug.html) it's not a full guide but something helpful.

HelloDan
3rd April 2009, 15:26
void qDebug ( const char * msg, ... )

Calls the message handler with the debug message msg. If no message handler has been installed, the message is printed to stderr. Under Windows, the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger.

Here, If I write a GUI program. the debug message will not send to the console, it will send to the debugger. where's the debugger? Is that to say that I should use the Qt 4.4.3 (Build Debug Libraries) console to compile the program?


The header file <QtGlobal> contains some debugging macros and #defines.

Three important macros are:

* Q_ASSERT(cond), where cond is a boolean expression, writes the warning "ASSERT: 'cond' in file xyz.cpp, line 234" and exits if cond is false.
* Q_ASSERT_X(cond, where, what), where cond is a boolean expression, where a location, and what a message, writes the warning: "ASSERT failure in where: 'what', file xyz.cpp, line 234" and exits if cond is false.
* Q_CHECK_PTR(ptr), where ptr is a pointer. Writes the warning "In file xyz.cpp, line 234: Out of memory" and exits if ptr is 0.

Here tells will send some warning, but where will the warnings send to? the console?

Thanks! Your are my great help.

talk2amulya
3rd April 2009, 15:36
you wont get any valuable information until you use Qt build debug libraries...cuz its the Qt's function that will show u whats going on inside..although i think in these cases, its best if u debug line by line, checking values of variables..

HelloDan
3rd April 2009, 18:31
Qt build debug libraries

Thank you! I got this word.