Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Last active March 14, 2018 14:11
Show Gist options
  • Save peteristhegreat/dbd0e481e3a6b5d89958e6d0e26d1a27 to your computer and use it in GitHub Desktop.
Save peteristhegreat/dbd0e481e3a6b5d89958e6d0e26d1a27 to your computer and use it in GitHub Desktop.
QML console.log qDebug() to QML TextEdit object using qInstallMessageHandler
#include <QtMessageHandler>
#include <QQmlApplicationEngine>
#include <QQmlContext>
QObject * s_qml_text_edit = 0;
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if(s_qml_text_edit)
{
QString temp = msg;
// http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods
QMetaObject::invokeMethod(s_qml_text_edit, "append",
// Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QString, temp));
// QByteArray localMsg = msg.toLocal8Bit();
// fprintf(stderr, "Debug1: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
// break;
}
else if(MainWindow::s_textEdit)
{
switch (type) {
case QtDebugMsg:
case QtWarningMsg:
case QtCriticalMsg:
// redundant check, could be removed, or the
// upper if statement could be removed
if(MainWindow::s_textEdit != 0)
MainWindow::appendText(msg);
break;
case QtFatalMsg:
abort();
}
}
else
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
abort();
}
}
}
int main() {
qInstallMessageHandler(myMessageOutput);
// ...
QApplication a(argc, argv);// can be changed to QGuiApplication for QML only
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/MainWindowForm.qml")));
// get the object with the name "textEdit" and make it accessible by the messageHandler
s_qml_text_edit = engine.rootObjects().first()->findChild<QObject*>("textEdit");
QmlMainWindow w(engine.rootObjects().first());
w.start();
return a.exec();
}
import QtQuick.Window 2.2
Window {
id: root
Flickable {
id: flick
Layout.minimumHeight: 50
Layout.fillHeight: true
contentWidth: textEdit.paintedWidth
contentHeight: textEdit.paintedHeight
clip: true
function ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}
TextEdit {
objectName: "textEdit"
id: textEdit
width: flick.width
height: flick.height
focus: true
wrapMode: TextEdit.Wrap
text: qsTr("QML Debug Output")
font.pixelSize: 12
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
onTextChanged: cursorPosition = text.length
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment