Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. simplest mvc pattern in pyside6
QtWS25 Last Chance

simplest mvc pattern in pyside6

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 311 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiabi
    wrote on 21 Mar 2025, 18:36 last edited by abiabi
    #1

    iam looking for simple skelton of mvc pattern implemented in pyside6.

    i know a bit about mvc i found simple code with help of AI but the problem is all of the mvc patterns uses a centralized approach,

    iam looking for a decentralized approach like attaching controller with specific view.so every widget can have its own controller.so i dont have to write every sub widgets code in main.py file.

    is it actually possible.below is code generated AI

    from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
    from PySide6.QtCore import QObject, Signal
    import sys
    
    # Model: Stores count and emits updates
    class CounterModel(QObject):
        updated = Signal(int)
    
        def __init__(self):
            super().__init__()
            self.count = 0
    
        def update(self, value):
            self.count += value
            self.updated.emit(self.count)
    
    # View: UI elements
    class CounterView(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("Simple MVC - PySide6")
            layout = QVBoxLayout(self)
    
            self.label = QLabel("Count: 0")
            self.inc_btn = QPushButton("Increment")
            self.dec_btn = QPushButton("Decrement")
    
            layout.addWidget(self.label)
            layout.addWidget(self.inc_btn)
            layout.addWidget(self.dec_btn)
    
    # Controller: Connects model and view
    class CounterController:
        def __init__(self, model, view):
            self.model = model
            self.view = view
    
            self.view.inc_btn.clicked.connect(lambda: self.model.update(1))
            self.view.dec_btn.clicked.connect(lambda: self.model.update(-1))
            self.model.updated.connect(lambda value: self.view.label.setText(f"Count: {value}"))
    
    # Main Application
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        model = CounterModel()
        view = CounterView()
        controller = CounterController(model, view)
    
        view.show()
        sys.exit(app.exec())
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Mar 2025, 19:46 last edited by
      #2

      Hi,

      Did you already took a look at Qt's model view implementation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CristianMaureira
        wrote on 1 Apr 2025, 13:30 last edited by
        #3

        In case you need some guidance, here is an auto-translate (C++ -> Python) for the snippet within that page https://doc.qt.io/qtforpython-6/overviews/qtwidgets-model-view-programming.html (some of the code might not be 100% working but it might be hopefully a starting point)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved