本文介绍了QT-逐步将小部件添加到水平布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平布局,当用户输入一个数字时,我会将那个数量的小部件(包含图片)添加到该布局中.

I have a horizontal layout and when user enters a number, I am adding that number of widgets (which contain picture) to that layout.

void MainWindow::on_pushButton_2_clicked()
{
    for(int i=0; i<count; i++)
    {
        ui->horizontalLayout_4->addWidget(label);
    }
}

例如,如果用户输入100,则此函数循环100次,并在函数执行完毕后,同时添加100个小部件.

For example, if user enters 100, this function loop 100 times and after the function finishes its execution, it adds 100 widgets at the same time.

但是我想要函数在步骤bu步骤中添加小部件.

But I want function to add widgets step bu step.

例如,当i = 0时,它添加;当i = 1时,它添加..用户应该逐步看到添加的项.

For example, when i=0, it adds, when i=1 it adds.. And user should see the adding items step by step.

有可能吗?

推荐答案

on_pushButton_2_clicked中,您可以启动QTimer,该QTimer连接到添加了单个小部件的插槽.给计时器一个合理的超时时间,以便您可以看到"正在添加的每个小部件.然后在课堂上使用一个计数器,以便您知道何时停止计时器.因此,如果用户输入10,则将计数器设置为10,并在每次触发计时器时从中减去1.当计数器达到零时,停止计时器.

In on_pushButton_2_clicked you could start a QTimer, connected to a slot that adds a single widget. Give the timer a reasonable timeout so that you can "see" each widget being added. Then use a counter in your class so that you know when to stop the timer. So, if the user entered 10, set the counter to 10 and subtract one from it each time the timer is fired. Stop the timer when the counter reaches zero.

这篇关于QT-逐步将小部件添加到水平布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:20