Up until now, we have only deal with the aesthetic aspec of the GUI. Now, we will bind logics to our widgets. Mainly, we will populate the createConnects() function.

    def createConnects(self):
        self.button.clicked.connect(self.updateText)
        
    def updateText(self):
        self.button.setText("Updated!")

Upon button click, the updateText() function will be called, and the button text will be rewritten as “Updated!”. We can add a decorater @Slot to the updateText() that improves the memory management from the Qt Framework. Also, the decorator improves the code readability.

from PySide2.QtCore import Slot 

   ...
    
    @Slot(str)
    def updateText(self):
        self.button.setText("Updated!")

In the previous post regarding menubar, you can add multiple .triggered.connect() functionality to the widgets.

    def createConnects(self):
        self.actionAbout.triggered.connect(self.showInfo)
        
    def showInfo(self):
        print("Clicked show Info")

Adding connectivity to the Stacked Layout

In the previous layout post, we demonstrated the stacked layout. Here, we will extend that example with connectivities.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PySide2 import QtWidgets
class MainWindow(QtWidgets.QWidget):
    
    def __init__(self, *args):
        QtWidgets.QWidget.__init__(self, *args)
        self.createMenu()
        self.createComponents()
        self.createLayout()
        self.createConnects()
        self.setWindowTitle(self.tr("Name"))
    
    def createMenu(self):
        pass
    
    def createLayout(self):
        # declare a QVBox
        mainLayout = QtWidgets.QVBoxLayout()
    
        # declare a stacked layout
        self.stackLayout = QtWidgets.QStackedLayout()
        # First Page Layout
        self.stackLayout.addWidget(self.nameLabel)
        
        # Second Page Layout
        horizontalLayout = QtWidgets.QHBoxLayout()
        horizontalLayout.addWidget(self.line)
        horizontalLayout.addWidget(self.button)
        secondPage = QtWidgets.QWidget()
        secondPage.setLayout(horizontalLayout)
        self.stackLayout.addWidget(secondPage)
        
        mainLayout.addWidget(self.comboStack)
        mainLayout.addLayout(self.stackLayout)
        
        self.setLayout(mainLayout)
        
    def createComponents(self):
        self.nameLabel = QtWidgets.QLabel(self)
        self.nameLabel.setText('Name')
        self.line = QtWidgets.QLineEdit(self)
        self.button = QtWidgets.QPushButton('OK', self)
        
        self.comboStack = QtWidgets.QComboBox()
        self.comboStack.addItem("Page 1")
        self.comboStack.addItem("Page 2")
        
    def createConnects(self):
        self.comboStack.currentIndexChanged.connect(
            self.stackLayout.setCurrentIndex)

def main(argv):
    app = QtWidgets.QApplication(argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())
    
if __name__ == "__main__":
    main(sys.argv)

How to pass data to the connected function?

def createConnects(self):
    self.actionAbout.triggered.connect(lambda: self.showInfo(4))

def showInfo(self, data):
    print("Clicked show Info data: " + str(data))

Another interesting example is a question I answered in Stackoverflow, and yet demonstrate the signal passing in a PySide2 application. In the question, the user wants to implement a GUI that highlight the next QLineEdit after the current QLineEdit is being written.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PySide2 import QtWidgets


class MainWindow(QtWidgets.QWidget):
    
    def __init__(self, *args):
        QtWidgets.QWidget.__init__(self, *args)

        self.setWindowTitle("Keyword") 
        self.anchor = []
		
		# Line Edit 1
        self.nameLabel = QtWidgets.QLabel(self)
        self.nameLabel.setText('#1')
        self.line1 = QtWidgets.QLineEdit(self)
        self.line1.move(80, 20)
        self.line1.resize(200, 32)
        self.nameLabel.move(20, 20)
        self.anchor.append(self.line1)
		
		# Line Edit 2
        self.nameLabel2 = QtWidgets.QLabel(self)
        self.nameLabel2.setText('#2')
        self.line2 = QtWidgets.QLineEdit(self)
        self.line2.move(80, 60)
        self.line2.resize(200, 32)
        self.nameLabel2.move(20, 60)
        self.anchor.append(self.line2)
		
		# Line Edit 3
        self.nameLabel3 = QtWidgets.QLabel(self)
        self.nameLabel3.setText('#3')
        self.line3 = QtWidgets.QLineEdit(self)
        self.line3.move(80, 100)
        self.line3.resize(200, 32)
        self.nameLabel3.move(20, 100)
        self.anchor.append(self.line3)
		
		# Line Edit 4
        self.nameLabel4 = QtWidgets.QLabel(self)
        self.nameLabel4.setText('#4')
        self.line4 = QtWidgets.QLineEdit(self)
        self.line4.move(80, 140)
        self.line4.resize(200, 32)
        self.nameLabel4.move(20, 140)
        self.anchor.append(self.line4)
		
		# Button
        pybutton = QtWidgets.QPushButton('OK', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(200,32)
        pybutton.move(80, 200)        

        # Connect
        self.line1.textEdited.connect(lambda state, x=1: self.checkDigit(x))
        self.line2.textEdited.connect(lambda state, x=2: self.checkDigit(x))
        self.line3.textEdited.connect(lambda state, x=3: self.checkDigit(x))
        self.line4.textEdited.connect(lambda state, x=4: self.checkDigit(x))
		
    def checkDigit(self, data):
        try:
            if (int(self.anchor[data-1].text()) < 10):
                self.anchor[data].setFocus()
        except:
            pass
            
		
    def clickMethod(self):
        print("Your four digits are:" + 
		      " " + self.line1.text() + 
		      " " + self.line2.text() +
			  " " + self.line3.text() +
			  " " + self.line4.text())

def main(argv):
    app = QtWidgets.QApplication(argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())
if __name__ == "__main__":
    main(sys.argv)

Leave a comment

Your email address will not be published. Required fields are marked *