Customizable Task Manager with MVC Architecture
Customizable Task Manager with Model-View-Controller (MVC) Architecture
This example demonstrates a task manager application with a customizable, modern UI, utilizing the Model-View-Controller (MVC) architecture with Qt 6.
Overview
The task manager application has the following features:
- Modular Design: Separation of Concerns using the MVC pattern
- Customizable UI: Users can switch between different themes and layout orientations
- Task List Management: Users can add, remove, and edit tasks
- Due Date and Priority: Users can set due dates and priorities for tasks
Code Structure
task_manager/
main.py
models/
__init__.py
task.py
controllers/
__init__.py
task_controller.py
views/
__init__.py
task_view.py
task_widget.py
main_window.py
themes/
dark/
light/
__init__.py
resources.qrc
Source Code
models/task.py
from PyQt6.QtCore import pyqtProperty, pyqtSignal, QObject
class Task(QObject):
nameChanged = pyqtSignal(str)
dueDateChanged = pyqtSignal(str)
priorityChanged = pyqtSignal(str)
doneChanged = pyqtSignal(bool)
def __init__(self, name, due_date, priority, done=False):
super().__init__()
self._name = name
self._due_date = due_date
self._priority = priority
self._done = done
@pyqtProperty(str)
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
self.nameChanged.emit(value)
@pyqtProperty(str)
def dueDate(self):
return self._due_date
@dueDate.setter
def dueDate(self, value):
self._due_date = value
self.dueDateChanged.emit(value)
@pyqtProperty(str)
def priority(self):
return self._priority
@priority.setter
def priority(self, value):
self._priority = value
self.priorityChanged.emit(value)
@pyqtProperty(bool)
def done(self):
return self._done
@done.setter
def done(self, value):
self._done = value
self.doneChanged.emit(value)
controllers/task_controller.py
from PyQt6.QtCore import QObject
from .task import Task
class TaskController(QObject):
def __init__(self):
super().__init__()
self.tasks = []
def addTask(self, name, due_date, priority):
task = Task(name, due_date, priority)
self.tasks.append(task)
def removeTask(self, task):
self.tasks.remove(task)
def getTask(self, index):
return self.tasks[index]
views/task_view.py
from PyQt6.QtWidgets import QListView, QAbstractItemView
from PyQt6.QtCore import QAbstractListModel, Qt
from .task import Task
class TaskListModel(QAbstractListModel):
def __init__(self, tasks):
super().__init__()
self.tasks = tasks
def rowCount(self, parent=None):
return len(self.tasks)
def data(self, index, role):
if role == Qt.ItemDataRole.DisplayRole:
return self.tasks[index.row()].name
class TaskView(QListView):
def __init__(self, tasks):
super().__init__()
self.tasks = tasks
self.model = TaskListModel(self.tasks)
self.setModel(self.model)
self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
main.py
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from .views import TaskView
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.tasks = []
self.controller = TaskController()
self.view = TaskView(self.tasks)
layout = QVBoxLayout()
layout.addWidget(self.view)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
Productivity Hacks and Best Practices
- MVC Architecture: Separate concerns using the MVC pattern for better maintainability and scalability.
- Modular Code Structure: Organize code into logical modules for easier management.
- Customizable UI: Allow users to switch between different themes and layout orientations for improved user experience.
Conclusion
This task manager application demonstrates a customizable, modern UI utilizing the MVC architecture with Qt 6. By following productivity hacks and best practices, developers can create efficient and scalable applications.
Comments