Qt

PySide: QVBoxLayout with QScrollArea

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_()
Posted by happyneal in Python, 1 comment
PySide: QTreeListWidget deleting an Item, editing an Item

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_()
Posted by happyneal in Python, 0 comments
PySide: Using standard system Icons

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_()
Posted by happyneal in Python, 0 comments
PySide: Rightclick Button Menu

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.

Posted by happyneal in Python, 0 comments