我在 Qt 中有一个 GUI C++ Class
,它有一个 SLOT
,它在 QGridLayout
中添加了一组文本框和标签。该数组是方形的,具体取决于变量 n
。即,如果 n == 10
那么 QGridLayout
中有 10x10
QTextbox
& QLabel
>。基本上,当用户按下 Increase 按钮时,n
的值会增加 1,相应地 QTextbox
和 QLabel
在 SLOT
中创建并添加到 QGridLayout
中。当 n
的值为 15
时,GUI 更新需要 1-2 秒。然而,当我在 SLOT
中使用 QTime
时,它显示执行该 SLOT
的实际时间约为 100 毫秒。在 Debug 模式下,我观察到 QDebug
用于打印耗时,即使 GUI 没有完全更新为更高的 n
值。我想知道为什么会有如此多的延迟以及在执行 SLOT
之后运行的是什么,以便我可以测量相同的时间。我希望我说清楚了。如果您不明白我的问题,请告诉我。
谢谢。
实际上,我现在没有确切的密码。所以我创建了一个示例代码来突出我的代码的逻辑。请忽略任何语法错误,因为我已经尽可能快地准备好了。对不起:( 所以这是代码片段:
Class A
{
private:
int n;
QList <QLabel *> *labelList;
QList <QTextEdit *> *textList;
QGridLayout *inp;
public slots:
updateGUI();
}
A::updateGUI()
{
QTime t;
t.start();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
int even = j%2;
QLabel *tempLab = new QLabel();
//some stuff for initialisation of the QLabel
QTextEdit *tempText = new QTextEdit();
//some stuff for initialisation of the QTextEdit
labelList->append(tempLab);
textList->append(tempText);
if(even == 0)
inp->AddWidget(tempLab, i, j, 5, 5, Qt::AlignCenter);
else
inp->AddWidget(tempText, i, j, 5, 5, Qt::AlignCenter);
}
}
QDebug("%d", t.elapsed());
}
PS: QTime tobj.start()
是 SLOT
& QDebug("%d", tobj.elapsed())
是 SLOT
当然是最后一行!
最佳答案
当您对小部件进行批量更新时,关闭 the updatesEnabled property .当您添加 (11*11-10*10) 个新按钮时,这会阻止 21 次单独更新。
关于c++ - 在 Qt 中创建的 GUI 滞后(由 QTime 验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14852597/