PDA

View Full Version : Difference between what a QGraphicsScene shows and what I save



cydside
29th January 2010, 05:51
Hi to all,
I'm having trouble saving the image showed in a QGraphicsScene. Here is an application's screenshot (screen-01.jpg) that worth more than thousands words:

http://www.qtcentre.org/asset.php?fid=4197&uid=6778&d=1264742723

it's an histogram that shows two values, and the following is the resulting image saved (statistiche.png):

http://www.qtcentre.org/asset.php?fid=4196&uid=6778&d=1264742721

it's, incredibly, without the green values.
Ok, I'm going to show you my code to build the histogram and save it:



// Build the Histogram
void frmStats::setupGrafico()
{
// qDebug() << "frmStats setupGrafico";

if (maxCifraAnno == 0)
{
singolaUnitaIsto = 0;
}
else
{
singolaUnitaIsto = - 1 * (double(maxAltezzaIsto) / double(maxCifraAnno));
}

scene->clear();

// X line
QGraphicsLineItem *ascissa =
new QGraphicsLineItem(QLine(margineX, endY - margineY, endX - 10,
endY - margineY), 0, scene);
ascissa->setPen(QPen(Qt::black, 3, Qt::SolidLine));

// Y line
QGraphicsLineItem *ordinata =
new QGraphicsLineItem(QLine(margineX, 10, margineX, endY - margineY), 0,
scene);
ordinata->setPen(QPen(Qt::black, 3, Qt::SolidLine));

for (int i = 1; i < 4; i++)
{
// 25%, 50% and 75% lines
int factorY = ((endY - margineY) / 4) * i;
QGraphicsLineItem *livelliY =
new QGraphicsLineItem(QLine
(margineX, factorY, endX - 10, factorY), 0, scene);
livelliY->setPen(QPen(Qt::black, 1, Qt::DashLine));

QGraphicsTextItem *testoPercentuale =
new QGraphicsTextItem(QString::number(100 - (25 * i)) + "%", 0, scene);
int h = int(testoPercentuale->boundingRect().height() / 2);
int w = int(testoPercentuale->boundingRect().width());
testoPercentuale->setPos(margineX - w, factorY - h);
}

QString inEuro;

QFont font;
font.setPointSize(14);

for (int i = 0; i < 12; i++)
{
/*
Draw the histogram's values per month
*/
QGraphicsRectItem *isto =
new QGraphicsRectItem(QRect(margineX + 1 + 60 * i, endY - margineY - 1,
60, int(totMese[i] * singolaUnitaIsto)), 0, scene);
isto->setPen(QPen(Qt::black, 1, Qt::SolidLine));
isto->setBrush(Qt::green);
isto->setOpacity(0.7);

/*
histogram' s text
*/
inEuro = QString("%L1").arg(totMese[i] ,12,'f',2,' ');

QGraphicsTextItem *testoIsto =
new QGraphicsTextItem("", 0, scene);
testoIsto->setFont(font);
testoIsto->setDefaultTextColor(Qt::black);
testoIsto->setHtml(inEuro);
int xSize = int(testoIsto->boundingRect().height());
testoIsto->setPos(margineX + 2 + 30 - int(xSize / 2) + 60 * i,
(endY - margineY - 2));
testoIsto->rotate(-90);
testoIsto->translate(20, 0);
/*
Porto in fronte - di livello - il testo con setZValue.
*/
testoIsto->setZValue(0.1);

/*
Month's text
*/
QGraphicsTextItem *testoMese =
new QGraphicsTextItem(mesiAnno.at(i), 0, scene);
int h = int(testoMese->boundingRect().height() / 2);
int w = int(testoMese->boundingRect().width() / 2);
testoMese->setPos
(margineX + 30 - w + 60 * i, (endY - (margineY / 2 + h)));
}
}




// Print the image

void frmStats::creaFileHtml()
{
// qDebug() << "->frmStats creaFileHtml";

QPixmap pixmap(endX, endY);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
scene->render(&painter);
painter.end();
pixmap.save(QDir::currentPath() + "/statistiche.png");

// qDebug() << QDir::currentPath() + "/statistiche.png";
}


and that's all, please help me!
thanks

cydside
29th January 2010, 13:44
Hi to all,
I've created a little, stand-alone project to test with Qt Creator. Could someone test it?
I need feedback, please!
Thanks.