PySide How to see QML errors in python console?
I have the following code:
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('./QML/main.qml')
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
As you can see, if `engine.load' fails all I'll see is a '-1' exit code, without any elaboration on why it failed and what the error happened. How can I print the QML error in the python console?
There was a walkaround for this with when using QQuickView instead of QQmlApplicationEngine and is described in this post, however, I wonder if the something similar can be achieved for QQmlApplicationEngine?
python qml pyside2
add a comment |
I have the following code:
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('./QML/main.qml')
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
As you can see, if `engine.load' fails all I'll see is a '-1' exit code, without any elaboration on why it failed and what the error happened. How can I print the QML error in the python console?
There was a walkaround for this with when using QQuickView instead of QQmlApplicationEngine and is described in this post, however, I wonder if the something similar can be achieved for QQmlApplicationEngine?
python qml pyside2
relative stackoverflow.com/questions/45805076/…
– eyllanesc
Dec 31 '18 at 22:29
@eyllanesc I tried to setQQmlEngine::setOutputWarningsToStandardError(bool enabled)to true, and to connect to thewarningssignal ... No luck so far...
– Curtwagner1984
Dec 31 '18 at 22:41
Exactly, you have reviewed stackoverflow.com/questions/45805076/…: this is probably a bug, I recommend you report it.
– eyllanesc
Dec 31 '18 at 22:43
add a comment |
I have the following code:
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('./QML/main.qml')
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
As you can see, if `engine.load' fails all I'll see is a '-1' exit code, without any elaboration on why it failed and what the error happened. How can I print the QML error in the python console?
There was a walkaround for this with when using QQuickView instead of QQmlApplicationEngine and is described in this post, however, I wonder if the something similar can be achieved for QQmlApplicationEngine?
python qml pyside2
I have the following code:
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('./QML/main.qml')
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
As you can see, if `engine.load' fails all I'll see is a '-1' exit code, without any elaboration on why it failed and what the error happened. How can I print the QML error in the python console?
There was a walkaround for this with when using QQuickView instead of QQmlApplicationEngine and is described in this post, however, I wonder if the something similar can be achieved for QQmlApplicationEngine?
python qml pyside2
python qml pyside2
edited Dec 31 '18 at 22:09
eyllanesc
78k103156
78k103156
asked Dec 31 '18 at 20:49
Curtwagner1984Curtwagner1984
2361717
2361717
relative stackoverflow.com/questions/45805076/…
– eyllanesc
Dec 31 '18 at 22:29
@eyllanesc I tried to setQQmlEngine::setOutputWarningsToStandardError(bool enabled)to true, and to connect to thewarningssignal ... No luck so far...
– Curtwagner1984
Dec 31 '18 at 22:41
Exactly, you have reviewed stackoverflow.com/questions/45805076/…: this is probably a bug, I recommend you report it.
– eyllanesc
Dec 31 '18 at 22:43
add a comment |
relative stackoverflow.com/questions/45805076/…
– eyllanesc
Dec 31 '18 at 22:29
@eyllanesc I tried to setQQmlEngine::setOutputWarningsToStandardError(bool enabled)to true, and to connect to thewarningssignal ... No luck so far...
– Curtwagner1984
Dec 31 '18 at 22:41
Exactly, you have reviewed stackoverflow.com/questions/45805076/…: this is probably a bug, I recommend you report it.
– eyllanesc
Dec 31 '18 at 22:43
relative stackoverflow.com/questions/45805076/…
– eyllanesc
Dec 31 '18 at 22:29
relative stackoverflow.com/questions/45805076/…
– eyllanesc
Dec 31 '18 at 22:29
@eyllanesc I tried to set
QQmlEngine::setOutputWarningsToStandardError(bool enabled) to true, and to connect to the warnings signal ... No luck so far...– Curtwagner1984
Dec 31 '18 at 22:41
@eyllanesc I tried to set
QQmlEngine::setOutputWarningsToStandardError(bool enabled) to true, and to connect to the warnings signal ... No luck so far...– Curtwagner1984
Dec 31 '18 at 22:41
Exactly, you have reviewed stackoverflow.com/questions/45805076/…: this is probably a bug, I recommend you report it.
– eyllanesc
Dec 31 '18 at 22:43
Exactly, you have reviewed stackoverflow.com/questions/45805076/…: this is probably a bug, I recommend you report it.
– eyllanesc
Dec 31 '18 at 22:43
add a comment |
1 Answer
1
active
oldest
votes
If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.
import os
import sys
from PySide2 import QtCore, QtGui, QtQml
def qt_message_handler(mode, context, message):
if mode == QtCore.QtInfoMsg:
mode = 'Info'
elif mode == QtCore.QtWarningMsg:
mode = 'Warning'
elif mode == QtCore.QtCriticalMsg:
mode = 'critical'
elif mode == QtCore.QtFatalMsg:
mode = 'fatal'
else:
mode = 'Debug'
print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
QtCore.qInstallMessageHandler(qt_message_handler)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
if not engine.load(QtCore.QUrl.fromLocalFile(qml_filename)):
sys.exit(-1)
sys.exit(app.exec_())
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in aheight:parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where myengine.loadis failing. Yet it seems thatqInstallMessageHandlerisn't helpful for this scenario.
– Curtwagner1984
Jan 1 at 0:32
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991306%2fpyside-how-to-see-qml-errors-in-python-console%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.
import os
import sys
from PySide2 import QtCore, QtGui, QtQml
def qt_message_handler(mode, context, message):
if mode == QtCore.QtInfoMsg:
mode = 'Info'
elif mode == QtCore.QtWarningMsg:
mode = 'Warning'
elif mode == QtCore.QtCriticalMsg:
mode = 'critical'
elif mode == QtCore.QtFatalMsg:
mode = 'fatal'
else:
mode = 'Debug'
print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
QtCore.qInstallMessageHandler(qt_message_handler)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
if not engine.load(QtCore.QUrl.fromLocalFile(qml_filename)):
sys.exit(-1)
sys.exit(app.exec_())
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in aheight:parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where myengine.loadis failing. Yet it seems thatqInstallMessageHandlerisn't helpful for this scenario.
– Curtwagner1984
Jan 1 at 0:32
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
|
show 5 more comments
If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.
import os
import sys
from PySide2 import QtCore, QtGui, QtQml
def qt_message_handler(mode, context, message):
if mode == QtCore.QtInfoMsg:
mode = 'Info'
elif mode == QtCore.QtWarningMsg:
mode = 'Warning'
elif mode == QtCore.QtCriticalMsg:
mode = 'critical'
elif mode == QtCore.QtFatalMsg:
mode = 'fatal'
else:
mode = 'Debug'
print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
QtCore.qInstallMessageHandler(qt_message_handler)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
if not engine.load(QtCore.QUrl.fromLocalFile(qml_filename)):
sys.exit(-1)
sys.exit(app.exec_())
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in aheight:parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where myengine.loadis failing. Yet it seems thatqInstallMessageHandlerisn't helpful for this scenario.
– Curtwagner1984
Jan 1 at 0:32
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
|
show 5 more comments
If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.
import os
import sys
from PySide2 import QtCore, QtGui, QtQml
def qt_message_handler(mode, context, message):
if mode == QtCore.QtInfoMsg:
mode = 'Info'
elif mode == QtCore.QtWarningMsg:
mode = 'Warning'
elif mode == QtCore.QtCriticalMsg:
mode = 'critical'
elif mode == QtCore.QtFatalMsg:
mode = 'fatal'
else:
mode = 'Debug'
print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
QtCore.qInstallMessageHandler(qt_message_handler)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
if not engine.load(QtCore.QUrl.fromLocalFile(qml_filename)):
sys.exit(-1)
sys.exit(app.exec_())
If you want to know the error message when using QQmlApplicationEngine you should use the warnings signal but it does not seem to work, so a workaround is to use qInstallMessageHandler to get the messages that Qt gives.
import os
import sys
from PySide2 import QtCore, QtGui, QtQml
def qt_message_handler(mode, context, message):
if mode == QtCore.QtInfoMsg:
mode = 'Info'
elif mode == QtCore.QtWarningMsg:
mode = 'Warning'
elif mode == QtCore.QtCriticalMsg:
mode = 'critical'
elif mode == QtCore.QtFatalMsg:
mode = 'fatal'
else:
mode = 'Debug'
print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file))
if __name__ == '__main__':
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
QtCore.qInstallMessageHandler(qt_message_handler)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml')
if not engine.load(QtCore.QUrl.fromLocalFile(qml_filename)):
sys.exit(-1)
sys.exit(app.exec_())
edited Dec 31 '18 at 23:13
answered Dec 31 '18 at 23:03
eyllanesceyllanesc
78k103156
78k103156
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in aheight:parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where myengine.loadis failing. Yet it seems thatqInstallMessageHandlerisn't helpful for this scenario.
– Curtwagner1984
Jan 1 at 0:32
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
|
show 5 more comments
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in aheight:parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where myengine.loadis failing. Yet it seems thatqInstallMessageHandlerisn't helpful for this scenario.
– Curtwagner1984
Jan 1 at 0:32
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in a
height: parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where my engine.load is failing. Yet it seems that qInstallMessageHandler isn't helpful for this scenario.– Curtwagner1984
Jan 1 at 0:32
This seems to work when there is an error in the QML files themselves,(For example when I input a string value in a
height: parameter) however, it doesn't work when there is a problem in the imports. I made a delegate in another file(In the same directory as 'main.qml') and I'm trying to import it into 'main.qml' this is where my engine.load is failing. Yet it seems that qInstallMessageHandler isn't helpful for this scenario.– Curtwagner1984
Jan 1 at 0:32
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@Curtwagner1984 You could share your test through gist or github.
– eyllanesc
Jan 1 at 0:34
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@eyllansec Yes, Here it is: gist.github.com/curtwagner1984/c091e4ec262289e2c59e911137d1b5c6
– Curtwagner1984
Jan 1 at 0:48
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@Curtwagner1984 Do not you have another simpler example? I do not want to have to get the credentials of reddit to be able to test the problem :-)
– eyllanesc
Jan 1 at 0:55
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
@eyllansec I've changed the main, added a moc sub, also created a new 'qml' file. Right now it exits with code -1 without telling what the error is. I've updated the gist Does it work for you when you try to import a file that doesn't exist? (IE does it complain that a file doesn't exist, or just exits with -1?)
– Curtwagner1984
Jan 1 at 1:13
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991306%2fpyside-how-to-see-qml-errors-in-python-console%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
relative stackoverflow.com/questions/45805076/…
– eyllanesc
Dec 31 '18 at 22:29
@eyllanesc I tried to set
QQmlEngine::setOutputWarningsToStandardError(bool enabled)to true, and to connect to thewarningssignal ... No luck so far...– Curtwagner1984
Dec 31 '18 at 22:41
Exactly, you have reviewed stackoverflow.com/questions/45805076/…: this is probably a bug, I recommend you report it.
– eyllanesc
Dec 31 '18 at 22:43