elight
elight

Reputation: 61

Running QLineEdit::SetText costs much time

I find that constructing a QLineEdit with a QString costs 0.7s on my low-end PC, while constructing a QTextEdit with a QString costs less than 50ms.

To locate the problem, I try explicit QLineEdit(const QString &, QWidget *parent = Q_NULLPTR); or void setText(const QString &); to set text, and the 2 approaches both call void QWidgetLineControl::updateDisplayText(bool forceUpdate) in the end, leading to high time cost.

Another phenomenon is that when I set text the second time in the same program, it'll be fast.

But I also find that if I use explicit QLineEdit(QWidget *parent = Q_NULLPTR); to construct the widget and use a QPushButton to set text dynamically, there is no delay before the text appears in the widget.

I'd like to know why setting text in a QLineEdit costs so much time and how I can solve it, thanks.

QtGuiTest::QtGuiTest(QWidget *parent)
{
/*QLineEdit *edit = NULL;
    QPushButton *button = NULL;*/
    resize(WW, WH);

    edit = new QLineEdit(this);
    button = new QPushButton("button", this);
    edit->setText("hello");//This is slow
    button->move(30, 30);

    QObject::connect(button, &QPushButton::clicked, this, &QtGuiTest::func);
}

void QtGuiTest::func()
{
    edit->setText("hello");//This is fast
}

Upvotes: 2

Views: 84

Answers (1)

elight
elight

Reputation: 61

With the help of the second comment, I now realize where the problem is.

Not using the debugger, I measure the time cost again. I find that if I don't set text before the window shows up, there will be nothing in the window and a 0.7s initialization will happen that shows the widgets. While setting text before the window shows up will make the new window filled with widgets, not needing to load again.

According to the research result, the 0.7s process I mentioned in the qusetion is caused by Qt's general initialization.

Upvotes: 1

Related Questions