Skip to main content

Makes PySide a little smarter.

Project description

SmartSide is one of many available ways to have PySide signals connected in a easy way.

Example of what it can do for you

Suppose you have designed a GUI using Qt Designer

Compile it with:

$ pyside-uic.exe myform.ui -o myform_ui.py

# if you have resources included compile them too
$ pyside-rcc.exe myresources.qrc -o myresources_rc.py

Then use a code like this to show your form and bind some signals:

import sys
from myform_ui import *   # this will also include `QtCore` and `QtGui`
import smartside.signal as smartsignal

class MyApplication(QtGui.QMainWindow, Ui_MainWindow, smartsignal.SmartSignal):
    def __init__(self, parent=None):
        super(MyApplication, self).__init__(parent)
        self.setupUi(self)

        # create any local UI object here, so they signal are
        # going to be auto-connected too

        self.auto_connect()

    # will respond to stateChanged signal from checkBox widget
    # notice the double underline between widget name and signal name
    def _on_checkBox__stateChanged(self):
        print 'check', self.sender().isChecked()

    # will respond to `pressed` signal of btn_add widget
    def _on_btn_add__pressed(self):
        print 'btn_add was pressed'

    # list some widgets and use regex `regex`, to select multiples.
    # starting with underline is mandatory
    _myfuncs = 'btn_base, btn_format, `btn_.+log.+`, btn_sqr'
    # will respond to clicked signal of all widget listed above
    def _when_myfuncs__clicked(self):
        print 'multiples', self.sender()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApplication()
    window.show()

    # uncomment line below to print a list of ALL signals available on your form
    # window.print_all_signals()

    sys.exit(app.exec_())

Your form is supposed to be called Ui_MainWindow in this example.

First we use setupUi as usual to create the interface.

Then auto_connect will connect member functions to signals when they match.

The last case use a multiple connection, so more then one widgets will call the same callback function. You can also use regex to select related widgets. In the example above we have selected a few widgets by its explicit name and also all widget whose name starts with 'btn\_' and have 'log' in some part of its name. All of them are going to be connected to _when_myfuncs__clicked.

Yes, it works with actions too. Like def _on_actionTest__triggered(self):. This is usefull when you create context menu by code. Just remember to call auto_connect after menu creation.

Show icon on Windows taskbar

Usually Windows 7+ executes Python scripts as a group and put every icon you define to your GUI as a child of Python’s taskbar icon, since python actually hosts your code. This happens even if you give .pyw as extension for your python script.

To solve this you have to tell Windows your script is an application by calling smartside.setAsApplication() and pass to this function an unique identifier for your script, like: ‘company.product.version’.

# ....

if __name__ == "__main__":

    from smartside import setAsApplication
    setAsApplication('example_co.exampleProd.'+__version__)

    app = QtGui.QApplication(sys.argv)
    window = MyApplication()
    window.show()
    sys.exit(app.exec_())

Console Widget

Using Qt Designer promote a QPlainTextEdit to ConsoleWidget, and use smartside.console as header (source).

Then, inside __init__ of this form use: self.name_of_widget.setLocals({'name': object, 'me': self}).

This will make the promoted QPlainTextEdit to become a python console with access to two objects: name and me.

Language

The function getBestTranslation is used to discover the best translation file available for an app.

It will look in a folder for a .qm file in the following order:

  • en-US.qm

  • en_US.qm

  • en.qm

You can specify a list of desired languages or let the function to check the system languages. If no translation is found the native language will be used.

# ....

if __name__ == "__main__":

    from smartside import getBestTranslation

    # this will look for translations inside folder ./i18n
    # it will search by system languages
    translator = getBestTranslation('i18n')

    app = QtGui.QApplication(sys.argv)
    app.installTranslator(translator)

    window = MyApp()
    window.show()
    sys.exit(app.exec_())
# if you want to specify the languages
translator = getBestTranslation('language', ['pt-BR', 'es'])

In this case it will try:

  • language/pt-BR.qm

  • language/pt_BR.qm

  • language/pt.qm

  • language/es.qm

Change History

0.1.7:

Added language locator.

0.1.6:

Fixed setup typo.

0.1.5:

Added support to python 3.

0.1.4:

Added ConsoleWidget class.

0.1.3:

Added setAsApplication.

0.1.2:

Added QAction support; For every QAction created before calling auto_connect() you can use def _on_action_name__clicked(self): like you do with signals.

0.1.1:

Small fix.


Development:

https://github.com/xgvargas/smartside - please use this space if you found a problem or think any other task on PySide can be simplified.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

smartside-0.2.0.zip (17.1 kB view details)

Uploaded Source

smartside-0.2.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

smartside-0.2.0-py2.7.egg (11.0 kB view details)

Uploaded Source

File details

Details for the file smartside-0.2.0.zip.

File metadata

  • Download URL: smartside-0.2.0.zip
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for smartside-0.2.0.zip
Algorithm Hash digest
SHA256 574858affda8447c82dfafb67799a5a7aa6502183312a98646bfb68356393812
MD5 a322f053020048d449da281a4ebe7e3b
BLAKE2b-256 4936afd1b9ae6f88cc10758edd5f47d1c3d74f82d123f0cb381fef692d7662eb

See more details on using hashes here.

File details

Details for the file smartside-0.2.0.tar.gz.

File metadata

  • Download URL: smartside-0.2.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for smartside-0.2.0.tar.gz
Algorithm Hash digest
SHA256 947dad408823bbba9e6b36dc9cdff81f21d804610d334e809fcfc42f51ac35ae
MD5 d8eff48cf6b807abf524101707e88108
BLAKE2b-256 c6610818edddd77723fb3d0d3c78cc26bc4cd168480a125adcffc58a9e20b5a1

See more details on using hashes here.

File details

Details for the file smartside-0.2.0-py2.7.egg.

File metadata

File hashes

Hashes for smartside-0.2.0-py2.7.egg
Algorithm Hash digest
SHA256 ff8537165f585722c3fc0677c3e2777167fd10103062b1780ad1bc8a8b7fa36e
MD5 00bb12422c40b145c7908b86b40d5ce3
BLAKE2b-256 6df2e5c47e47029767ffe2c16de5206ed0f59385cf168cb19df1dc671a177318

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page