Browser for QDebug output: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
'''English''' | [[Browser_for_QDebug_output_German|Deutsch]] | '''English''' | [[Browser_for_QDebug_output_German|Deutsch]] | ||
[toc align_right= | [toc align_right="yes" depth="4"] | ||
= A Browser for QDebug Log Output = | = A Browser for QDebug Log Output = | ||
Line 13: | Line 13: | ||
==== logbrowser.pro ==== | ==== logbrowser.pro ==== | ||
<code> | <code> | ||
QT ''= core gui | |||
TARGET = DebugWindow | |||
TEMPLATE = app | |||
SOURCES''= main.cpp logbrowserdialog.cpp logbrowser.cpp | |||
HEADERS += logbrowserdialog.h logbrowser.h | |||
</code> | |||
==== logbrowser.h ==== | ==== logbrowser.h ==== | ||
Line 21: | Line 27: | ||
<code> | <code> | ||
#ifndef LOGBROWSER_H | #ifndef LOGBROWSER_H | ||
#define LOGBROWSER_H | |||
#include | #include <QObject> | ||
class LogBrowserDialog; | class LogBrowserDialog; | ||
class LogBrowser : public QObject | class LogBrowser : public QObject | ||
{ | |||
Q_OBJECT | |||
public: | |||
explicit LogBrowser(QObject *parent = 0); | |||
~LogBrowser(); | |||
public slots: | public slots: | ||
void outputMessage( QtMsgType type, const QString &msg ); | |||
signals: | signals: | ||
void sendMessage( QtMsgType type, const QString &msg ); | |||
private: | private: | ||
LogBrowserDialog *browserDialog; | |||
}; | }; | ||
#endif // LOGBROWSER_H | #endif // LOGBROWSER_H | ||
</code> | |||
==== logbrowser.cpp ==== | ==== logbrowser.cpp ==== | ||
Line 43: | Line 59: | ||
<code> | <code> | ||
#include | #include "logbrowser.h" | ||
#include | #include <QMetaType> | ||
#include | #include "logbrowserdialog.h" | ||
LogBrowser::LogBrowser(QObject *parent) : | LogBrowser::LogBrowser(QObject *parent) : | ||
QObject(parent) | |||
{ | |||
qRegisterMetaType<QtMsgType>("QtMsgType"); | |||
browserDialog = new LogBrowserDialog; | |||
connect(this, SIGNAL (sendMessage(QtMsgType,QString)), browserDialog, SLOT (outputMessage(QtMsgType,QString)), Qt::QueuedConnection); | |||
browserDialog->show(); | |||
} | |||
LogBrowser::~LogBrowser() | LogBrowser::~LogBrowser() | ||
{ | |||
delete browserDialog; | |||
} | |||
void LogBrowser::outputMessage(QtMsgType type, const QString &msg) | void LogBrowser::outputMessage(QtMsgType type, const QString &msg) | ||
{ | |||
emit sendMessage( type, msg ); | |||
} | |||
</code> | |||
==== logbrowserdialog.h ==== | ==== logbrowserdialog.h ==== | ||
Line 59: | Line 89: | ||
<code> | <code> | ||
#ifndef DIALOG_H | #ifndef DIALOG_H | ||
#define DIALOG_H | |||
#include | #include <QDialog> | ||
class QTextBrowser; | class QTextBrowser; | ||
class QPushButton; | |||
class LogBrowserDialog : public QDialog | class LogBrowserDialog : public QDialog | ||
{ | |||
Q_OBJECT | |||
public: | public: | ||
LogBrowserDialog(QWidget *parent = 0); | |||
~LogBrowserDialog(); | |||
public slots: | public slots: | ||
void outputMessage( QtMsgType type, const QString &msg ); | |||
protected slots: | protected slots: | ||
void save(); | |||
protected: | protected: | ||
virtual void keyPressEvent( QKeyEvent *e ); | |||
virtual void closeEvent( QCloseEvent *e ); | |||
QTextBrowser *browser; | QTextBrowser *browser; | ||
QPushButton *clearButton; | |||
QPushButton *saveButton; | |||
}; | |||
#endif // DIALOG_H | #endif // DIALOG_H | ||
</code> | |||
==== logbrowserdialog.cpp ==== | ==== logbrowserdialog.cpp ==== | ||
Line 83: | Line 127: | ||
<code> | <code> | ||
#include | #include "logbrowserdialog.h" | ||
#include <QVBoxLayout> | |||
#include <QHBoxLayout> | |||
#include <QTextBrowser> | |||
#include <QPushButton> | |||
#include <QFileDialog> | |||
#include <QDir> | |||
#include <QFile> | |||
#include <QMessageBox> | |||
#include <QTextStream> | |||
#include <QCloseEvent> | |||
#include <QKeyEvent> | |||
LogBrowserDialog::LogBrowserDialog(QWidget *parent) | |||
: QDialog(parent) | |||
{ | |||
QVBoxLayout *layout = new QVBoxLayout; | |||
setLayout(layout); | |||
browser = new QTextBrowser(this); | |||
layout->addWidget(browser); | |||
QHBoxLayout '''buttonLayout = new QHBoxLayout; | |||
buttonLayout->setContentsMargins(0, 0, 0, 0); | |||
layout->addLayout(buttonLayout); | |||
buttonLayout->addStretch(10); | |||
clearButton = new QPushButton(this); | |||
clearButton->setText("clear"); | |||
buttonLayout->addWidget(clearButton); | |||
connect(clearButton, SIGNAL (clicked()), browser, SLOT (clear())); | |||
saveButton = new QPushButton(this); | |||
saveButton->setText("save output"); | |||
buttonLayout->addWidget(saveButton); | |||
connect(saveButton, SIGNAL (clicked()), this, SLOT (save())); | |||
resize(200, 400); | |||
} | |||
LogBrowserDialog::~LogBrowserDialog() | |||
{ | |||
} | |||
LogBrowserDialog:: | void LogBrowserDialog::outputMessage(QtMsgType type, const QString &msg) | ||
{ | |||
switch (type) { | |||
case QtDebugMsg: | |||
browser->append(msg); | |||
break; | |||
browser | case QtWarningMsg: | ||
browser->append(tr("— WARNING: %1").arg(msg)); | |||
break; | |||
case QtCriticalMsg: | |||
browser->append(tr("— CRITICAL: %1").arg(msg)); | |||
break; | |||
case QtFatalMsg: | |||
browser->append(tr("— FATAL: %1").arg(msg)); | |||
break; | |||
} | |||
} | |||
void LogBrowserDialog::save() | |||
{ | |||
QString saveFileName = QFileDialog::getSaveFileName( | |||
this, | |||
tr("Save Log Output"), | |||
tr("%1/logfile.txt").arg(QDir::homePath()), | |||
tr("Text Files ('''.txt);;All Files (*)") | |||
); | |||
if(saveFileName.isEmpty()) | if(saveFileName.isEmpty()) | ||
return; | |||
QFile file | QFile file(saveFileName); | ||
if(!file.open(QIODevice::WriteOnly)) { | |||
QMessageBox::warning( | |||
this, | |||
tr("Error"), | |||
QString(tr("<nobr>File '%1'<br/>cannot be opened for writing.<br/><br/>" | |||
"The log output could <b>not</b> be saved!</nobr>")) | |||
.arg(saveFileName)); | |||
return; | |||
} | |||
QTextStream stream(&file);< | QTextStream stream(&file); | ||
stream << browser->toPlainText(); | |||
file.close(); | |||
} | |||
void LogBrowserDialog::closeEvent(QCloseEvent *e) | void LogBrowserDialog::closeEvent(QCloseEvent *e) | ||
{ | |||
QMessageBox::StandardButton answer = QMessageBox::question( | |||
this, | |||
tr("Close Log Browser?"), | |||
tr("Do you really want to close the log browser?"), | |||
QMessageBox::Yes | QMessageBox::No | |||
); | |||
if (answer == QMessageBox::Yes) | if (answer == QMessageBox::Yes) | ||
e->accept(); | |||
else | |||
e->ignore(); | |||
} | |||
void LogBrowserDialog::keyPressEvent(QKeyEvent *e) | void LogBrowserDialog::keyPressEvent(QKeyEvent *e) | ||
{ | |||
// ignore all keyboard events | |||
// protects against accidentally closing of the dialog | |||
// without asking the user | |||
e->ignore(); | |||
} | |||
</code> | |||
==== main.cpp ==== | ==== main.cpp ==== | ||
Line 123: | Line 254: | ||
<code> | <code> | ||
#include | #include <QApplication> | ||
#include <QPointer> | |||
#include <QDebug> | |||
#include "logbrowser.h" | |||
QPointer | QPointer<LogBrowser> logBrowser; | ||
void myMessageOutput(QtMsgType type, const char *msg) | void myMessageOutput(QtMsgType type, const char *msg) | ||
{ | |||
if(logBrowser) | |||
logBrowser->outputMessage( type, msg ); | |||
} | |||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | |||
QApplication a(argc, argv); | |||
logBrowser = new LogBrowser; | logBrowser = new LogBrowser; | ||
qInstallMsgHandler(myMessageOutput); | |||
qDebug() | qDebug() << "test for debug"; | ||
int result = a.exec(); | |||
qDebug() << "application exec return result =" << result; | |||
delete logBrowser; | delete logBrowser; | ||
return result; | |||
} | |||
</code> |
Revision as of 09:24, 25 February 2015
English | Deutsch
[toc align_right="yes" depth="4"]
A Browser for QDebug Log Output
The code on this page shows how a simple log browser can be added to an application.
It consists of the actual log browser (class LogBrowserDialog in logbrowserdialog.h and logbrowserdialog.h) and a small wrapper (class LogBrowser in logbrowser.h and logbrowser.h). The wrapper is instantiated in the main function of an application and creates the browser window. It also acts as an intermediary and converts the const char * based messages from the debug system into QString based messages. With this trick the debug messages can be sent to the actual browser by the means of signal/slot connections. This adds basic thread support to the browser.
logbrowser.pro
QT ''= core gui
TARGET = DebugWindow
TEMPLATE = app
SOURCES''= main.cpp logbrowserdialog.cpp logbrowser.cpp
HEADERS += logbrowserdialog.h logbrowser.h
logbrowser.h
#ifndef LOGBROWSER_H
#define LOGBROWSER_H
#include <QObject>
class LogBrowserDialog;
class LogBrowser : public QObject
{
Q_OBJECT
public:
explicit LogBrowser(QObject *parent = 0);
~LogBrowser();
public slots:
void outputMessage( QtMsgType type, const QString &msg );
signals:
void sendMessage( QtMsgType type, const QString &msg );
private:
LogBrowserDialog *browserDialog;
};
#endif // LOGBROWSER_H
logbrowser.cpp
#include "logbrowser.h"
#include <QMetaType>
#include "logbrowserdialog.h"
LogBrowser::LogBrowser(QObject *parent) :
QObject(parent)
{
qRegisterMetaType<QtMsgType>("QtMsgType");
browserDialog = new LogBrowserDialog;
connect(this, SIGNAL (sendMessage(QtMsgType,QString)), browserDialog, SLOT (outputMessage(QtMsgType,QString)), Qt::QueuedConnection);
browserDialog->show();
}
LogBrowser::~LogBrowser()
{
delete browserDialog;
}
void LogBrowser::outputMessage(QtMsgType type, const QString &msg)
{
emit sendMessage( type, msg );
}
logbrowserdialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QTextBrowser;
class QPushButton;
class LogBrowserDialog : public QDialog
{
Q_OBJECT
public:
LogBrowserDialog(QWidget *parent = 0);
~LogBrowserDialog();
public slots:
void outputMessage( QtMsgType type, const QString &msg );
protected slots:
void save();
protected:
virtual void keyPressEvent( QKeyEvent *e );
virtual void closeEvent( QCloseEvent *e );
QTextBrowser *browser;
QPushButton *clearButton;
QPushButton *saveButton;
};
#endif // DIALOG_H
logbrowserdialog.cpp
#include "logbrowserdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextBrowser>
#include <QPushButton>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <QCloseEvent>
#include <QKeyEvent>
LogBrowserDialog::LogBrowserDialog(QWidget *parent)
: QDialog(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
browser = new QTextBrowser(this);
layout->addWidget(browser);
QHBoxLayout '''buttonLayout = new QHBoxLayout;
buttonLayout->setContentsMargins(0, 0, 0, 0);
layout->addLayout(buttonLayout);
buttonLayout->addStretch(10);
clearButton = new QPushButton(this);
clearButton->setText("clear");
buttonLayout->addWidget(clearButton);
connect(clearButton, SIGNAL (clicked()), browser, SLOT (clear()));
saveButton = new QPushButton(this);
saveButton->setText("save output");
buttonLayout->addWidget(saveButton);
connect(saveButton, SIGNAL (clicked()), this, SLOT (save()));
resize(200, 400);
}
LogBrowserDialog::~LogBrowserDialog()
{
}
void LogBrowserDialog::outputMessage(QtMsgType type, const QString &msg)
{
switch (type) {
case QtDebugMsg:
browser->append(msg);
break;
case QtWarningMsg:
browser->append(tr("— WARNING: %1").arg(msg));
break;
case QtCriticalMsg:
browser->append(tr("— CRITICAL: %1").arg(msg));
break;
case QtFatalMsg:
browser->append(tr("— FATAL: %1").arg(msg));
break;
}
}
void LogBrowserDialog::save()
{
QString saveFileName = QFileDialog::getSaveFileName(
this,
tr("Save Log Output"),
tr("%1/logfile.txt").arg(QDir::homePath()),
tr("Text Files ('''.txt);;All Files (*)")
);
if(saveFileName.isEmpty())
return;
QFile file(saveFileName);
if(!file.open(QIODevice::WriteOnly)) {
QMessageBox::warning(
this,
tr("Error"),
QString(tr("<nobr>File '%1'<br/>cannot be opened for writing.<br/><br/>"
"The log output could <b>not</b> be saved!</nobr>"))
.arg(saveFileName));
return;
}
QTextStream stream(&file);
stream << browser->toPlainText();
file.close();
}
void LogBrowserDialog::closeEvent(QCloseEvent *e)
{
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Close Log Browser?"),
tr("Do you really want to close the log browser?"),
QMessageBox::Yes | QMessageBox::No
);
if (answer == QMessageBox::Yes)
e->accept();
else
e->ignore();
}
void LogBrowserDialog::keyPressEvent(QKeyEvent *e)
{
// ignore all keyboard events
// protects against accidentally closing of the dialog
// without asking the user
e->ignore();
}
main.cpp
#include <QApplication>
#include <QPointer>
#include <QDebug>
#include "logbrowser.h"
QPointer<LogBrowser> logBrowser;
void myMessageOutput(QtMsgType type, const char *msg)
{
if(logBrowser)
logBrowser->outputMessage( type, msg );
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
logBrowser = new LogBrowser;
qInstallMsgHandler(myMessageOutput);
qDebug() << "test for debug";
int result = a.exec();
qDebug() << "application exec return result =" << result;
delete logBrowser;
return result;
}