[QT] Use of progressBar (13)

progressBar is mostly used to record program running time, file download time, etc. Today we will take a look at how to use progressBar skillfully.

1. Environment configuration

1.python 3.7.8  

You can directly enter the official website to download and install: Download Python | Python.org

2.QT Designer 

Official download path: Qt Designer Download for Windows and Mac

2. Example demonstration

1.  For convenience, Qt Designer is used directly to design the layout of the serial port interface, as follows:

 (1) This interface is designed to be a Linux file transfer function interface, so let’s start with the most basic progressBar, how to make it display the progress bar during version transfer and download, so that we can clearly see the progress execution status.

2.  Basic operations of progressBar function:

​
self.progressBar.setMinimum(0)

self.progressBar.setMaximum(100)

self.progressBar.setValue(i)

​

Let’s first understand the functions of the above three functions in English, as follows:

(1) progressBar.setMinimum(0): Progress bar. Set the minimum value (0)

(2) progressBar.setMaximum(100): Progress bar. Set the maximum value (100)

(3) progressBar.setValue(i): Progress bar. Set value (i)

Is it much easier to understand functions in this way, but not every function can be understood in this way. Indeed, the functions of the three functions above are (1. Set the minimum value of the progress bar 2. Set the maximum value of the progress bar 3. Set the value of the progress bar).

2.1 progressBar running program:

Knowing the functions of the above three functions, can we make progressBar run by adding a loop?

​
for i in range(101):
            time.sleep(1)
            self.progressBar.setValue(i)  # 发送进度条的值 信号

​

 This simple loop can indeed make our progress bar run, but how to use it in conjunction with a program? Let's look at an example of file transfer and download to have a deeper understanding.

3. Edit the .py file generated by Qt Designer:

(1) If you want to keep the file download and UI interface progressBar updated synchronously, we add multi-threading here:

self.threading_1 = Worker()
self.threading_1.progressBarValue.connect(self.copy_file)

 

(2) Progress bar update

def copy_file(self, i):
        self.progressBar.setValue(i)

(3) The progress bar executes the main program:

class Worker(QThread):

    progressBarValue = pyqtSignal(int)  # 更新进度条

    def __init__(self):
        super(Worker, self).__init__()


    def run(self):
        for i in range(101):
            time.sleep(1)
            self.progressBarValue.emit(i)  # 发送进度条的值 信号

Define the time based on our file download time and transfer the value of the progressBar setting.

(4) Main program main.py:

if __name__ == '__main__':

    app = QApplication(sys.argv)

    MainWindow = QMainWindow()
    ui = xxx.Ui_MainWindow()
    ui.setupUi(MainWindow)
    

    MainWindow.show()##显示
    test_one = threading.Thread(target=xxx)
    
    test_two = threading.Thread(target=xxx)

    sys.exit(app.exec_())##退出界面

 implement:

 

When we click download, a multi-threaded task, the file is downloaded and the value of the progressBar is updated at the same time.

 

3. Summary

Here is a simple program to illustrate how to use progressBar. Everyone is welcome to communicate and learn from each other.

@Neng

Guess you like

Origin blog.csdn.net/pengneng123/article/details/132420313