Results 1 to 2 of 2

Thread: Difference between what a QGraphicsScene shows and what I save

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Difference between what a QGraphicsScene shows and what I save

    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:



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



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

    Qt Code:
    1. // Build the Histogram
    2. void frmStats::setupGrafico()
    3. {
    4. // qDebug() << "frmStats setupGrafico";
    5.  
    6. if (maxCifraAnno == 0)
    7. {
    8. singolaUnitaIsto = 0;
    9. }
    10. else
    11. {
    12. singolaUnitaIsto = - 1 * (double(maxAltezzaIsto) / double(maxCifraAnno));
    13. }
    14.  
    15. scene->clear();
    16.  
    17. // X line
    18. QGraphicsLineItem *ascissa =
    19. new QGraphicsLineItem(QLine(margineX, endY - margineY, endX - 10,
    20. endY - margineY), 0, scene);
    21. ascissa->setPen(QPen(Qt::black, 3, Qt::SolidLine));
    22.  
    23. // Y line
    24. QGraphicsLineItem *ordinata =
    25. new QGraphicsLineItem(QLine(margineX, 10, margineX, endY - margineY), 0,
    26. scene);
    27. ordinata->setPen(QPen(Qt::black, 3, Qt::SolidLine));
    28.  
    29. for (int i = 1; i < 4; i++)
    30. {
    31. // 25%, 50% and 75% lines
    32. int factorY = ((endY - margineY) / 4) * i;
    33. QGraphicsLineItem *livelliY =
    34. (margineX, factorY, endX - 10, factorY), 0, scene);
    35. livelliY->setPen(QPen(Qt::black, 1, Qt::DashLine));
    36.  
    37. QGraphicsTextItem *testoPercentuale =
    38. new QGraphicsTextItem(QString::number(100 - (25 * i)) + "%", 0, scene);
    39. int h = int(testoPercentuale->boundingRect().height() / 2);
    40. int w = int(testoPercentuale->boundingRect().width());
    41. testoPercentuale->setPos(margineX - w, factorY - h);
    42. }
    43.  
    44. QString inEuro;
    45.  
    46. QFont font;
    47. font.setPointSize(14);
    48.  
    49. for (int i = 0; i < 12; i++)
    50. {
    51. /*
    52.   Draw the histogram's values per month
    53.   */
    54. new QGraphicsRectItem(QRect(margineX + 1 + 60 * i, endY - margineY - 1,
    55. 60, int(totMese[i] * singolaUnitaIsto)), 0, scene);
    56. isto->setPen(QPen(Qt::black, 1, Qt::SolidLine));
    57. isto->setBrush(Qt::green);
    58. isto->setOpacity(0.7);
    59.  
    60. /*
    61.   histogram' s text
    62.   */
    63. inEuro = QString("%L1").arg(totMese[i] ,12,'f',2,' ');
    64.  
    65. QGraphicsTextItem *testoIsto =
    66. new QGraphicsTextItem("", 0, scene);
    67. testoIsto->setFont(font);
    68. testoIsto->setDefaultTextColor(Qt::black);
    69. testoIsto->setHtml(inEuro);
    70. int xSize = int(testoIsto->boundingRect().height());
    71. testoIsto->setPos(margineX + 2 + 30 - int(xSize / 2) + 60 * i,
    72. (endY - margineY - 2));
    73. testoIsto->rotate(-90);
    74. testoIsto->translate(20, 0);
    75. /*
    76.   Porto in fronte - di livello - il testo con setZValue.
    77.   */
    78. testoIsto->setZValue(0.1);
    79.  
    80. /*
    81.   Month's text
    82.   */
    83. QGraphicsTextItem *testoMese =
    84. new QGraphicsTextItem(mesiAnno.at(i), 0, scene);
    85. int h = int(testoMese->boundingRect().height() / 2);
    86. int w = int(testoMese->boundingRect().width() / 2);
    87. testoMese->setPos
    88. (margineX + 30 - w + 60 * i, (endY - (margineY / 2 + h)));
    89. }
    90. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Print the image
    2.  
    3. void frmStats::creaFileHtml()
    4. {
    5. // qDebug() << "->frmStats creaFileHtml";
    6.  
    7. QPixmap pixmap(endX, endY);
    8. QPainter painter(&pixmap);
    9. painter.setRenderHint(QPainter::Antialiasing);
    10. scene->render(&painter);
    11. painter.end();
    12. pixmap.save(QDir::currentPath() + "/statistiche.png");
    13.  
    14. // qDebug() << QDir::currentPath() + "/statistiche.png";
    15. }
    To copy to clipboard, switch view to plain text mode 

    and that's all, please help me!
    thanks
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difference between what a QGraphicsScene shows and what I save

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

Similar Threads

  1. I want to save and retrive a QGraphicsScene
    By c_srikanth1984 in forum Qt Programming
    Replies: 16
    Last Post: 7th January 2014, 14:19
  2. Rendering QGraphicsScene to QPixmap to save
    By Rooster in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2009, 05:24
  3. QWebView shows '?' instead of all images
    By doep in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2009, 10:03
  4. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03
  5. Can I save the drawings from QGraphicsScene?
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 19th November 2008, 11:03

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.