Skip to main content

MVVM library for Qt with observable properties and data bindings.

Project description

QtMVVM

MVVM library for Qt with observable properties and data bindings.

Languages: 🇬🇧 English | 🇷🇺 Русский

Installation

uv add qtmvvm

Quick Start

1. Create Observable Properties

from qtmvvm import ObservableProperty

# Create a property with initial value
name = ObservableProperty("John")
age = ObservableProperty(25)

# Get value
print(name.value)  # "John"
print(name())      # "John"

# Set value
name.value = "Jane"
age(30)            # Alternative syntax

2. Create ViewModel

from qtmvvm import BaseViewModel, computed_property

class PersonViewModel(BaseViewModel):
    # Simple properties
    name: str = "John"
    age: int = 25

    # Computed property (read-only, auto-updates)
    @computed_property(depends_on=["name", "age"])
    def description(self):
        return f"{self.name}, {self.age} years old"

# Usage
vm = PersonViewModel()
print(vm.name)         # "John"
print(vm.description)  # "John, 25 years old"

vm.name = "Jane"
print(vm.description)  # "Jane, 25 years old" (auto-updated!)

3. Use Binding Operators

QtMVVM provides three operators for data binding:

Operator Direction Description
>> Property → Widget 1-way binding
@ Property ↔ Widget 2-way binding
<< Signal → Property Signal binding

1-Way Binding (>>)

from qtmvvm import ObservableProperty
from qtpy.QtWidgets import QLabel

name = ObservableProperty("John")
label = QLabel()

name >> label  # When name changes, label updates automatically
name.value = "Jane"  # label.text = "Jane"

2-Way Binding (@)

from qtmvvm import ObservableProperty
from qtpy.QtWidgets import QLineEdit

name = ObservableProperty("John")
edit = QLineEdit()

name @ edit  # Sync in both directions

edit.setText("Jane")  # name.value = "Jane"
name.value = "Bob"    # edit.text = "Bob"

Signal Binding (<<)

from qtmvvm import ObservableProperty
from qtpy.QtWidgets import QPushButton

click_count = ObservableProperty(0)
button = QPushButton("Click me")

click_count << button.clicked  # Each click increments click_count

4. Using Command

Command wraps ViewModel methods for binding to buttons with automatic disabling during execution.

from qtmvvm import BaseViewModel, command
from qtpy.QtWidgets import QPushButton

class CounterViewModel(BaseViewModel):
    count: int = 0

    @command
    def increment(self):
        self.count += 1

    @command
    async def load_data(self):
        import asyncio
        await asyncio.sleep(1)  # Async operation

vm = CounterViewModel()

# Bind to buttons
inc_btn = QPushButton("+")
load_btn = QPushButton("Load")

vm.increment << inc_btn    # Click → increment()
vm.load_data << load_btn   # Button disabled during loading

Documentation

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

qtmvvm-1.0.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file qtmvvm-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: qtmvvm-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for qtmvvm-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b02bbe46448a3ddfca4ec22111f7b2ec003c0ce297b31a4fc22c4f23518d864
MD5 71cd7f0ba8c6fe695d0898da481bc1db
BLAKE2b-256 03d2dba64e436689b96ed14a791db7c80d972664064fd9c38076ed42cb0a0d13

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