[SOLVED] Graphics program eats a lot of memory? Is that normal?
-
Hello,
I have an application with QGraphicsView. A scene filled with around 30 items inherited from QGraphicsRectItem. PNG image as a resource embed in the exe with ResourceLoader. When I ran the program the memory allocated is over 700 MB. So is this normal? Or I have to revisit my code in detail?
Here is the code that botters me. Can you confirm it`s not allocating some trash memory?
@
MainWindow::MainWindow(int w, int h, QApplication* qapp) {QDesktopWidget wd; QRect r = wd.availableGeometry(); qDebug() << "SCREEN GEOMETRY:" << r.width() << "X" << r.height() <<"\n"; timer = new QTimer; /* test */ player = new QMediaPlayer(); player->setMedia(QUrl(":/resources/sounds/blop.wav")); width = r.width(); height = r.height(); QImage image; QString s("/home/ilian/QT5/MathForFun/myresource.rcc"); AssetManager::loadFileFromResource(s); QPixmap pim(":/resources/images/test3.png"); pim = pim.scaled(width,height); image = pim.toImage(); QRgb rgb ; QRgb special = qRgb(255,0,255); pim = QPixmap::fromImage(image); myQapp = qapp; myScene = new QGraphicsScene; myScene->setBackgroundBrush(pim); myView = new QGraphicsView(myScene); myScene->setSceneRect(QRectF(0,0,width,height)); int i, j, count=0; ActionRectItem *tmpItem; QColor colVal; bool ranswer; for(i=0; i < height; i++) { for (j=0; j < width; j++) { rgb = qRgb(i,j, 0); if ( i % 2 == 0 && j % 2 == 0) { //scanlines //image.setPixel(j, i, special); } else { } colVal = image.pixel(j,i); if ( count % 2 ==0 ) ranswer = true; else ranswer = false; if ( colVal.red() == MY_RED && colVal.green() == MY_GREEN && colVal.blue() == MY_BLUE) { actionItems.append( new ActionEllipseItem (j,i, (int)width * 2/40, (int)height * 2/30, false,"ELL",count++,this, myScene, gameofnumbers) ); } tmpItem = new ActionRectItem(0,0, 300,200, "Screen", 1, this, myScene); } } gameofnumbers->createWinningNumbers(actionItems); i=0; j=0; myScene->addItem(tmpItem); connect(timer, SIGNAL(timeout()), myScene, SLOT(advance())); timer->start(100); for (i=0; i < actionItems.size(); i++) { actionItems.at(i)->drawEllipseItem(); myScene->addItem(actionItems.at(i)); } myView->show();
}
@
Thats the effect: https://www.flickr.com/photos/heatblazer/15959286192/ [EDIT] Sorry, I saw my error. I am creating N times ActionRectItem. It was not supposed to be in the loop. I
ve just mistyped it in the for loop. Now the program takes 50 MB. I was shocked!!! Sorry, to ask this. -
You don't need to apologize for asking a question about Qt application on Qt forum. We all make mistakes even such silly as mistyping.
Thank you for sharing a solution.
-
Thank you. I am quite new in Qt and work alone, so I need to share sometimes with fellow programmers.
[quote author="andreyc" date="1417927488"]You don't need to apologize for asking a question about Qt application on Qt forum. We all make mistakes even such silly as mistyping.Thank you for sharing a solution.[/quote]