0

I'm trying to make the loading ui. I want to show the loading ui when the operation starts in the thread, and when the thread operations is completed, I want to hide the loading ui.

class TableThread(QThread):
    set_signal = pyqtSignal(int)
    stop_signal = pyqtSignal()

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

    def run(self):
        for i in range(10):
            time.sleep(0.5)
            self.set_signal.emit(i)
        self.stop_signal.emit()

class Test(QDialog, form_settings_ui):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.tableWidget.setRowCount(0)
        self.movie = QMovie('loading.gif', QByteArray(), self)
        self.movie.setCacheMode(QMovie.CacheAll)
        self.label.setMovie(self.movie)

        self.pushButton.clicked.connect(self.clicked_push_button)

    def set_table_list(self, i):
        self.tableWidget.insertRow(i)
        self.tableWidget.setItem(i, 0, QTableWidgetItem(str(i)))
        self.tableWidget.setItem(i, 1, QTableWidgetItem(str(i)))
        self.tableWidget.resizeColumnsToContents()

    def clicked_push_button(self):

        self.table_thread = TableThread(self)
        self.table_thread.set_signal.connect(self.set_table_list)
        self.table_thread.stop_signal.connect(self.stop_loading)
        self.table_thread.start()
        self.movie.start()

    def stop_loading(self):
        self.movie.stop()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        perforce_ui_window = Test()
        perforce_ui_window.show()
        app.exec_()

This is the test code.

I put in the self.movie.stop () code, but it just only stopped moving, it didn't disappear.

  • Where did you put it? There's no trace of `stop()` in your code avove. – musicamante Apr 28 '22 at 12:15
  • Sorry, I uploaded the code that deleted the stop() function. I put the stop() code in run() function. but it just only stopped moving gif, it didn't disapper(hide) – seonguk.jeon Apr 29 '22 at 01:14
  • You cannot access UI elements (including QMovie, which is not thread-safe) from external threads. Add `self.finished.connect(self.movie.stop)` at the end of the `__init__`. – musicamante Apr 29 '22 at 01:16
  • @musicamante, I added the stop code you said, but It still doesn't disappear... just stop moving. I modified the code above, could you take a look at it? – seonguk.jeon Apr 29 '22 at 01:46
  • The concept is still the same: `self.table_thread.finished.connect(self.label.hide)`. Note: you don't need a `stop_signal`, as `finished` already does that when `run` returns. – musicamante Apr 29 '22 at 02:23
  • Thanks a lot, I didn't know the "self.table_thread.finished.connect". I modified the code and checked it out to disappear. – seonguk.jeon Apr 29 '22 at 04:25
  • That's absolutely the same as `self.finished` pointed out in my previous comment. That `self` refers to the current `TableThread` instance, just as like as `self.table_thread` is. Please do some research on the meaning of `self` in python and what classes and instance are and how they work. – musicamante Apr 29 '22 at 04:34

0 Answers0