TagPySide

PySide: QPushbutton Text below Icon

Apparently using a QPushbutton with a Icon does not work. As alternative I am going to use a QToolbutton.

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Widget(QWidget):
    
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        
        layout = QVBoxLayout(self)
        
        #QToolButton
        btn = QToolButton()
        btn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        btn.setText("test")
        btn.setIcon(btn.style().standardIcon(QStyle.SP_DirIcon))
        btn.setIconSize(QSize(54,54))
        
        layout.addWidget(btn)
        
        self.setLayout(layout)
        
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    
    dialog = Widget()
    dialog.show()

    app.exec_()

PySide: QVBoxLayout with QScrollArea

Here is an example that adds Scrollbars to a QVBoxLayout. The one thing that tripped me up was that you need to explicitly create a Widget for the QScrollArea.

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Widget(QWidget):
    
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        self.setFixedHeight(200)
        
        #Container Widget        
        widget = QWidget()
        #Layout of Container Widget
        layout = QVBoxLayout(self)
        for _ in range(20):
            btn = QPushButton("test")
            layout.addWidget(btn)
        widget.setLayout(layout)

        #Scroll Area Properties
        scroll = QScrollArea()
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll.setWidgetResizable(False)
        scroll.setWidget(widget)
        
        #Scroll Area Layer add 
        vLayout = QVBoxLayout(self)
        vLayout.addWidget(scroll)
        self.setLayout(vLayout)
        
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    
    dialog = Widget()
    dialog.show()

    app.exec_()

PySide: QTreeListWidget deleting an Item, editing an Item

This example creates a QTreeListWidget to display a folder-structure.

import sys, os
from PySide.QtCore import *
from PySide.QtGui import *

class TreeTest(QTreeWidget):
    
    def __init__(self, parent = None):
        super(TreeTest, self).__init__(parent)
        self.setColumnCount(1)
        self.setHeaderLabel("Folders")
        
        actionEdit = QAction("New Folder", self)
        actionEdit.triggered.connect(self.addItemAction)
        self.setContextMenuPolicy(Qt.ActionsContextMenu)
        self.addAction(actionEdit)
        
        actionDelete = QAction("Delete", self)
        actionDelete.triggered.connect(self.deleteItem)
        self.addAction(actionDelete)
        
        self.style()
        
    def addItem(self, name, parent):
        self.expandItem(parent) 
        item = QTreeWidgetItem(parent)
        item.setText(0, name)
        #It is important to set the Flag Qt.ItemIsEditable
        item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsEditable)

        item.setIcon(0,self.style().standardIcon(QStyle.SP_DirIcon))
        return item
    
    def addItemAction(self):
        parent = self.currentItem()
        if parent is None:
            parent = self.invisibleRootItem()
        new_item = self.addItem("New Folder", parent)
        self.editItem(new_item)
    
    def deleteItem(self):
        root = self.invisibleRootItem()
        for item in self.selectedItems():
            (item.parent() or root).removeChild(item)
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    test = QWidget()
    treeWidget = TreeTest()
    layout = QHBoxLayout()
    layout.addWidget(treeWidget)
    
    test.setLayout(layout)
    test.show()
    
    treeWidget.addItem("top", treeWidget.invisibleRootItem())
    item = treeWidget.addItem("item", treeWidget.invisibleRootItem())
    treeWidget.addItem("subitem", item)
    
    app.exec_()

PySide: Using standard system Icons

You can add Icons to many types of QWidgets. PySide provides you with a method to access the native system icons.
A complete list of icons can be found here: PySide.QtGui.PySide.QtGui.QStyle.StandardPixmap

To apply the QIcon you need to

btn = QPushButton("Folder")
style = btn.style()
icon = style.standardIcon(QStyle.SP_DirIcon)
btn.setIcon(icon)
#or short:
btn.setIcon(btn.style().standardIcon(QStyle.SP_DirIcon))

Here a small example:

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Widget(QWidget):
    
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        
        
        btn_folder = QPushButton("Folder")
        btn_folder.setIcon(self.style().standardIcon(QStyle.SP_DirIcon))
        
        btn_one = QPushButton("Play")
        btn_one.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        
        btn_two = QPushButton("Stop")
        btn_two.setIcon(self.style().standardIcon(QStyle.SP_MediaStop))
        
        btn_three = QPushButton("Pause")
        btn_three.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
        
        layout = QHBoxLayout()
        layout.addWidget(btn_folder)
        layout.addWidget(btn_one)
        layout.addWidget(btn_two)
        layout.addWidget(btn_three)
        
        self.setLayout(layout)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    
    dialog = Widget()
    dialog.show()

    app.exec_()

PySide: QMessageBox with QCheckBox

The QMessageBox is very useful for creating standard filesystem dialogs (it even comes with alert sounds) and is limited to display an icon, text and standard buttons. If you want to add something extra like a checkbox for “do not ask me again”, you need to extend the base class.

PySide: Rightclick Button Menu

PySide (www.pyside.org) is an awesome Framework that allows you to use Qt with Python. Installation is as straightforward as typing

pip install pyside

I will be creating a menu that opens with a right click.
This menu is called “Context Menu” in QT.
In this example the right click menu on the button removes the button at runtime.

© 2017 Neal Bürger

Theme by Anders NorénUp ↑