Call progress bar class from another file

3

I've been trying to find a way to do this, but I can not figure out how to do this, contains a progressbar in pyqt4, I want to call the class of the file passing as a parameter a title, something like:

 from modules import progressbar 

 progressbar.ProgressBar("TITULO AQUI")

NOTE: Since it is an activity bar I do not worry about passing the% progress, I will simply leave it active while a certain process is running, Looking forward to feedback from you

from PyQt4 import QtCore
from PyQt4 import QtGui
import time
import sys
import qdarkstyle

class ProgressBar(QtGui.QWidget):

    def __init__(self,title):
        super(ProgressBar, self).__init__()
        layout = QtGui.QGridLayout(self)
        self.title = title

        label = QtGui.QLabel()

        label.setText(self.title)
        label.setAlignment(QtCore.Qt.AlignCenter)
        layout.addWidget(label)
        # Create a progress bar and a button and add them to the main layout
        self.progressBar = QtGui.QProgressBar(self)
        self.progressBar.setRange(0,1)
        layout.addWidget(self.progressBar)

        self.myLongTask = TaskThread()
        self.myLongTask.taskFinished.connect(self.onFinished)
        self.onStart()

    def onStart(self):
        self.progressBar.setRange(0,0)
        self.myLongTask.start()

    def onFinished(self):
        # Stop the pulsation
        self.progressBar.setRange(0,1)


class TaskThread(QtCore.QThread):
    taskFinished = QtCore.pyqtSignal()
    def run(self):
        time.sleep(30)
        self.taskFinished.emit()

In my main function progressbar.py the part that I call progressbar is exactly this:

# -*- coding: utf-8 -*-
import sys, time
from PyQt4.QtGui import  QMessageBox, QMainWindow, QApplication
from PyQt4.uic import loadUi
import qdarkstyle

from .modules import tricks
from .modules import progressbar 


    class PyAuto(QMainWindow):
        def __init__(self):
            super(PyAuto, self).__init__()

            # instances
            self.ui = loadUi('Pyauto/views/menu.ui', self)
            self.ui.test.clicked.connect(self.test_progressbar)
            self.ui.show()

        def test_progressbar(self):

            window = progressbar.ProgressBar("Test")
            window.show()
            app.exec_()



    if __name__ == '__main__':

        app = QApplication(sys.argv)
        app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False))   # set stylesheet dark
        myWindow = PyAuto()
        app.exec_()
    
asked by anonymous 03.02.2017 / 15:16

1 answer

2

The problem is that you create (instance) your object from class ProgressBar within the slot of the button click (within the test_progressbar call). Thus, the scope of this variable is just that function (that is, it only exists as long as the function exists). Once the function is finished, the object is deleted and you see it appear and disappear.

Ideally, you have this instance in a property of your main class ( PyAuto ). For example:

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

    # instances
    self.window = progress.ProgressBar("Test") # <==== Adição
    . . . 

And then, at the click of the button, control the display of the existing class:

def test_progressbar(self):
    if not self.window.isVisible():
        self.window.show()
    else:
        self.window.hide()

If you still prefer to instantiate a new class with each click of the button, make sure that its reference is stored in the main class. Something like:

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

    # instances
    self.window = None

And then:

def test_progressbar(self):
    self.window = progressbar.ProgressBar("Test")
    self.window.show()

And do not need exec , since the call is already being made in the main function.

    
03.02.2017 / 18:00