Display message(Qmessagebox) from a class other than the main one in Pyqt4

Asked

Viewed 165 times

2

Friends, I ask for help from you, I imagine it is simple but I am having difficulty, because I am still learning object orientation with Pyqt4. I have a main class (Application) separate from the class that forms the main screen (in the ui_main.py file) of the system and another (Treaticicle) that has a specific function of checking if there is a path to a directory and if there is one, creates a folder inside it, but I want to make an error message appear in a window(widget) if there is no path. Follow the code below:

Main class in the file (update_weekly.py)

from PyQt4.QtGui import  (QApplication,QMessageBox)
from PyQt4.QtCore import (SIGNAL, QTimer, QString)
#-------------------------
from ui_principal import *
from tratar_diretorio import *

class Aplicacao(QtGui.QMainWindow, Ui_JanelaPrincipal):
    def __init__(self, parent=None):
        super(Aplicacao, self).__init__(parent)
        self.setupUi(self)
        self.animacao_titulo()
        self.pushButton_sair.clicked.connect(self.sair)
        self.pushButton_processar.clicked.connect(self.processar_sistema)
    #----------------------------------------
    def sair(self):
        sys.exit()
    #----------------------------------------
    def processar_sistema(self):
        c = TrataDiretorio()
        if c.verificar_criar_diretorio():
            self.pushButton_processar.setEnabled(False)
#------------------------------
if __name__ == "__main__":
    app = QApplication([sys.argv])
    app.processEvents()
    w = Aplicacao()
    w.show()
    w.raise_()
    sys.exit(app.exec_())  

Treatdirectory class in the file (treat_directory.py)

import sys
import os.path
import time
#--------------------
class TrataDiretorio():
    def __init__(self):
        self.data                = None
        self.pasta               = None
        self.diretorio           = None
        self.status              = True
        self.log                 = open('log.txt', 'w+')
    #---------------------------------------
    def verificar_criar_diretorio(self):
        try:
            if os.path.isdir(self.diretorio): # vemos se este dir já existe
                os.mkdir(self.pasta)
                time.sleep(2)
                self.log.write('verificar_criar_diretorio: \n')
                self.log.write('-----------------------------\n')
                self.status = True
                return self.status
            else:
======>         QMessageBox.information(self, u"Informação", u'O caminho <b>"%s"</b> para criação da pasta <b>"%s"</b> não existe!'%(self.diretorio,self.data))
                self.status = False
                return self.status              
        except Exception as erro:
            self.log.write('verificar_criar_diretorio: \n%s\n'%erro)
======>     QMessageBox.critical(self, "Erro", erro)
            self.status = False
            return self.status  
  • People, seeing some similar questions but with theads in stackoverflow in English, I saw that it was enough to create a class and inherit from the Qmessagebox class, with this, just use the methods of this class. Then in the filename tratar_directorio.py, before the Treatdirectory class, I set the Message class.

  • class Mensagem(QMessageBox): &#xA; def __init__(self, parent=None): &#xA; super(Mensagem, self).__init__(parent)

  • In the check_crea_directory method, I entered the class and used its methods, it is important not to forget the method ". exec()"

  • msg = Mensagem() texto = u'O caminho <b>"%s"</b> para criação da pasta <b>"%s"</b> não existe!'%(self.diretorio, self.data) msg.setWindowTitle("Informação") msg.setIcon(QMessageBox.Information) msg.setText(texto) msg.setStandardButtons(QMessageBox.Ok) msg.exec_()

  • I am unable to format the above code snippets.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.