PDA

View Full Version : Strange behaviour of QPainter...



oscar
7th November 2008, 22:42
I'm missing something:
In the following code, I must display a QMessageBox to get a correct QPixmap (with a chart in it).
Do you know what's the problem?

Thanks.
Regards,
Oscar



QPixmap MyExport::drawPie(int size, QList<int> valueList, QList<QString> colorList) {

QPixmap qPixmap(size,size);
QPainter *myQPainter = new QPainter(&qPixmap);

myQPainter->setBrush(QBrush(QColor(245,245,245)));
myQPainter->drawRect(0,0,size,size);

double startAngle = 0.0;
double totalValue; foreach(int value, valueList) totalValue += value;

for (int i=0; i<valueList.size(); i++) {
if (valueList[i] > 0.0) {
double angle = 360*valueList[i]/totalValue;
QColor color = QColor(colorList[i]);
myQPainter->setBrush(QBrush(color));
myQPainter->drawPie(0, 0, size-2, size-2, int(startAngle*16), int(angle*16));
startAngle += angle;
}
}
delete myQPainter;

QMessageBox::information(NULL, QString::fromUtf8("Warn"),
QString::fromUtf8("This messagebox must be called to make this example function"),
QString::fromUtf8("Quitter"));
return qPixmap;
}

oscar
8th November 2008, 00:08
This code works fine on Linux but generates an empty pixmap on windows (unless I display a messagebox at the end of processing!)

It sounds like a bug...

Regards,
Oscar

oscar
8th November 2008, 12:07
Well, now it's working when I create a class Pie subclassing
a QWidget and draw the pie in the paintEvent(QPaintEvent *event) method.

The Pie class is used like this:



Pie *pie = new Pie(valueList, colorList, 150);
QImage image; image = QPixmap::grabWidget(pie).toImage();^M
image = image.copy(1,1,image.width()-1,image.height()-1);^M
qPainter->drawImage(xPos, yPos, image);


I still think there's a problem in Windows release of Qt when using QPainter...

Regards,
Oscar