问题
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