3

I'm trying to change the text of a Qstatusbar created in a mainwindows Class from another class

I've try several thing for the web but didn't find the answer ...

class MainWindow(QMainWindow):

    def __init__(self, widget):
        QMainWindow.__init__(self)
        self.setWindowTitle("Main Windows")

        # Status Bar
        self.main_statusbar = QStatusBar()
        self.main_statusbar.showMessage("Ready")
        self.setStatusBar(self.main_statusbar)

class Widget(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.buttons_layout = QHBoxLayout()
        self.btn_enregister = QPushButton("Save")
        self.btn_enregister.clicked.connect(self.save_information)

    def save_information(self):

        if self.line_prenom.text() and self.line_nom.text():
            info_client.write(db)
        else:
            (this line)---> main_statusbar.showMessage("Fidouda")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = Widget()
    window = MainWindow(widget)
    window.resize(800, 700)
    window.show()
    sys.exit(app.exec_())

I would like to be able to modify the statusbar text created in the MainWindow class from the Widget class

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mamquam
  • 37
  • 6

1 Answers1

2

One of the advantages of Qt over other libraries are the signals that allow you to notify between objects, in this case it is the best option:

from PySide2.QtCore import Signal # <---


class MainWindow(QMainWindow):

    def __init__(self, widget):
        QMainWindow.__init__(self)
        self.setWindowTitle("Main Windows")

        # Status Bar
        self.main_statusbar = QStatusBar()
        self.main_statusbar.showMessage("Ready")
        self.setStatusBar(self.main_statusbar)
        widget.messageChanged.connect(self.main_statusbar.showMessage) # <---

class Widget(QWidget):
    messageChanged = Signal(str) # <---

    # ...

    def save_information(self):

        if self.line_prenom.text() and self.line_nom.text():
            info_client.write(db)
        else:
            self.messageChanged.emit("Fidouda") # <---
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Hi thank for the answer do I have to import something for that to work ? – Mamquam Aug 30 '19 at 18:09
  • @AntoineV I have added the import in the first line, see again – eyllanesc Aug 30 '19 at 18:10
  • Thanks a lot ! Work fine ! – Mamquam Aug 30 '19 at 18:13
  • I have another question regarding to this … If I want to send "Fidouda" and time as integer to the showMessage method how can I do that ? Ex: self.messageChanged.emit("Fidouda", 15) – Mamquam Aug 30 '19 at 18:56
  • @AntoineV change to `messageChanged = Signal(str, int)` – eyllanesc Aug 30 '19 at 18:58
  • @AntoineV 15 means 15 ms that our eyes don't see for a short time, if you want 15 seconds then change it to "15 * 1000" – eyllanesc Aug 30 '19 at 19:15
  • @AntoineV Yes I see that a little bit too late … I've tried the Signal(str, int) before I ask you but that seem to do nothing until I realise this was ms ... – Mamquam Aug 30 '19 at 19:19
  • @AntoineV I recommend you read the docs before asking https://doc.qt.io/qt-5/qstatusbar.html#showMessage : *Hides the normal status indications and displays the given message for the specified number of **milli-seconds** (timeout)* – eyllanesc Aug 30 '19 at 19:21