How to add a progress bar?

前端 未结 4 1813
误落风尘
误落风尘 2020-12-15 12:22

I have been trying to understand how to add a progress bar, I can create one within the GUI I am implementing and get it to appear but even after checking through http://doc

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 12:58

    Your question is a bit vague, but it sounds to me like you want the progress bar to show progress for a specific running method, which I'll call the "work()" method. Unfortunately, there's no way to just pass a reference to your method to a progress bar - your method needs to explicitly tell the progress bar what to display. Here's what I would do:

    1. Make the progress bar's reference available to work() - either pass it in as an argument to work(), or provide an accessor method that your code in work() can call to get a reference to the progress bar.

    2. Inside work(), after you've obtained a reference to the progress bar (which I'll call "pb", call pb.setMinimum(0) and pb.setMaximum(n) where n is the number of steps your method has to get through.

    3. As your method completes each step, call pb.setValue(pb.getValue()+1);

    4. At the end of your method, call pb.setValue(0); to reset the progress bar prior to returning.

    Also, if you want your progress bar to display a String message, you first have to call pb.setStringPainted(true), then subsequent calls to pb.setString(string) will show up on the progress bar.

提交回复
热议问题