How to send data from one window to another window Pyqt5 using signals

173 views Asked by At

I am trying to send data from my login window to admin dashboard I have three files

  1. Main App which contains QApp
from PyQt5 import QtWidgets
import sys, loginScreen, adminPanel
from dbHandler import databaseHandler

try:
    dbCon = databaseHandler()
except e:
    print(e)

app = QtWidgets.QApplication(sys.argv)
window1 = loginScreen.loginScreen(dbCon)
window2 = adminPanel.adminDashboard()

window1.loginSignal.connect(window2.show)
app.exec_()
  1. Login Window which gets information username, password and date from user:
from PyQt5 import uic
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QLineEdit, QDateEdit, QPushButton, QMessageBox
    
    
    class loginScreen(QWidget):
    
        loginSignal = pyqtSignal()
    
        username = None
        password = None
        date = None
    
        unameInp = None
        passInp=None
        dateInp=None
    
        systemDateBt = None
        loginBt = None
    
        dbCon=None
    
    
        def __init__(self, dbCon):
            super(loginScreen, self).__init__()
            uic.loadUi('screens/loginscreen.ui', self)
    
            self.dbCon = dbCon
            self.getWidgets()
            self.setSignals()
    
            self.show()
    
        def getWidgets(self):
            self.unameInp = self.findChild(QLineEdit, "unameInp")
            self.passInp = self.findChild(QLineEdit, "passInp")
            self.dateInp = self.findChild(QDateEdit, "dateInp")
    
            self.systemDateBt = self.findChild(QPushButton, "systemDateBt")
            self.loginBt = self.findChild(QPushButton, "loginBt")
    
    
    
        def setSignals(self):
            self.loginBt.clicked.connect(self.login)
            self.systemDateBt.clicked.connect(self.getSystemDate)
    
    
        def getSystemDate(self):
            self.dateInp.setDateTime(QtCore.QDateTime.currentDateTime())
    
    
        def login(self):
            username = self.unameInp.text()
            password = self.passInp.text()
            authToken = self.dbCon.checkUser(username, password)
    
            if(authToken==-1):
                error = QMessageBox()
                error.setIcon(QMessageBox.Warning)
                error.setWindowTitle("Error")
                error.setText("Authentication Error")
                error.exec_()
    
            else:
                self.loginSignal.emit()
            self.close()
  1. Admin Dashboard which gets the information i.e username and date from login:
from PyQt5 import uic, QtCore
from PyQt5.QtWidgets import QMainWindow, QLabel

class adminDashboard(QMainWindow):

    def __init__(self):
        super(adminDashboard, self).__init__()
        uic.loadUi('screens/adminpanel.ui', self)

Now I want to send information from login screen to admin dashboard. I have emitted signal to approve login but how can I send information such as date and who logged in as I have two profiles i.e Admin and Cashier.

1

There are 1 answers

0
Yasin Ahmed On

I have sent data from one view by implementing a getData() method in view 1. I fetched data from login form and set it in the class attributes. I returned class attributes using getData() in the controller. I parameterized the constructor of view 2 and sent data using signal with lambda function.

app=QtWidgets.QApplication(sys.argv)
screen1 = loginScreen.loginScreen(dbCon)
screen2 = adminPanel.adminDashboard(dbCon)
data = screen1.getData()
screen1.loginSignal.connect(lambda: screen2.setScreenData(data))