QT - Adding widgets to horizontal layout step by s

2019-08-12 17:05发布

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);
    }
}

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.

But I want function to add widgets step bu step.

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

Is it possible?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-12 17:47

i would implement a timer , that gives the UI a chance to refresh between each shot

查看更多
欢心
3楼-- · 2019-08-12 18:02

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.

查看更多
登录 后发表回答