QT - Adding widgets to horizontal layout step by step

夙愿已清 提交于 2020-01-05 07:57:10

问题


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?


回答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.




回答2:


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



来源:https://stackoverflow.com/questions/16427608/qt-adding-widgets-to-horizontal-layout-step-by-step

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!