Code Snippets: Qt PySide2

Updated: Feb 9

some layouts

vertical_layout = QtWidgets.QVBoxLayout()
horizontal_layout = QtWidgets.QHBoxLayout()
grid_layout = QtWidgets.QGridLayout()

# add a box around a layout
name_box = QtWidgets.QGroupBox('Label')
name_layout = QtWidgets.QVBoxLayout()
name_box.setLayout(name_layout)

main_layout.addWidget(name_box)

add widgets/layouts to a layout

vertical_layout.addLayout(horizontal_layout)

horizontal_layout.addWidget(widget)
grid_layout.addWidget(widget, 0, 0) # row, column
 

some widgets

name_txt = QtWidgets.QLabel('Label') # label/text
name_chk = QtWidgets.QCheckBox('Label') # check box
name_fld = QtWidgets.QLineEdit('text') # text field
name_btn = QtWidgets.QPushButton('Label') # button

# option menu
options = ['a', 'b', 'c']
name_mnu = QtWidgets.QComboBox()
name_mnu.insertItems(0, options)

# list view
name_lst = QtWidgets.QListWidget()
name_lst.addItems(options)

get and set widget values

state = name_chk.checkState()
name_chk.setCheckState(QtCore.Qt.Checked) # or use 'Unchecked'

text = name_fld.text()
name_fld.setText('text')

item = name_mnu.currentText()
name_mnu.setCurrentText('a')

item = name_lst.currentItem()

connect a command to a widget

name_chk.stateChanged.connect(self.definition)
name_fld.returnPressed.connect(self.definition)
name_btn.clicked.connect(self.definition)
name_lst.itemDoubleClicked.connect(self.definition)

customize widgets

widget.setMinimumWidth(1)
widget.setMaximumWidth(150)
widget.setMinimumHeight(1)
widget.setMaximumHeight(24)
widget.setFixedWidth(40)

widget.setToolTip('tool tip')

name_btn.setAutoDefault(0) # prevents keyboard focus
 

hide/show a widget

widget.hide()
widget.show()

disable/enable a widget

widget.setEnabled(False)
widget.setEnabled(True)
 

delete a single widget

name_layout.removeWidget(widget)
widget.setParent(None)

delete all widgets in a layout

for i in reversed(range(name_layout.count())):
    widget_to_remove = name_layout.itemAt(i).widget()
    name_layout.removeWidget(widget_to_remove)
    widget_to_remove.setParent(None)
 

divider line

div_layout = QtWidgets.QHBoxLayout()

divider = QtWidgets.QLabel('')
divider.setStyleSheet("QLabel {background-color: #3e3e3e; padding: 0; margin: 0; border-bottom: 1 solid #666; border-top: 1 solid #2a2a2a;}")
divider.setMaximumHeight(2)

div_layout.addWidget(divider)
 

basic ui start

from PySide2 import QtCore, QtWidgets, QtUiTools, QtGui

class Dialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        # define the dialog
        super (Dialog, self).__init__(parent)
        
        #self.setFixedSize(self.sizeHint()) # always keep widgets packed together
        self.setWindowTitle('UI Start') #name of the dialog
        widget = QtWidgets.QWidget()
        
            # open the dialog at mouse location
        mouse_position = QtGui.QCursor().pos()
        x = mouse_position.x()
        y = mouse_position.y()
        self.setGeometry(x, y, 250, 50) # (x,y,w,h)
        
        # add widgets/layouts here
        ex_layout = QtWidgets.QHBoxLayout()
        self.ex_txt = QtWidgets.QLabel('Hello World')
        ex_layout.addWidget(self.ex_txt)

        # add layouts to the dialog
        main_layout = QtWidgets.QVBoxLayout()
        main_layout.addLayout(ex_layout)

        widget.setLayout(main_layout)
        self.setLayout(main_layout)
        
def run():
    dialog = Dialog()
    dialog.setParent(hou.ui.mainQtWindow(), QtCore.Qt.Window) # note: change based on program (this is for houdini)
    dialog.show()
    
run()
83 views