Last active
December 31, 2015 03:59
-
-
Save nbergont/7930963 to your computer and use it in GitHub Desktop.
Widget console de log pour Qt.
Exemple : int main()
{ InstallQConsoleMsgHandler(); ...
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "QConsoleWidget.h" | |
#include <QMutex> | |
#include <QAction> | |
#define HTML_CONSOLE_MODE | |
#if QT_VERSION >= 0x050000 | |
static QtMessageHandler _old_handler = 0; | |
#else | |
static QtMsgHandler _old_handler = 0; | |
#endif | |
static QMutex _console_mutex; | |
const int _max_buffer_console = 10000; | |
static QByteArray _buffer_console; | |
static bool _console_critical_msg = false; | |
void InstallQConsoleMsgHandler() | |
{ | |
#if QT_VERSION >= 0x050000 | |
_old_handler = qInstallMessageHandler(QConsoleMsgHandler); | |
#else | |
_old_handler = qInstallMsgHandler(QConsoleMsgHandler); | |
#endif | |
_buffer_console.reserve(_max_buffer_console); | |
} | |
#if QT_VERSION >= 0x050000 | |
void QConsoleMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) | |
#else | |
void QConsoleMsgHandler(QtMsgType type, const char *msg) | |
#endif | |
{ | |
_console_mutex.lock(); | |
//_buffer_console.append(QString::number((int)QThread::currentThreadId())); | |
//_buffer_console.append(" - "); | |
switch(type) | |
{ | |
case QtDebugMsg: | |
_buffer_console.append("<span style=\"color:black;\">Debug : "); | |
break; | |
case QtWarningMsg: | |
_buffer_console.append("<span style=\"color:#ff8d02;font-weight:bold;\">Warning : "); | |
_console_critical_msg = true; | |
break; | |
case QtCriticalMsg: | |
_buffer_console.append("<span style=\"color:red;font-weight:bold;\">Critical : "); | |
_console_critical_msg = true; | |
break; | |
case QtFatalMsg: | |
_buffer_console.append("<span style=\"color:red;font-weight:bold;\">Fatal : "); | |
_console_critical_msg = true; | |
break; | |
} | |
#if QT_VERSION >= 0x050000 | |
_buffer_console.append(msg.toHtmlEscaped()); | |
#else | |
_buffer_console.append(Qt::escape(QString::fromLocal8Bit(msg))); | |
#endif | |
_buffer_console.append("</span><br>"); | |
//Ancien handler (utile pour execution dans Qt Creator) | |
if(_old_handler) | |
#if QT_VERSION >= 0x050000 | |
_old_handler(type, context, msg); | |
#else | |
_old_handler(type, msg); | |
#endif | |
_console_mutex.unlock(); | |
} | |
QConsoleWidget::QConsoleWidget(QWidget *parent) : QPlainTextEdit(parent) | |
{ | |
setWordWrapMode(QTextOption::NoWrap); | |
setMaximumBlockCount(_max_buffer_console); | |
setReadOnly(true); | |
setContextMenuPolicy(Qt::ActionsContextMenu); | |
QAction *clear_action = new QAction(tr("Clear"), this); | |
connect(clear_action, SIGNAL(triggered()), SLOT(clear())); | |
addAction(clear_action); | |
QAction *copy_action = new QAction(tr("Copy"), this); | |
connect(copy_action, SIGNAL(triggered()), SLOT(copy())); | |
addAction(copy_action); | |
/* | |
QPalette p = palette(); | |
p.setColor(QPalette::Active, QPalette::Base, Qt::black); | |
p.setColor(QPalette::Inactive, QPalette::Base, Qt::black); | |
setPalette(p); | |
*/ | |
connect(&_update_timer, SIGNAL(timeout()), SLOT(update())); | |
_update_timer.start(200); | |
} | |
void QConsoleWidget::update() | |
{ | |
_console_mutex.lock(); | |
if(!_buffer_console.isEmpty()) | |
{ | |
_buffer_console.chop(4); //Last <br> | |
appendHtml(_buffer_console); | |
ensureCursorVisible(); | |
_buffer_console.clear(); | |
if(_console_critical_msg) | |
{ | |
emit critical_msg(); | |
_console_critical_msg = false; | |
} | |
} | |
_console_mutex.unlock(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef QCONSOLEWIDGET_H | |
#define QCONSOLEWIDGET_H | |
#include <QPlainTextEdit> | |
#include <QTimer> | |
void InstallQConsoleMsgHandler(); | |
#if QT_VERSION >= 0x050000 | |
void QConsoleMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); | |
#else | |
void QConsoleMsgHandler(QtMsgType type, const char *msg); | |
#endif | |
class QConsoleWidget : public QPlainTextEdit | |
{ | |
Q_OBJECT | |
public: | |
explicit QConsoleWidget(QWidget *parent = 0); | |
signals: | |
void critical_msg(); | |
private slots: | |
void update(); | |
private: | |
QTimer _update_timer; | |
}; | |
#endif // QCONSOLEWIDGET_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment