paintEvent override



  • Hi everyone...
    I'm working on a qt project where I need to redefine the paintEvent function...
    I'm using this fonction with a qpainter and this way:

    QPainter painter(this);
    

    But, everytime my window get scaled, this widget create a new qpainter and increase the memory size of my program... (140k to 300k)
    Is there any way to delete the older QPainter to free the memory???

    Thx


  • Qt Champions 2017

    Hi
    Normally you have like this

    widget::PaintEvent(xx) {
    QPainter painter(this);
    }

    and painter only lives until paintEvent ends

    so maybe its something else that happens when you scale the window?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show the implementation of your paint event ?

    Currently, your painter object will be deleted at the end of the function.



  • Here is my function:

    void Histogram::paintEvent(QPaintEvent *event)
    {
    Q_UNUSED(event);

    QPainter painter(this);
    int i = 0;
    
    if(data.count() > 0)
    {
        double barWidth = (width() * 0.9) / (double)data.size();
        uint max = 0;
        double step = 0, height = this->height() * 0.75;
        QLineF horAxe(width() * 0.05, this->height()*0.9, width() * 0.95, this->height()*0.9);
        QLineF verAxe(width() * 0.05, this->height()*0.9, width() * 0.05, this->height()*0.1);
    
        for(i=0; i < data.size(); i++)
        {
            if (data[i] > max)
                max = data[i];
        }
    
        painter.drawLine(horAxe);
        painter.drawLine(verAxe);
        painter.drawText(width() * 0.04, this->height() * 0.95, "0");
        painter.drawText(width() * 0.96, this->height() * 0.95, QString::number(data.size() - 1));
    
        step = height / max;
    
        for(i=0; i < data.size(); i++)
        {
            double start = this->height()*(0.75 + 0.25 /2) - (data[i] * step);
            double startWidth = (barWidth * i) + barWidth * 0.05 + width() * 0.05;
    
            painter.fillRect(startWidth, start, barWidth * 0.9, data[i] * step, Qt::green);
            painter.drawRect(startWidth, start, barWidth * 0.9, data[i] * step);
        }
    }
    

    }

    Normally the object should be destroyed but I don't know cause it gets the widget as a parent, so coud it be possible it never get deleted???


  • Lifetime Qt Champion

    Does the memory foot print change each time you make your window bigger ? Or both making it it smaller and bigger triggers that ?



  • Both triggers that


  • Lifetime Qt Champion

    Can you run your application through Valgrind to see what it happening with the memory ?


Log in to reply
 

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