QIODevice cannot open .ui file from resources.
-
I'm trying to use the FormBuilder class to load ui files from project resources I get the error:
QIODevice::read (QFile, ":\_qtresources\ui\NewDocumentForm.ui"): device not open Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
Here is what is loading it:
class NewDocumentForm(QDialog): accepted = Signal() def __init__(self): super().__init__() form = QFile(":/_qtresources/ui/NewDocumentForm.ui") form.open(QFile.ReadOnly) builder = QFormBuilder() builder.load(form, self)
I can only guess that it's somehow not finding the ui file since the qiodevice turns up effectively empty.
-
@zspinelli
For the error message, that comes from trying to load from a non-open file.form.open(QFile.ReadOnly)
returns a boolean to indicate success/failure, and I would guess its returningFalse
.QIODevice.errorString()
might provide more information. -
Hi,
@zspinelli said in QIODevice cannot open .ui file from resources.:
@JonB
I just found something now for a tool called rcc. Is that absolutely always neccessary to use qrc stuff?
If so, I think this might be the problem.Yes that is mandatory. Resources must be first created like in C++. See here.
-
@zspinelli said in QIODevice cannot open .ui file from resources.:
That's the exact path to it from the top of the source directory in the file system.
That's not what you want at runtime. The
:/...
file path syntax is treated by Qt as referring to a resource compiled into your executable, not a real, external file. Follow @SGaist's link to achieve that. -
@SGaist
The link you posted shows an example using a tool called "pyside6-rcc".
If that's supposed to be installed with PySide6 then for some reason I don't have it. I found this page https://stackoverflow.com/questions/22479581/cannot-use-pyside-rcc-to-compile-qrc-file mentioning some locations where it might be but it isn't present in any of them.However I found another page: https://doc.qt.io/qtforpython/overviews/resources.html showing the use of the regular "rcc" which I do have as part of the Qt installation. Before I try the example shown there, should I know if one is one tool is more preferable than the other?
-
If the one you have supports Python code generation, then use it.
-
I found pyside6-rcc. I'm going to note for posterity that it was found under C:\Users\<username>\AppData\Roaming\Python\Python39\Scripts
I ran it, got the py output as per the demonstration, and imported it. The errorString for the QFile is now "Unknown error".
-
@JonB @SGaist
After lot of internet searching and fistfighting the code, I got my project back on track and got some other stuff fixed as well.
I had pyside6 module installed wrong and was using the wrong approach for integrating the UI form.
@SGaist Your recommendation for pyside6-rss led me to pyside6-uic, which was what I really needed.I'm thankful to you both for tolerating and assisting me.