Skip to main content

A general-purpose MVVM framework based on PySide6 property system

Project description

MVVM Framework for PySide6

一个基于 PySide6 属性系统实现的通用 QtWidgets MVVM 框架。

项目结构

mvvm_framework/
├── __init__.py              # 包入口,导出核心组件
├── core/                    # 核心模块
│   ├── __init__.py
│   ├── observable.py        # 可观察对象和列表
│   ├── model.py             # 模型基类
│   ├── viewmodel.py         # 视图模型基类
│   ├── command.py           # 命令模式实现
│   └── binding.py           # 数据绑定工具

核心组件

1. ObservableObject(可观察对象)

所有 MVVM 组件的基类,提供属性变更通知机制。

from mvvm_framework import ObservableObject

class MyObject(ObservableObject):
    def __init__(self):
        super().__init__()
        self._value = 0
    
    @property
    def value(self) -> int:
        return self._value
    
    @value.setter
    def value(self, val: int):
        if self._value != val:
            self._value = val
            self.notify_property_changed("value")

2. Model(模型)

数据模型层,包含业务逻辑和数据验证。

from mvvm_framework import Model

class Person(Model):
    def __init__(self, name: str = "", age: int = 0):
        super().__init__()
        self._name = name
        self._age = age
    
    @property
    def name(self) -> str:
        return self._name
    
    @name.setter
    def name(self, value: str):
        if self._name != value:
            self._name = value
            self.notify_property_changed("name")
    
    def validate_name(self, value: str) -> str | None:
        if not value.strip():
            return "Name cannot be empty"
        return None

3. ViewModel(视图模型)

连接 Model 和 View 的桥梁,暴露数据和命令给视图。

from mvvm_framework import ViewModel, Command

class PersonViewModel(ViewModel[Person]):
    def __init__(self, model: Person):
        super().__init__(model)
        self._save_command = Command(self.save, self.can_save)
    
    @property
    def name(self) -> str:
        return self.model.name
    
    @name.setter
    def name(self, value: str):
        if self.model.name != value:
            self.model.name = value
            self.notify_property_changed("name")
    
    @property
    def save_command(self) -> Command:
        return self._save_command
    
    def can_save(self) -> bool:
        return self.model.name != ""
    
    def save(self):
        print(f"Saving {self.model.name}")

4. Command(命令)

实现命令模式,用于处理用户操作。

from mvvm_framework import Command

# 基本命令
save_cmd = Command(
    execute=lambda: print("Saved!"),
    can_execute=lambda: True
)

# 带参数的命令
from mvvm_framework.core.command import ParameterizedCommand
delete_cmd = ParameterizedCommand(
    execute=lambda item: print(f"Deleting {item}"),
    can_execute=lambda item: item is not None
)

5. ObservableList(可观察列表)

支持变更通知的列表类型。

from mvvm_framework import ObservableList

numbers = ObservableList([1, 2, 3])
numbers.itemAdded.connect(lambda idx, val: print(f"Added {val} at {idx}"))
numbers.append(4)  # 触发 itemAdded 信号

6. Binding(数据绑定)

简化 View 和 ViewModel 之间的绑定。

from mvvm_framework import Binding

# 双向文本绑定
Binding.bind_text(viewmodel, "name", line_edit)

# 单向标签绑定
Binding.bind_label(viewmodel, "display_name", label)

# 命令绑定
Binding.bind_command(viewmodel, "save_command", button)

# 值绑定(SpinBox/Slider)
Binding.bind_value(viewmodel, "age", spinbox)

# 选中状态绑定
Binding.bind_checked(viewmodel, "is_active", checkbox)

完整示例

运行示例应用:

python -m mvvm_framework.examples.person_view

或者在代码中使用:

import sys
from PySide6.QtWidgets import QApplication
from mvvm_framework.examples import Person, PersonViewModel, PersonView

app = QApplication(sys.argv)

# 创建 Model
person = Person(name="John", age=30, email="john@example.com")

# 创建 ViewModel
viewmodel = PersonViewModel(person)

# 创建 View
view = PersonView(viewmodel)
view.show()

sys.exit(app.exec())

特性

  • 属性变更通知: 自动通知 UI 更新
  • 双向数据绑定: View 和 ViewModel 自动同步
  • 命令模式: 统一的动作处理方式
  • 数据验证: 内置验证系统
  • 计算属性缓存: 优化性能
  • 集合变更通知: ObservableList 支持
  • 类型提示: 完整的类型注解支持

安装依赖

pip install PySide6

许可证

MIT License

Project details


Download files

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

Source Distribution

mvvm_framework-0.1.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mvvm_framework-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file mvvm_framework-0.1.0.tar.gz.

File metadata

  • Download URL: mvvm_framework-0.1.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for mvvm_framework-0.1.0.tar.gz
Algorithm Hash digest
SHA256 db777e7a47f9dba4cc24fdadd6c4c2a85ba05d7dc3d7f6003cb50603132a1e8f
MD5 c0a21a5af26b5f4b9c7937ac0813f274
BLAKE2b-256 f32242ebf1d015b19f31c667fe8eb697d566989caa991e14ff1c03909a21a878

See more details on using hashes here.

File details

Details for the file mvvm_framework-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mvvm_framework-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d1a475f158460e6b86e097be356a715ded051958cbd999deb7090819896832a
MD5 124fedbe8e125a028981d58b1ae63ab3
BLAKE2b-256 6ce69e8417d3ca6eb7c3dca1bb8dafd7672eee813f594964cff24de111840b85

See more details on using hashes here.

Supported by

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