PySide如何在python控制台中查看QML错误?

时间:2018-12-31 20:49:16

标签: python qml pyside2

我有以下代码:

CREATE MATERIALIZED VIEW my_materialized_view
AS
SELECT
  id,
  column_a,
  column_b,
  array_agg(description) as column_c
FROM table_a
JOIN table_b on table_b.id = any(column_c)
GROUP BY table_a.id
ORDER BY table_a.id ASC
WITH DATA;

如您所见,如果engine.load失败了,我将看到的是退出代码-1,而没有详细说明为什么失败以及发生了什么错误。如何在python控制台中打印QML错误?

使用import React from "react"; import { Link } from "react-router-dom"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import { openModal } from "../modal/index"; import Login from "../auth/Login"; import Register from "../auth/Register"; class Navbar extends React.Component { static propTypes = { dispatch: PropTypes.func.isRequired, }; constructor(props) { super(props); this.login = this.login.bind(this); this.register = this.register.bind(this); } login() { this.props.dispatch( openModal({content: <Login />,title: "Login"})); } register() { this.props.dispatch( openModal({content: <Register />,title: "Register"})); } render() { const guestLinks = ( <ul className="navbar-nav ml-auto"> <li className="nav-item"> <button className="m-1 btn-sm btn btn-outline-primary mt-1" onClick={this.register}>Register</button> </li> <li className="nav-item"> <button className="m-1 btn-sm btn btn-outline-primary mt-1" onClick={this.login}>Login</button> </li> </ul> ); return ( <nav className="navbar navbar-expand-sm navbar-light bg-light mb-4"> <div className="container"> <Link className="navbar-brand" to="/"> Domras </Link> <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#mobile-nav"> <span className="navbar-toggler-icon" /> </button> <div className="collapse navbar-collapse" id="mobile-nav"> <ul className="navbar-nav mr-auto"> <li className="nav-item"> <Link className="nav-link" to="/profiles"> Driver </Link> </li> </ul> </div> </div> </nav> ); } } const mapStateToProps = state => ({ auth: state.auth, }); export default connect(mapStateToProps)(Navbar); 代替QQmlApplicationEngine时有一个解决方法,并且在此post中有描述,但是,我想知道QQmlApplicationEngine是否可以实现类似的功能吗?

1 个答案:

答案 0 :(得分:1)

如果您想在使用QQmlApplicationEngine时了解错误消息,则应使用warnings信号,但它似乎不起作用,因此一种解决方法是使用qInstallMessageHandler获取Qt给出的消息

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')
    engine.load(QtCore.QUrl.fromLocalFile(qml_filename))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())