Working with QTableWidget on PySide

def createTable(self):
   # Create table
    self.tableWidget = QTableWidget()
    self.tableWidget.setRowCount(4)
    self.tableWidget.setColumnCount(2)
    self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
    self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
    self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
    self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
    self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (3,1)"))
    self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (3,2)"))
    self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (4,1)"))
    self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (4,2)"))
    self.tableWidget.move(0,0)

    # table selection change
    self.tableWidget.doubleClicked.connect(self.on_click)

@pyqtSlot()
def on_click(self):
    print("\n")
    for currentQTableWidgetItem in self.tableWidget.selectedItems():
        print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())

References
https://pythonspot.com/pyqt5-table/

Show MessageBox on PySide

def showdialog():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Information)

   msg.setText("This is a message box")
   msg.setInformativeText("This is additional information")
   msg.setWindowTitle("MessageBox demo")
   msg.setDetailedText("The details are as follows:")
   msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
   msg.buttonClicked.connect(msgbtn)
  
   retval = msg.exec_()
   print "value of pressed message box button:", retval
  
def msgbtn(i):
   print "Button pressed is:",i.text()

References
https://pythonbasics.org/pyqt-qmessagebox/
https://www.tutorialspoint.com/pyqt/pyqt_qmessagebox.htm

Open new window on PySide

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtGui, QtCore
import sys


class Second(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)


class First(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.pushButton = QtGui.QPushButton("click me")

        self.setCentralWidget(self.pushButton)

        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        self.dialog = Second(self)

    def on_pushButton_clicked(self):
        self.dialog.show()


def main():
    app = QtGui.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

References
https://stackoverflow.com/questions/36768033/pyqt-how-to-open-new-window

Connect clicked event of Button in PySide

import sys
import os

from PySide2.QtWidgets import QApplication, QWidget, QPushButton
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class main(QWidget):
    def __init__(self):
        super(main, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.path.join(os.path.dirname(__file__), "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        self.window:QWidget = loader.load(ui_file, self)
        ui_file.close()

        # pushButton is the name of button control
        self.pushButton:QPushButton = self.window.findChild(QPushButton,"pushButton")
        self.pushButton.clicked.connect(self.clicked)

    def clicked(self):
        print("Hello")


if __name__ == "__main__":
    app = QApplication([])
    widget = main()
    widget.show()
    sys.exit(app.exec_())

References
https://www.blog.pythonlibrary.org/2018/05/30/loading-ui-files-in-qt-for-python/

Load ui file in PySide2

import sys
import os


from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class main(QWidget):
    def __init__(self):
        super(main, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.path.join(os.path.dirname(__file__), "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()

if __name__ == "__main__":
    app = QApplication([])
    widget = main()
    widget.show()
    sys.exit(app.exec_())