[PyQt] QFile "Unknown error" when opening file from resource
Elvis Stansvik
elvstone at gmail.com
Mon Feb 15 13:36:03 GMT 2016
Hi all,
I'm trying to load a file from a Qt resource (.qrc).
These are the files contained in the .qrc:
(guipy2)estan at newton:~/orexplore/dev/orexplore.gui$ find resources
resources
resources/rock_types
resources/rock_types/all.txt
resources/rock_types/igneous.txt
resources/rock_types/sedimentary.txt
resources/rock_types/metamorphic.txt
resources/rock_types.qrc
As can be seen by looking at it:
(guipy2)estan at newton:~/orexplore/dev/orexplore.gui$ cat resources/rock_types.qrc
<RCC>
<qresource prefix="/">
<file>rock_types/all.txt</file>
<file>rock_types/igneous.txt</file>
<file>rock_types/metamorphic.txt</file>
<file>rock_types/sedimentary.txt</file>
</qresource>
</RCC>
He's how I generate rock_types_rc.py from it:
(guipy2)estan at newton:~/orexplore/dev/orexplore.gui$ pyrcc5
resources/rock_types.qrc -o orexplore/gui/resources/rock_types_rc.py
(guipy2)estan at newton:~/orexplore/dev/orexplore.gui$
And this is how I import and try to use one of the embedded files:
(guipy2)estan at newton:~/orexplore/dev/orexplore.gui$ cat
orexplore/gui/corelog/view/rock_type.py
from PyQt5.QtCore import QFile, QIODevice
from PyQt5.QtWidgets import QStyledItemDelegate, QLineEdit, QCompleter
from orexplore.gui.resources import rock_types_rc
class RockTypeDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(RockTypeDelegate, self).__init__(parent)
rockTypes = []
rockTypesFile = QFile(':/rock_types/all.txt')
rockTypesFile.open(QIODevice.ReadOnly)
print(rockTypesFile.errorString())
while not rockTypesFile.atEnd():
rockTypes.append(str(rockTypesFile.readLine()))
self._completer = QCompleter(rockTypes)
def createEditor(self, parent, option, index):
edit = QLineEdit(parent)
edit.setCompleter(self._completer)
return edit
Note how I'm printing the QFile errorString() right after opening the
file. The result when running the app is:
(guipy2)estan at newton:~/orexplore/dev/orexplore.gui$ python -m orexplore.gui.main
Unknown error
Anyone know what I'm doing wrong here?
Cheers,
Elvis
More information about the PyQt
mailing list