I am trying to build a UI using PyQt5 and python. I built a base using main.qml and some styled qml files in the same directory and it imported fine, but I decided to break everything up into folders so it is more organized but now none of my imports are working no matter what I do.
The new file structure is:
Interface/
│ ├── main.py
│ ├── resources.qrc
│ ├── resources_rc.py
│ │
│ ├── src/
│ │ ├── main.qml
│ │ │
│ │ ├── Components/ # Directory for custom QML components
│ │ │ ├── Sections/ # Directory for "Sections" module
│ │ │ │ ├── Status.qml
├── Title.qml
│ │ │ │ └── qmldir # qmldir file for "Sections" module
│ │ │ │
│ │ │ └── StyledElements/ # Directory for "StyledElements" module
│ │ │ ├── StyledButton.qml
│ │ │ └── qmldir
StyledElements & Sections contain a qmldir file that looks like:
module Sections
Status 1.0 Status.qml
Title 1.0 Title.qml
My main.py calls it like this:
import os
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
def handle_warnings(warnings):
for warning in warnings:
print(f"QML Warning: {warning.toString()}")
app = QApplication([])
engine = QQmlApplicationEngine()
engine.warnings.connect(handle_warnings)
# Load QML file
qml_file_path = os.path.abspath('src/main.qml')
print(f"Loading QML file: {qml_file_path}")
engine.load(qml_file_path)
if not engine.rootObjects():
print("Failed to load QML file.")
sys.exit(-1)
else:
print("QML file loaded successfully.")
app.exec_()
sys.exit(app.exec_())
And my main.qml looks like:
import QtQuick 2.15
import QtQuick.Controls 2.15
import StyledElements 1.0
import Sections 1.0
ApplicationWindow {
title: 'Interceptor'
visible: true
width: 700
height: 600
Rectangle {
id: rectangle
width: 700
height: 725
color: 'white'
Section_Title{}
StyledButton{}
}
}
Always gets me these errors:
QML Warning: module "Sections" is not installed
QML Warning: module "StyledElements" is not installed
QML Warning: module "Sections" is not installed
QML Warning: module "StyledElements" is not installed
Things I've tried:
I have tried putting quote around my import in my main.qml like:
import "StyledElements" 1.0
import "Sections" 1.0
Ive tried setting up import paths like so:
# Set the path to the QML files
engine.addImportPath(os.path.abspath("src/Components/Sections"))
engine.addImportPath(os.path.abspath("src/Components/StyledElements"))
# Load QML file
qml_file_path = os.path.abspath('src/main.qml')
engine.load(qml_file_path)
Also tried:
engine.addImportPath(os.path.abspath("qrc:/"))
or
engine.addImportPath(os.path.abspath("qrc:/src/main.qml"))
From what I can tell the issue is not to do with loading the main.qml because when i change the path it gives me an error about it not being found and the error seems to be coming from the import in the main.qml.
I tried using a resource file that looks like:
<RCC>
<qresource prefix="/">
<file>src/main.qml</file>
<file>src/Components/Sections/NvStatus.qml</file>
<file>src/Components/Sections/qmldir</file>
<file>src/Components/StyledElements/StyledButton.qml</file>
<file>src/Components/StyledElements/qmldir</file>
</qresource>
</RCC>
I compiled resource file into python with
pyrcc5 resources.qrc -o resources_rc.py
& imported the new py file using in main.py
import resources_rc
At this point ive spent hours with Chat GPT and google trying to find an answer. Ive tried multiple things that 'work' in the sense that they load the qml in the end but cant get the custom module to load. I am not sure how to proceed. Everything I try has lead me to the same issue of the main.qml unable to load the module with the errors showed above
os.path.abspath("/Components/Sections")
seems quite wrong, since the prepending/
is already considered an absolute path. The path should be reconstructed based on the relative path of the script, using__file__
as reference./
before Components/Sections but I still got the same result.src
); 2. it may not be accurate depending on the cwd. If you don't know how the working dir affects relative paths, please do appropriate research as it's quite an important and mandatory knowledge in programming.src
should be in there. I have flipped this thing around a few times trying out different troubleshooting and I must have copied that from before I had the src folder. I apologize for the confusion & updated it. I made sure to print out the cwd to make sure my paths were correct. One thing that stands out is any changes made to addImportPath:engine.addImportPath(os.path.abspath("src/Components/Sections"))
Isnt causing any visible changes on my end. No new errors or anything no matter what I do to it.print( os.getcwd() )
- because python can run it in different folder and then relative path will search files in different folder. You have to use__file__
to get folder with your code - e.g.BASE = os.path.dirname(os.path.abppath(__file__))
and later create full pathos.path.join(BASE, 'Components/Sections')