我有一个函数,它根据用户通过QCheckBoxes选择的技术编写报告。根据选择的技术数量,QProgressBar将通过设置值,直到函数达到完成为止。
例如:
1 tech: 0 -> 99 -> 100
2 tech: 0 -> 50 -> 99 -> 100
3 tech: 0 -> 33 -> 66 -> 99 -> 100
4 tech: 0 -> 25 -> 50 -> 75 -> 99 -> 100不管选择了多少技术,一旦处理了最后一个技术,进度条将被设置为99,以便在到达100之前进行文本清理。
目前,我有两个这样的技术(PV和风力涡轮机),我手动设置进度值。
我如何以更系统的方式实现上述目标?
我现在的代码是:
def report(self):
# Define progress bar
progressMessageBar = iface.messageBar().createMessage(str('Generating report...'))
progress = QProgressBar()
progress.setMaximum(100)
progress.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
progressMessageBar.layout().addWidget(progress)
iface.messageBar().pushWidget(progressMessageBar, iface.messageBar().INFO)
progress.setValue(0)
sleep(5)
# Define Report text browser
display_report = self.report_dockwidget.report_textBrowser
# Technology checkboxes
pv_checkBox = self.pv_generation_dockwidget.returnResults_Checkbox
wind_checkBox = self.wind_generation_dockwidget.returnResults_Checkbox
# For PV
if pv_checkBox.isChecked():
# Do some processing
progress.setValue(50)
if not wind_checkBox.isChecked():
progress.setValue(99)
sleep(5)
# For Wind
if wind_checkBox.isChecked():
# Do some processing
if not pv_checkBox.isChecked():
progress.setValue(50)
else:
progress.setValue(99)
sleep(5)
# Do some cleanup
progressMessageBar.setText('Complete')
progress.setValue(100)
sleep(1)
# Show report
self.report_dockwidget.show()发布于 2017-12-05 13:45:53
一种方法是将所有技术人员聚集到一个队列中,并在队列中进行迭代,并在执行过程中更新进度。
由于我的Python是Rust-y (很抱歉使用那个双关语),下面是一些伪代码:
# Setup progress bar et cetera
# Gather techs in a queue
techs = Queue()
if pv_checkBox.isChecked():
techs.add(pv_task)
if wind_checkBox.isChecked():
techs.add(wind_task)
# Do the work
total_tasks = techs.count()
done_tasks = 0
while not techs.isEmpty():
task = techs.take()
task.doWork()
done_tasks += 1
new_progress = max(done_tasks / total_tasks * 100, 99)
progress.setValue(newProgress)
# Progress is now 99
doFinalStuff()
progress.setValue(100)这将以FIFO的方式完成所有任务,并按您的意愿设置进度,最后一步始终是99。
现在,最好将所有复选框收集到一个组或布局中,并通过获取和过滤所有子元素,通过循环添加技术。
发布于 2017-12-07 10:52:04
多亏了@PhilKiener的S的回答,我用Python2.7的相关方法翻译了他的伪代码,效果很好:
# Setup progress bar et cetera
# Gather techs in a queue
techs = Queue.Queue()
if pv_checkBox.isChecked():
techs.put(pv_task)
if wind_checkBox.isChecked():
techs.put(wind_task)
# Do the work
total_tasks = len(techs.queue)
done_tasks = 0
while not techs.empty():
task = techs.get()
done_tasks += 1
new_progress = int(ceil(float(done_tasks) / float(total_tasks) * 99))
progress.setValue(new_progress)https://codereview.stackexchange.com/questions/181979
复制相似问题