3
People come to ask for help from you, I believe very simple but that gives a bad headache for those who do not have much practice with object orientation, I have a file progressbar.py
, which since the name is already self-explanatory, contains a progressibar in pyqt4, I want to call the file class by passing a title, something like:
from modules import progressbar
progressbar.ProgressBar("TITULO AQUI")
NOTE: As it is an activity bar I do not worry about passing the % progress, I will simply leave it active while a certain process is being run, Eagerly awaiting 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 main.py
the part 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_()
Well, you just didn’t share the most important code, which is the class constructor
ProgressBar
. :) Anyway, you just add the parameter in the constructor (function__init__
), the same way you already do in your classMyCustomWidget
with the variabletitle
. (if I really understand your difficulty, because the question is not very clear...)– Luiz Vieira
Hello friend, put the code, in this test progressbar I will do something like while the process("test") exists: rodeprogressbar("test process running, wait") waiting for feedback, thank you.
– rodgomesc
Ah, your class is
ProgressBar
is actually calledMyCustomWidget
! I didn’t understand why you literally call herProgressBar
in an example at the beginning. Okay, but then I didn’t understand your problem/difficulty. You’re already passing a "title" ("Test"
) on the call of the builder, and inside you are already using it in alabel
. What’s the matter?– Luiz Vieira
the problem, is that if I call the file alone, it works, when I call another class the progressibar, opens and closes at the same instant.
– rodgomesc
P.S.: Be sure to correct the class name in the question (change
Progressbar("TITULO AQUI")
forMyCustomWidget("TITULO AQUI")
). Otherwise you can keep confusing other people. :)– Luiz Vieira
maybe something is missing in
window.show()
app.exec()
– rodgomesc
window.show()
only displays the graphical component. You at all times need to have aapp.exec()
(a single, in the main code) to allow Qt to maintain the application’s main loop. If not, it actually displays and ends.– Luiz Vieira
changed the names
– rodgomesc
@Luizvieira but the app.exec_() is in the code, even though it doesn’t run, from the following error:
QCoreApplication::exec: The event loop is already running
– rodgomesc
Yes, I tested and saw the error (had nothing to do with the lack of
exec
). I answered and hope it helps. In the future, try to prepare a [mcve] that reproduces the error. It is much easier for someone to be interested in helping you. : ) Good luck!– Luiz Vieira
@Luizvieira, thank you very much, I will check your example now, this is my first question posted on the stack, I will follow as you said.
– rodgomesc