0

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

5
  • 2
    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. Commented Jul 2 at 18:36
  • Hi, thanks for the suggestion. I just tried that without the / before Components/Sections but I still got the same result.
    – Isaac M
    Commented Jul 2 at 19:27
  • 1
    As already said, you must ensure that the absolute path is correctly constructed relative to the working dir. Removing the / alone won't certainly be enough since: 1. it doesn't contain the correct path (it's missing 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. Commented Jul 2 at 20:04
  • You are correct, the 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.
    – Isaac M
    Commented Jul 2 at 20:18
  • first check Current Working Directory - 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 path os.path.join(BASE, 'Components/Sections')
    – furas
    Commented Jul 2 at 23:08

1 Answer 1

0

After banging my head against the wall for a few more hours I figured it out:

The big issue was with how I was importing my modules into the main.qml. It wants you to add the folders/subfolders in the import like so :

Previously:

import StyledElements 1.0
import Sections 1.0

Fixed:

import src.Components.StyledElements 1.0
import src.Components.Sections 1.0

After that is was as easy as re-running my qrc conversion to a python script with :

pyrcc5 resources.qrc -o resources_rc.py

Found info here: https://doc.qt.io/qt-6/qtqml-modules-identifiedmodules.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.