Giter Site home page Giter Site logo

qt5.py's Introduction

Qt5.py enables you to write software that runs the same on both PySide2 and PyQt5.

See also

  • Qt.py - Like Qt5.py, with added support for PySide and PyQt4.

Goals

Write once, run in any binding.

Qt5.py is the younger brother to Qt.py and provides extended support for features unique to Qt 5, such as QtQml, QtMultimedia and QtWebAssembly.

Goal Description
Support co-existence Qt5.py should not affect other bindings, not even Qt.py, running in the same interpreter session
Build for one, run with all Code written with Qt5.py should run identically on both PySide2 and PyQt5
Explicit is better than implicit Differences between bindings should be visible to you.



Install

Qt5.py is a single file and can be either copy/pasted into your project, downloaded as-is, cloned as-is or installed via pip.

# From PyPI
$ pip install Qt5.py
  • Pro tip: The direct download and clone options refer to the latest commit of this project, which is typically beta. For the latest stable release, refer to any of the official releases
  • Pro top: Qt5.py supports vendoring



Usage

Use Qt5.py as you would use PySide2.

import os
import sys
import tempfile

from Qt5 import QtGui, QtQml

f = tempfile.NamedTemporaryFile("w", delete=False)
f.write("""\
import QtQuick 2.4
import QtQuick.Controls 1.4

ApplicationWindow {
    title: "My App"
    width: 240
    height: 180
    visible: true

    Button {
        text: "Hello World"
        anchors.centerIn: parent
    }
}
""")
f.close()

app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
engine.load(f.name)
engine.quit.connect(app.quit)
app.exec_()
os.remove(f.name)

qt5.py's People

Contributors

mottosso avatar valblfld avatar

Stargazers

Silvio Traversaro avatar Nguyen Phi Hung avatar Lee Dunham avatar Tomás Poveda avatar Alex Hughes avatar Carlos Rico Adega avatar John Docter avatar Sebastian Elsner avatar Brendan Abel avatar Alexey Denisenko avatar Toke Jepsen avatar Michael Malinowski avatar  avatar 了空 avatar Marcelo F. Bortolini avatar michael avatar Joe Yu avatar Deke Kincaid avatar John avatar Jer avatar Frank Rousseau avatar  avatar Tim Lehr avatar Hal avatar Miquel Campos avatar tm8r avatar  avatar Mel Massadian avatar David Lai avatar Justin Tennant avatar  avatar Desmond avatar Fredrik Averpil avatar

Watchers

Deke Kincaid avatar Fredrik Averpil avatar Silvio Traversaro avatar  avatar David Lai avatar  avatar Hal avatar ZMG avatar  avatar

qt5.py's Issues

Qt5 import issue compared with Qt, PyQt or Pyside

the old Qt module can easily subsitute Pyside2
but when trying that with the new Qt5 it has to be done in a specific way.

it doesn't allow us to do this

from PySide2.QtWidgets import QApplication
from Qt.QtWidgets import QApplication
from Qt5.QtWidgets import QApplication  # errors
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ModuleNotFoundError: No module named 'Qt5.QtWidgets'; 'Qt5' is not a package

This is fine

import PySide2
import Qt
import Qt5

This is fine

from PySide2 import QtWidgets
from Qt import QtWidgets
from Qt5 import QtWidgets

QtCore.QVariant for PySide

QtCore.QVariant was missing in PySide, but the document mentioned that one could just use "QVariant" like:

signal = QtCore.Signal("QVariant")

This works for Python built-in types, but looks like not all custom types will be handled properly.

import sys
from PySide2 import QtWidgets, QtCore

QtCore.QVariant = "QVariant"


class Sheet(list):
    pass


class Thing(object):
    pass


class Application(QtWidgets.QApplication):

    variant = QtCore.Signal(QtCore.QVariant)

    def __init__(self):
        super(Application, self).__init__(sys.argv)

        window = QtWidgets.QWidget()
        button = QtWidgets.QPushButton("Emit")

        layout = QtWidgets.QVBoxLayout(window)
        layout.addWidget(button)

        button.clicked.connect(self.emit_variant)
        self.variant.connect(self.process)
        self.window = window

        window.show()

    def emit_variant(self):
        self.variant.emit(Sheet(["a", "b"]))
        self.variant.emit(Thing())

    def process(self, variant):
        print(type(variant), variant)


if __name__ == "__main__":
    app = Application()
    app.exec_()

Hit the Emit button and the result will be:

<class 'list'> ['a', 'b']
<class '__main__.Thing'> <__main__.Thing object at 0x000002204CEA1240>

Notice that although the instance of Thing stays the same, but the instance of Sheet has became just a list.

But if you change "QVariant" to object

QtCore.QVariant = object

Then:

<class '__main__.Sheet'> ['a', 'b']
<class '__main__.Thing'> <__main__.Thing object at 0x000001E0A0DC1240>

They both get preserved.

So maybe we should use object instead of "QVariant" to fill up this gap ?
And, anyone knows why it works in this way in PySide ?

pyqtSignal and friends available on PyQt5

A current limitation of the way Qt5.py has been implemented is that, even though QtCore.Signal is consistent across both PySide2 and PyQt5, it doesn't successfully prohibit the use of pyqtSignal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.