Python QT6 status bar

I would like to use the Python QT6 statusBar to show the progress in a download process. In a loop I request several files to be downloaded. I would like to see the progress for each file downloaded. In the following code I see only the very last message (last file downloaded)

def darkDownloadButtonClicked(self):
self.statusBar.showMessage(“start download”)

    url = configs.get("Url").data
    files = [self.fileList.item(x).text() for x in range(self.fileList.count())]
    
    selectedDirectory = self.dirCombo.currentText()
    xDirectory =  selectedDirectory
    darkDirectory =  "darks"
    
    createDirectory(darkDirectory)
    self.statusBar.showMessage("directory dark created")
    
    for f in files:
        downloadFile(url, f, darkDirectory)
        self.statusBar.showMessage(f)
    
    cursor = QCursor(Qt.CursorShape.ArrowCursor)
    QApplication.changeOverrideCursor(cursor)  

    dlg = QMessageBox(self)
    dlg.setWindowTitle(configs.get("downloadLabel").data)
    dlg.setText(configs.get("downloadEndedLabel").data)
    button = dlg.exec()

    button = QMessageBox.StandardButton(button)
    
    self.downLoadButton.setDisabled(True)

Hi @Ahmad
Can you show runnable code, then O can see what is going on or just debug it.

Here is the code strongly simplified. You can use any url to get a reasonable number of files.

import sys

from PyQt6.QtCore import Qt, QSize

from PyQt6.QtGui import QAction, QPixmap

from PyQt6.QtWidgets import (
QMainWindow, QLabel
)

from mepMicroObservatory import viewMicroObservatoryExoplanetFilesWindow

class MainWindow(QMainWindow):
def init(self):
super().init()

    self.setWindowTitle("app")
    self.setMinimumSize(QSize(500, 300)) 

    label = QLabel(self)
    pixmap = QPixmap('img/exoplanet.jpg')
    label.setPixmap(pixmap)
    self.resize(pixmap.width(), pixmap.height())
    label.setAlignment(Qt.AlignmentFlag.AlignCenter)

    self.setCentralWidget(label)


    fileAction = QAction("file",self)
    fileAction.triggered.connect(self.fileActionClick)
       
  
    microObservatoryAction = QAction("Website", self)
    microObservatoryAction.triggered.connect(self.microObservatoryActionClick)
    
    viewMicroObservatoryExoplanetAction = QAction("view Observations", self)
    viewMicroObservatoryExoplanetAction.triggered.connect(self.viewMicroObservatoryExoplanetActionClick)



    menu = self.menuBar()

    file_menu = menu.addMenu("file")
    file_menu.addAction(fileAction)
    file_menu.addSeparator()
    
    mo_menu = menu.addMenu("download")
    mo_menu.addAction(viewMicroObservatoryExoplanetAction)

    

def fileActionClick(self):
    print("file action clicked")
    
def microObservatoryActionClick(self):
    print("file action clicked")
    
def viewMicroObservatoryExoplanetActionClick(self):
    self.w = viewMicroObservatoryExoplanetFilesWindow()
    self.w.setWindowTitle("view Observations")
    self.w.show()

from PyQt6.QtWidgets import QWidget, QFormLayout, QComboBox, QListWidget,
QDateTimeEdit, QPushButton, QMessageBox, QApplication

from mepDA import getUniqueStars, getFileNamesFromImageDirectory, createDirectory
from PyQt6.QtCore import QDate
from PyQt6.QtGui import QCursor

unique_stars = getUniqueStars()

class viewMicroObservatoryExoplanetFilesWindow(QWidget):
def init(self):
super().init()

    layout = QFormLayout()

    self.starLabel = QLabel("star")
    self.starLabel.setStyleSheet("color:blue;")
    
    self.starCombo = QComboBox()
    self.starCombo.addItem("*")
    self.starCombo.addItems(unique_stars)
    self.starCombo.currentTextChanged.connect(self.starComboViewTxtChanged)
    
    self.fitsFilesLabel = QLabel("fits Files")
    self.fitsFilesLabel.setStyleSheet("color:blue;")
    
    
    self.dateLabel = QLabel("date")
    self.dateLabel.setStyleSheet("color:blue;")
    self.date = QDateTimeEdit(self)
    self.date.setDate(QDate.currentDate())
    self.date.setDisplayFormat("dd.MM.yyyy")        
    
    self.fileList = QListWidget()
    self.fileList.itemDoubleClicked.connect(self.fileListItemDoubleClicked)
    
    self.refreshButton = QPushButton("refresh File List")
    self.refreshButton.clicked.connect(self.refreshButtonClicked)

    
    self.observationLabel = QLabel("number of Observations")
    self.observationLabel.setStyleSheet("color:blue;")
    self.observations = QLabel(str(self.fileList.count()))
    
    self.downLoadButton = QPushButton("download Files")
    self.downLoadButton.clicked.connect(self.fitsDownloadButtonClicked)
    self.downLoadButton.setDisabled(True)
    
    self.statusLabel = QLabel(" ")

    layout.addRow(self.starLabel, self.starCombo)
    layout.addRow(self.dateLabel, self.date)
    layout.addRow(self.fitsFilesLabel, self.fileList)
    layout.addRow("", self.refreshButton)
    layout.addRow(self.observationLabel, self.observations)
    layout.addRow("", self.downLoadButton)
    layout.addRow("downloaded files: ", self.statusLabel)
    
    self.setLayout(layout)
    
    
def fileListItemDoubleClicked(self):
    selectedIndex = self.fileList.currentItem().text()
    print(selectedIndex)
    
    
def starComboViewTxtChanged(self, selectedStar):
    self.downLoadButton.setDisabled(True)
    self.refreshButton.setEnabled(True)

    
def refreshButtonClicked(self, selectedStar):
    cursor = QCursor(Qt.CursorShape.WaitCursor)
    QApplication.setOverrideCursor(cursor)
    
    date = self.date.text()
    day = date[0:2]
    month = date[3:5]
    year = date[8:10]
    
    selectedStar = self.starCombo.currentText()
    star = selectedStar
    
    if selectedStar.startswith("HAT-P"):
        star = selectedStar.replace("HAT-P", "HATP")
        
    myDate = str(year) + str(month) + str(day)
    print("selected star: ", star, " date: " , myDate)
    
    fList = getFileNamesFromImageDirectory(star)

    self.fileList.clear()
    
    i = 0
    for f in fList:
        if myDate in f:
            i += 1
            if star == "*":
                self.fileList.addItem(f)
                
            elif f.startswith(star):
                self.fileList.addItem(f)
            
    self.observations.setText(str(i))
    self.downLoadButton.setEnabled(True)
    self.refreshButton.setDisabled(True)
    
    cursor = QCursor(Qt.CursorShape.ArrowCursor)
    QApplication.changeOverrideCursor(cursor)  
    
    
def fitsDownloadButtonClicked(self):
    print("fits download running")
    
    day = self.date.text()[0:2]
    month = self.date.text()[3:5]
    year = self.date.text()[8:10]
    
    dsDirectory = "microDirectory" + "/" + self.starCombo.currentText() + year + month + day
    createDirectory(dsDirectory)
    
    url = "https://mo-www.cfa.harvard.edu/ImageDirectory"
    files = [self.fileList.item(x).text() for x in range(self.fileList.count())]
    
    i = 0
    for f in files:
        downloadFile(url, f, dsDirectory)
        i += 1
        message = "file " + str(i) + "/" + str(len(files)) + " downloaded : "
        self.statusLabel.setText(message + f)

        print("file ", "{:02d}".format(i), "/", len(files), " downloaded : ", f)
        #print("{:02d}".format(i))
        
    dlg = QMessageBox(self)
    dlg.setWindowTitle("download ")
    dlg.setText("download Ended")
    button = dlg.exec()

    button = QMessageBox.StandardButton(button)
    print(i, " files downloded")

def downloadFile(url, file, directory):
import requests

file_path = url + '/' + file
download_path = directory + '/' + file

print(file_path, " ---> ", download_path)

response = requests.get(file_path)

if response.status_code == 200:
    with open(download_path, 'wb') as f:
        f.write(response.content)
        #print('File downloaded successfully: ', file)
else:
    print('Failed to download file: ', download_path)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

Your code just get spitted so, here is my short example how you can do it.
Just replace file_urls list with correct and note right now files are downloaded to current directory.

import os
import sys

import requests
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QLabel, QWidget, QStatusBar
from PySide6.QtCore import QThreadPool, QRunnable, Signal, QObject


class WorkerSignals(QObject):
    progress = Signal(str)
    completed = Signal(str)


class FileDownloadWorker(QRunnable):
    def __init__(self, url, save_path, signals):
        super().__init__()
        self.url = url
        self.save_path = save_path
        self.signals = signals

    def run(self):
        try:
            self.signals.progress.emit(f"Starting download: {self.url}")
            response = requests.get(self.url, stream=True)
            response.raise_for_status()
            print(f"Content-Type: {response.headers.get('Content-Type')}")
            with open(self.save_path, "wb") as file:
                for chunk in response.iter_content(chunk_size=8192):
                    file.write(chunk)
            self.signals.completed.emit(f"Completed: {self.save_path}")
        except Exception as e:
            self.signals.completed.emit(f"Failed: {self.url} ({e})")


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Parallel File Downloader")

        self.layout = QVBoxLayout()

        self.label = QLabel("Click 'Start Download' to begin.")
        self.layout.addWidget(self.label)

        self.start_button = QPushButton("Start Download")
        self.start_button.clicked.connect(self.start_download)
        self.layout.addWidget(self.start_button)

        self.status_bar = QStatusBar()
        self.setStatusBar(self.status_bar)

        container = QWidget()
        container.setLayout(self.layout)
        self.setCentralWidget(container)

        self.thread_pool = QThreadPool()
        self.total_files = 0
        self.completed_files = 0
        self.pending_files = 0

    def start_download(self):
        self.start_button.setEnabled(False)

        file_urls = [
            "https://www.exmapple.com/file1.txt",
            "https://www.exmapple.com/file2.txt",
            "https://www.exmapple.com/file3.txt",
        ]

        save_paths = [os.path.basename(url) for url in file_urls]
        self.total_files = len(file_urls)
        self.pending_files = self.total_files
        self.completed_files = 0

        for url, save_path in zip(file_urls, save_paths):
            signals = WorkerSignals()
            signals.progress.connect(self.update_status)
            signals.completed.connect(self.file_completed)

            worker = FileDownloadWorker(url, save_path, signals)
            self.thread_pool.start(worker)

    def update_status(self, message):
        self.status_bar.showMessage(message)

    def file_completed(self, message):
        self.completed_files += 1
        self.pending_files -= 1
        self.update_status(f"{message} | Completed: {self.completed_files}, Pending: {self.pending_files}")

        if self.completed_files == self.total_files:
            self.status_bar.showMessage("All downloads completed!")
            self.start_button.setEnabled(True)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())