0

I am having trouble creating custom signals and connecting them between classes.

Login Class

class LoginApp(Ui_Login_Win):
    switch_window = QtCore.pyqtSignal()

    def __init__(self):
        self.window = QtWidgets.QDialog()
        self.window.setFixedSize(348, 389)

        self.setupUi(self.window)
        self.login_btn.clicked.connect(self.validate)
        sys.exit(self.window.exec_())

    def validate(self):
        conn = sql.connect("stocks.db")
        curr = conn.cursor()

        find_user = 'SELECT username, password FROM admin WHERE username = ? AND password = ?'
        curr.execute(find_user, (self.username_entry.text(), self.password_entry.text()))
        if curr.fetchone() is not None:
            msg = QtWidgets.QMessageBox()
            msg.setIcon(QtWidgets.QMessageBox.Information)
            msg.setWindowTitle("Status")
            msg.setText("Successful!")
            msg.exec_()
            self.switch_window.emit()
        else:
            msg = QtWidgets.QMessageBox()
            msg.setIcon(QtWidgets.QMessageBox.Warning)
            msg.setWindowTitle("Status")
            msg.setText("Failed to Login!")
            msg.exec_()

Application Class (where it switches windows)

class Application(QtCore.QObject):
    def show_login(self):
        self.login = LoginApp()
        self.login.switch_window.connect(self.show_add)

After the user has successfully login, the program exits and does not proceed to the next window.

  • Is `Ui_Login_Win` a class created with pyuic? If that's the case, it won't work, as signals only properly work for QObject subclasses (QDialog, in your case), and pyuic "form classes" are simple python objects. Read more about the proper ways of those files in the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). Also, if you use `sys.exit()` the script will automatically click as soon as the dialog is closed, which is probably not the intended behavior. – musicamante Jun 27 '21 at 03:51
  • We have used qt designer to make the layout, and I intend to produce the appropriate functionality in a separate file. If that is the case that it won’t work in the generated code by pyuic, can you recommend a snippet to change window? Thank you for the information btw. – Christopher Gonzales Jun 27 '21 at 04:39
  • @ChristopherGonzales Does [this answer](https://stackoverflow.com/a/11812578/984421) provide what you want? – ekhumoro Jun 27 '21 at 09:48
  • @ChristopherGonzales I didn't say that it won't work, I said that you should use the pyuic generated file as explained in the link I gave you in order to correctly implement it. Also, the `sys.exit()` issue is still valid: when the dialog is closed, `self.window.exec_()` returns, causing `sys.exit` to execute (and then, quit python). Please edit your post and provide the ui code for the dialog. – musicamante Jun 27 '21 at 22:11
  • @musicamante we have already settled on rather making a function that destroys the previous window and instantiates the next window's class where the next UI is built. The pyqtsignal() would have been so much easier if it worked, but no luck for that. Thank you for the help though. Appreciate it! – Christopher Gonzales Jun 28 '21 at 12:09
  • That doesn't seem an optimal solution. As said, the pyqtSignal works fine, it's a standard system used on PyQt, as long as it's used properly, and the main issue is not even related to that. Long story short: it doesn't work because you're implementing it wrong (you just need to subclass from the correct superclasses, that's it!). I *strongly* suggest you to carefully read the above links, as your assumptions about "not working" are based on knowledge you seem you want to ignore, and workarounds based on insufficient knowledge almost always result in bad solutions and *terrible* problems. – musicamante Jun 28 '21 at 13:11

0 Answers0