I am developing a database software using PyQt6 and SQlite3. I am struggling a bit in organising the files and classes since this is my first project of such. I have kept the main.py in the root to open the window, and I have various stacked widgets like home_page, dashboard_page etc. Every such page has various QWidgets (pushbutton, combobox etc). In order to seperate the task inside the controller folder I have files for each stacked page that defines tasks for each widget in the respective pages. So I have the following directory:
root_directory/
│
├── main.py
│
├── ui/
│ ├── ui_main.py
│ └── ui_functions.py
│
└── controller/
├── home_page_controller.py
└── (other_controller_files.py)
In the main file I have created a MainWindow class which is an instance of QMainWindow, and initialized the UI components:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# SET AS GLOBAL WIDGETS
# ///////////////////////////////////////////////////////////////
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
global widgets
widgets = self.ui
Then I tried creating HomePageController class in homepage_controller.py. Here is the code:
from PyQt6.QtWidgets import QFileDialog
class HomePageController:
def __init__(self, Ui_MainWindow):
self.ui = Ui_MainWindow
# Connect buttons to functions
self.ui.open_db_btn_homepage.clicked.connect(self.open_db)
self.ui.create_new_db_btn_homepage.clicked.connect(self.create_new_db)
def open_db(self):
file_name, _ = QFileDialog.getOpenFileName(self.ui, "Open database file", "", "All Files (*)")
if file_name:
print(file_name)
But this way I am unable to access any widgets from HomePageCotroller class. As a result I ma getting an error from QFileDialog. Error:
file_name, _ = QFileDialog.getOpenFileName(self.ui, "Open database file", "", "All Files (*)")
TypeError: getOpenFileName(parent: typing.Optional[QWidget] = None, caption: str = '', directory: str = '', filter: str = '',
To resolve I tried the folloing line in MainWindow class:
# Instantiate HomePageController with self as the parent widget
self.home_page_controller = HomePageController(self.ui)