analitics

Pages

Friday, November 9, 2018

Python Qt5 : default icons with QStyle.

This is a simple example of QStyle.
Using the QStyle can solve more issues above this example.
The QStyle can solve more problems than this example.
You can change everything in PyQt5 and Qt.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI, read here.
This is the result of a source code I used to show you how can use default icons from PyQt5.

See this source code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Widget(QWidget):
  
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        icons = [
'SP_TitleBarMinButton', 
'SP_TitleBarMenuButton', 
'SP_TitleBarMaxButton', 
'SP_TitleBarCloseButton', 
'SP_TitleBarNormalButton', 
'SP_TitleBarShadeButton', 
'SP_TitleBarUnshadeButton', 
'SP_TitleBarContextHelpButton', 
'SP_MessageBoxInformation', 
'SP_MessageBoxWarning', 
'SP_MessageBoxCritical', 
'SP_MessageBoxQuestion', 
'SP_DesktopIcon', 
'SP_TrashIcon', 
'SP_ComputerIcon', 
'SP_DriveFDIcon', 
'SP_DriveHDIcon', 
'SP_DriveCDIcon', 
'SP_DriveDVDIcon', 
'SP_DriveNetIcon', 
'SP_DirHomeIcon', 
'SP_DirOpenIcon', 
'SP_DirClosedIcon', 
'SP_DirIcon', 
'SP_DirLinkIcon', 
'SP_FileIcon', 
'SP_FileLinkIcon', 
'SP_FileDialogStart', 
'SP_FileDialogEnd', 
'SP_FileDialogToParent', 
'SP_FileDialogNewFolder', 
'SP_FileDialogDetailedView', 
'SP_FileDialogInfoView', 
'SP_FileDialogContentsView', 
'SP_FileDialogListView', 
'SP_FileDialogBack', 
'SP_DockWidgetCloseButton', 
'SP_ToolBarHorizontalExtensionButton', 
'SP_ToolBarVerticalExtensionButton', 
'SP_DialogOkButton', 
'SP_DialogCancelButton', 
'SP_DialogHelpButton', 
'SP_DialogOpenButton', 
'SP_DialogSaveButton', 
'SP_DialogCloseButton', 
'SP_DialogApplyButton', 
'SP_DialogResetButton', 
'SP_DialogDiscardButton', 
'SP_DialogYesButton', 
'SP_DialogNoButton', 
'SP_ArrowUp',  
'SP_ArrowDown', 
'SP_ArrowLeft', 
'SP_ArrowRight', 
'SP_ArrowBack', 
'SP_ArrowForward', 
'SP_CommandLink', 
'SP_VistaShield', 
'SP_BrowserReload', 
'SP_BrowserStop', 
'SP_MediaPlay', 
'SP_MediaStop', 
'SP_MediaPause', 
'SP_MediaSkipForward',
'SP_MediaSkipBackward', 
'SP_MediaSeekForward',
'SP_MediaSeekBackward', 
'SP_MediaVolume', 
'SP_MediaVolumeMuted', 
'SP_CustomBase'
            ]
        Col_size = 6
         
        layout = QGridLayout()
 
        count = 0
        for i in icons:
            select_button = QPushButton(i)
            select_button.setIcon(self.style().standardIcon(getattr(QStyle, i)))
 
            layout.addWidget(select_button, count / Col_size, count % Col_size)
            count += 1
             
        self.setLayout(layout)
         
if __name__ == '__main__':
    app = QApplication(sys.argv)
             
    dialog = Widget()
    dialog.show()
             
    app.exec_()

Thursday, November 8, 2018

Python Qt5 : QFileDialog and QTextEdit example.

This tutorial is about QFileDialog and how to use it.
First you need to create a default PyQ5 application and add your method is called by my_OpenDialog.
This will be connected to one button.
My application uses the QTextEdit to show HTML and text files.
If you try to see another file this will open badly.
I set the completeSuffix just for HTML and text.
As you know this returns the complete suffix (extension) of the file.
The content of this file is put into QTextEdit widget create and named editor_text.
This is result of using QFileDialog with python:

This is the source code with the QFileDialog example:
import sys, os
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle('Text document')
        self.editor_text = QtWidgets.QTextEdit(self)

        self.button_OpenDialog = QtWidgets.QPushButton('Open', self)
        self.button_OpenDialog.clicked.connect(self.my_OpenDialog)

        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.editor_text, 0, 0, 1, 1)
        layout.addWidget(self.button_OpenDialog, 1, 0)
        #self.handleTextChanged()

    def my_OpenDialog(self):
        path = QtWidgets.QFileDialog.getOpenFileName(
            self, 'Open file', '',
            'HTML files (*.html);;Text files (*.txt)')[0]
        if path:
            file = QtCore.QFile(path)
            if file.open(QtCore.QIODevice.ReadOnly):
                stream = QtCore.QTextStream(file)
                text = stream.readAll()
                info = QtCore.QFileInfo(path)
                if info.completeSuffix() == 'html':
                    self.editor_text.setHtml(text)
                else:
                    self.editor_text.setPlainText(text)
                file.close()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())