[SOLVED] memory get exhausted due to setting Icon on a Tool-Button



  • Hello !

    I am using the following code to set the Icon on a Tool-Button:

     QIcon sc;
     sc.addPixmap(screen->grabWindow(0,-1,-1,screen->geometry().width(),screen->geometry().height()).scaled(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
     ui->toolButton_ScreenShot->setIcon(sc);
     ui->toolButton_ScreenShot->setIconSize(QSize(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height()));
    

    Every time the Icon gets renewed memory gets exhausted. This also happen if I use a file like

    sc.addFile(QStringLiteral("../logo.png"), QSize(), QIcon::Normal, QIcon::Off);

    Is this an issue caused by the way I doing this or is this a bug ?

    Best Regards Martin


  • Lifetime Qt Champion

    Hi,

    Do you mean that if you call that code once you're out of memory ?



  • @SGaist every time I call this code I loose memory. Seems like the old allocation for the Pixmap will not made free.
    This was testes on Mac OS X. I will check this issue for Linux....didn't do that yet.


  • Lifetime Qt Champion

    Do you have any allocation around that code ?



  • @SGaist No. nothing more around with any allocation of memory. Here the complete code (called by the timer):

    void Dog::ScreenShooter()
    {
        int scnum = ui->spinBox_screen_num->value();
    
        QList<QScreen*> sl = QGuiApplication::screens();
    
        QScreen *screen=NULL;
    
        if(  scnum  < sl.size() )
        {
         screen = sl.at(scnum);
         ui->label_screen->setText(QString("Screen(%1)").arg(screen->name()));
        }
        else
        {
         screen = QGuiApplication::primaryScreen();
         ui->label_screen->setText(QString("primaryScreen(%1)").arg(screen->name()));
        }
    
        if(screen)  // this is play code and the redundant lines will go later !
        {
         //FIXIT: memory get exhausted !
         QIcon sc;
         sc.addPixmap(screen->grabWindow(0,-1,-1,screen->geometry().width()+200,screen->geometry().height()+200).scaled(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
         ui->toolButton_ScreenShot->setIcon(sc);
         ui->toolButton_ScreenShot->setIconSize(QSize(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height()));
        }
        else
        {
            QIcon icon2;
            icon2.addFile(QStringLiteral("../fail.png"), QSize(), QIcon::Normal, QIcon::Off);
            ui->toolButton_ScreenShot->setIcon(icon2);
            ui->toolButton_ScreenShot->setIconSize(QSize(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height()));
        }
    }
    

  • Lifetime Qt Champion

    Did you check the pixmap size ?



  • @SGaist I found my mistake !

        QIcon sc;
         QPixmap pm=screen->grabWindow(QWidget::winId(),0,0,screen->geometry().width(),screen->geometry().height()).scaled(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
         sc.addPixmap(pm);
         ui->toolButton_ScreenShot->setIcon(sc);
         ui->toolButton_ScreenShot->setIconSize(QSize(ui->toolButton_ScreenShot->width(),ui->toolButton_ScreenShot->height()));
    

    It was not good to use -1 for x and y parameter of grabWindow. Now with this call everything works as expected. So it was a Layer 8 Problem :-)
    But thank you very much for your help !!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.