qt-interest@trolltech.com
[Top] [All Lists]

RE: How to speed up QWidget delete

Subject: RE: How to speed up QWidget delete
From: "Scott Aron Bloom"
Date: Mon, 14 Apr 2008 07:31:32 -0700
I think this definitely qualifies as a bug :)  However, I think TT will
list it as a mis-use of the API...

Since styleSheets are cascading, In theory each call belongs ontop of
each other, though Im not sure that should be true for calls to
setStyleSheet on the same widget...

In your real code, is it the same style sheet text getting set?


Well either way, Im glad  you found it

Scott

> -----Original Message-----
> From: Dave Smith [mailto:dave@xxxxxxxxxxxxxxx]
> Sent: Monday, April 14, 2008 12:27 AM
> To: qt
> Subject: Re: How to speed up QWidget delete
> 
> Scott Aron Bloom wrote:
> > It must be some sort of resource leak....
> >
> 
> I found it, and you are right! Surprisingly, it appears to be a
> Trolltech leak relating to QWidget::setStyleSheet(), but fortunately I
> have a work-around. I've pasted below a minimal example that
> demonstrates the problem.
> 
> But first, here's a description of the problem. If you call
> setStyleSheet() repeatedly on a widget, it leaks memory and increases
> the amount of time required to delete the widget. In my example, I
> construct a QLabel and call label-setStyleSheet("font-weight: bold")
on
> a 0 ms timer. The longer I let the program run, the longer it takes to
> delete the QLabel. In my example, if I call setStyleSheet 58,000
times,
> it takes 11 seconds to delete the QLabel. Watching my program run, its
> memory usage steadily climbs.  I've worked around this by checking the
> stylesheet before setting it (obviously) to not set the same
stylesheet
> twice.
> 
> Trolls, I can log this as a bug on the customer tracker, unless
someone
> objects (let me know if I've made a mistake).
> 
> Here's the program that demonstrates the problem:
> 
> 
> #include <QApplication>
> #include <QVBoxLayout>
> #include <QMessageBox>
> #include <QPushButton>
> #include <QString>
> #include <QDialog>
> #include <QTimer>
> #include <QLabel>
> #include <QTime>
> 
> #include <QtDebug>
> 
> class MyDialog : public QDialog
> {
>      Q_OBJECT
> 
>      public:
> 
>          MyDialog(QWidget *parent=0);
> 
>      private slots:
> 
>          void clicked();
>          void timerSlot();
> 
>      private:
> 
>         QLabel      *label;
>         QPushButton *button;
>         QTimer      *timer;
>         quint64      timerCount;
> };
> 
> MyDialog::MyDialog( QWidget *parent) : QDialog(parent)
> {
>     timerCount = 0;
> 
>     label  = new QLabel("Wait a while and then click", this);
>     button = new QPushButton( "Click me", this);
> 
>     connect(button, SIGNAL(clicked()),
>             this,   SLOT(clicked()));
> 
>     timer = new QTimer(this);
>     timer->start(0);
>     connect(timer, SIGNAL(timeout()),
>             this,  SLOT(timerSlot()));
> 
>     QVBoxLayout *layout = new QVBoxLayout(this);
>     setLayout(layout);
>     layout->addWidget(label);
>     layout->addWidget(button);
> }
> 
> void MyDialog::timerSlot()
> {
>     timerCount++;
>     label->setStyleSheet("font-weight: bold");
> }
> 
> void MyDialog::clicked()
> {
>     timer->stop();
> 
>     QTime deleteTime;
>     deleteTime.start();
> 
>     delete label;
> 
>     QMessageBox::information(this, "",
>             QString("After calling setStyleSheet() %L1 times, "
>                 "it took %L2 ms to delete the label.")
>             .arg(timerCount)
>             .arg(deleteTime.elapsed()));
> 
>     QApplication::quit();
> }
> 
> 
> int main(int argc, char **argv)
> {
>     QApplication app(argc, argv);
>     MyDialog dialog;
>     dialog.show();
>     return app.exec();
> }
> 
> #include "main.moc"
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>