pip install QSugar
QSugar is a PySide framework for efficiently building and reactive interfaces. It learns from the concepts of excellent frameworks mainly Vue. The development of QtQuick technology confirms that the Qt metatype system gives Qt unlimited potential for declarative UI. QSugar was conceived and boldly attempted on this basis.
If you don’t understand the concept of Vue framework, you can read the following documents
This is an example of a counter. You can understand how it differs from the traditional development model.
import sys
from PySide6.QtGui import Qt
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from QSugar import Batch, DSL, Ref, Computed, Bind, Row, Column
Batch(DSL)(QWidget, QPushButton)
Batch(Bind)(QLabel)
count = Ref(1)
countText = Computed(globals())
@countText
def getTip():
return f'The number is {count.value}.'
app = QApplication(sys.argv)
widget = QWidget(
child=Row(
children=[
QLabel(
alignment=Qt.AlignmentFlag.AlignCenter,
text=countText
),
Column(
children=[
QPushButton(
text='+1',
clicked=lambda: count << count.value + 1
),
QPushButton(
text='-1',
clicked=lambda: count << count.value - 1
)
]
)
]
)
)
widget.show()
app.exec()
QSugar enhances existing classes of QtWidgets with decorators, register with Batch(DSL)(...)
. At the same time, properties are packaged and calculated with the help of Ref
and Computed
classes. For details, see reactive programming.
QSugar uses DSL syntax and responsive objects to quickly build UI interfaces. Please note! QSugar supports more than one DSL syntax and is designed independently of reactive programming.
If you don't care about DSL syntax, reactive programming is applicable. For example:
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
from QSugar import Ref
btnText = Ref('Click Me!')
app = QApplication(sys.argv)
widget = QPushButton()
widget.setText(btnText)
widget.show()
# Modify the text content of the button
btnText << 'Modified'
app.exec()
Minimizing the intrusion of the PySide framework as much as possible is one of the principles of QSugar framework design. As its name suggests, if the traditional development model is compared to coffee, QSugar is like a sugar cube. The functions you need are added and the functions you don’t need are shelved.
Supported layout DSL syntax includes:
- __init__method and property
- Fluent API
- qtml file parsing (in experiment)
- with statement (not in practice)
The project plans to develop the dsl-cli
command line tool in the future to facilitate conversion between different DSL syntaxes and reduce the compatibility burden. For a detailed introduction, see DSL Syntax.
Currently there is only md file, see GitHub Document.
Q:Why is the old project deleted?
A:The widget enhancement idea of QSugar's old design is Mixin injection. Python has general support for Mixin features, and its behaviors that do not meet expectations often occur. For example, the DSL enhancement in old design is implemented by inheriting BaseDSLMixin. Method proxies (especially events) of subclasses of enhanced classes often fail, causing a lot of problems in development, so this project is rewritten from scratch with decorators.
Q: Why is it not compatible with PyQt?
A: QSugar currently only supports PySide. Since the binding of PyQt is very different from PySide, there are such problems:
(1) Although PyQt object property can be set in the init method, the corresponding setter method will not be triggered. Although it can be solved through reflection, since setting property/binding signals is a high-frequency operation in UI construction, the method of frequent reflection to obtain the setter will cause serious performance problems. (After testing, page jam often occur)
(2) The sip binding used by PyQt lacks shiboken support for the middle layer of Python , resulting in many features unavailable. For example, when using a decorator proxy method/class, inexplicable exceptions will occur in some methods (suspected memory release and function addressing issues).
If you think the project is valuable, please give the project a Star⭐, thank you!
If you buy a cup of hot cocoa for the sewer rat
I'll thank you very much and share the technical details with you while I'm at it.
gmail mailbox: niwik.dev@gmail.com