[PyQt] Qt5 error messages

Florian Bruhin me at the-compiler.org
Fri Sep 11 07:15:02 BST 2015


* Holger Jahn <lists at loomsday.co.nz> [2015-09-11 17:44:14 +1200]:
> Hi,
> 
> Is there a way to determine the detailed error reason of a failed Qt5 method
> call in PyQt5?

The same way as in Qt - which unfortunately isn't the way you're used
to from Python, which typically uses exceptions.

> Here is what I am doing (Qt 5.5 and Python 3.4 on latest Arch Linux):
> 
> image = QImage()
> image.load( '/does_absolutely_not_exist' )
> 
> Now, I would like somehow retrieve a "File not found!" error message within
> my script by some means. Instead, this is happening:

QImage doesn't seem to have an error()/errorString() method like other
classes typically have.

What you could do is to use the overload using a QIODevice instead:

    >>> from PyQt5.QtCore import QFile, QIODevice
    >>> f = QFile('/does_absolutely_not_exist')
    >>> ok = f.open(QIODevice.ReadOnly)
    >>> ok
    False
    >>> f.error()
    5
    >>> f.errorString()
    'No such file or directory'

And then later use QImage(f) (if the file was valid).

> 3) try:/except: does not work, "except:" is not triggered.

Sidenote: Except for testing, you never should use a bare 'except:' -
it's better to just let the exception propagate if you don't know how
to handle it. Using a bare "except:" will even hide things like
KeyboardInterrupt, etc.

(Python 3 really should've killed bare "except:" and require an
explicit "except BaseException:" instead... but I digress.)

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150911/ec2a28a4/attachment-0001.sig>


More information about the PyQt mailing list