Nelston
Nelston

Reputation: 78

PyQt5: The connected slot to a clicked signal not working

first of all, sorry for my English. My mother tongue is Spanish. So, I am working on this mini project to learn python. I'm learning how to do UI using PyQt5. This application is simple, it has three inputs, one button, and one output. I'm using MVC software pattern in this app and I have my view, model, and controller in separate files.

THE PROBLEM: In the controller class I connect the only button I have to a slot called (_calculate). When I run the app and press that button the terminal should print a text so I can see if it is working. The terminal shows me nothing. Traying different sorts of things I discovered that if I do the same binding in the view class, _calculate is executed. I did a tutorial about a Calculator using PyQt5. The calculator from the tutorial work just fine using MVC, so I used that to find out if I forget or miss something but nothing obvious appear.

My controller class

class Controller:
def __init__(self, view):
    self._view = view
    self._connectSignals()


def _connectSignals(self):
    self._view.button.clicked.connect(self._calculate)


def _calculate(self):
    print('trying to calculate')

My view class

from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QSpacerItem
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import TextEdit

from PyQt5.QtGui import QPixmap

from PyQt5.QtCore import Qt
from ToolController import Controller

class UserInterface(QMainWindow):
  def __init__(self):
    super().__init__()

    self.setWindowTitle('BDO Tool')
    self.setFixedSize(450, 300)
    self._centralWidget = QWidget(self)
    self.setCentralWidget(self._centralWidget)
    self._createWindowSkeleton()


  def _createWindowSkeleton(self):
    # Vertical container who contains all the program widget
    self._generalLayout = QVBoxLayout()
    self._centralWidget.setLayout(self._generalLayout)
    self._generalLayout.setAlignment(Qt.AlignCenter)

    self._generalLayout.addLayout(self._createFirstRow())
    self._generalLayout.addLayout(self._createButton())
    self._generalLayout.addWidget(self._createAreaText())


  def _createFirstRow(self):
    hLayout = QHBoxLayout()
    spacer = QSpacerItem(20, 20, hPolicy=QSizePolicy.Expanding)
    self._inputBoxes = {
            self.INPUT_BASE_FAIL: (QPixmap(self.IMG_BASE_FAILS), QLineEdit()),
            self.INPUT_TARGET_FAIL: (QPixmap(self.IMG_TARGET_FAIL), QLineEdit()),
            self.INPUT_STACK_AMOUNT: (QPixmap(self.IMG_STACK_AMOUNT), QLineEdit()),
        }
    keys = list(self._inputBoxes.keys())

    for key, value in self._inputBoxes.items():
        pixmap, editLine = value
        label = QLabel()

        label.setPixmap(pixmap)
        editLine.setFixedWidth(40)
        editLine.setAlignment(Qt.AlignRight)

        hLayout.addWidget(label)
        hLayout.addWidget(editLine)

        if key != keys[-1]:
            hLayout.addSpacerItem(spacer)

    return hLayout


  def _createButton(self):
    self.button = QPushButton('Calculate')
    spacer = QSpacerItem(20, 20, hPolicy=QSizePolicy.Expanding)
    hLayout = QHBoxLayout()

    hLayout.addSpacerItem(spacer)
    hLayout.addWidget(self.button)
    hLayout.addSpacerItem(spacer)

    return hLayout


  def _createAreaText(self):
    self._infoDisplay = QTextEdit()
    self._infoDisplay.setEnabled(False)

    return self._infoDisplay

  INPUT_BASE_FAIL = 1
  INPUT_TARGET_FAIL = 2
  INPUT_STACK_AMOUNT = 3
  IMG_BASE_FAILS = 'img\\user25x25.png'
  IMG_TARGET_FAIL = 'img\\target25x25.png'
  IMG_STACK_AMOUNT = 'img\\stack25x25.png'

My main

import sys

from PyQt5.QtWidgets import QApplication

from ToolView import UserInterface
from ToolController import Controller


def main():
    app = QApplication(sys.argv)
    view = UserInterface()
    view.show()

    Controller(view=view)

    sys.exit(app.exec())


if __name__ == '__main__':
    main()

Upvotes: 1

Views: 904

Answers (1)

musicamante
musicamante

Reputation: 48231

The problem is that you didn't create a persistent object for the controller instance, so the instance is immediately garbage collected afterwards because it isn't referenced anywhere else.

As long as a reference to the instance exists, it will work as expected.
In this case a local variable will suffice, since app.exec() will block further processing within main, ensuring that the instance will exist until it exists.

def main():
    app = QApplication(sys.argv)
    view = UserInterface()
    view.show()

    controller = Controller(view=view)

    sys.exit(app.exec())

Nonetheless, let me tell you that while conceptually using an MVC is usually a good idea, you should use it carefully, without "exaggerating" the pattern and only if it really helps the development.

I'd like to point up one of the disadvantages of MVC:

Lack of incremental benefit – UI applications are already factored into components, and achieving code reuse and independence via the component architecture, leaving no incremental benefit to MVC.

I understand that yours is a simple and conceptual example, but it certainly is an overly complicated one for what it does. For instance:

  • you don't seem to be using a model, or at least it doesn't seem that you need a full MVC pattern to do that (one should choose an MVC whenever there's actual advantage in doing so, not "because you should use it");
  • using an MVC pattern doesn't necessarily mean that you have to use 3 classes (and 3 separate files) which might also decrease the code navigability; MVC is mostly about the way the three elements are divided in the program logic: Qt (as most frameworks that provide UI elements) already helps with that, since it provides ui elements that do almost nothing besides showing themselves, and other elements that allow interacting with them (file access, network interfaces and actual models);
  • overusing functions to create the UI is usually a bad idea, as it makes things much more complicated than they are, most importantly from the point of view of readability: while using separate functions might help in keeping your code "tidier", functions are also created for their reusability, and in your UserInterface class there are 4 functions that will most certainly be used only once.

Upvotes: 1

Related Questions