Browser for QDebug output: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=A Browser for QDebug Log Output=
[[Category:snippets]]
 
'''English''' | [[Browser_for_QDebug_output_German|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.
The code on this page shows how a simple log browser can be added to an application.
Line 5: Line 11:
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.
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====
==== logbrowser.pro ====
 
<code><br />QT ''= core gui
<br />TARGET = DebugWindow<br />TEMPLATE = app
<br />SOURCES''= main.cpp logbrowserdialog.cpp logbrowser.cpp<br />HEADERS += logbrowserdialog.h logbrowser.h<br /></code>
 
==== logbrowser.h ====
 
<code>
 
#ifndef LOGBROWSER_H<br />#define LOGBROWSER_H
 
#include &lt;QObject&amp;gt;
 
class LogBrowserDialog;
 
class LogBrowser : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit LogBrowser(QObject *parent = 0);<br /> ~LogBrowser();
 
public slots:<br /> void outputMessage( QtMsgType type, const QString &amp;msg );
 
signals:<br /> void sendMessage( QtMsgType type, const QString &amp;msg );
 
private:<br /> LogBrowserDialog *browserDialog;
 
};
 
#endif // LOGBROWSER_H<br /></code>
 
==== logbrowser.cpp ====
 
<code>
 
#include &quot;logbrowser.h&amp;quot;
 
#include &lt;QMetaType&amp;gt;
 
#include &quot;logbrowserdialog.h&amp;quot;
 
LogBrowser::LogBrowser(QObject *parent) :<br /> QObject(parent)<br />{<br /> qRegisterMetaType&amp;lt;QtMsgType&amp;gt;(&quot;QtMsgType&amp;quot;);<br /> browserDialog = new LogBrowserDialog;<br /> connect(this, SIGNAL (sendMessage(QtMsgType,QString)), browserDialog, SLOT (outputMessage(QtMsgType,QString)), Qt::QueuedConnection);<br /> browserDialog-&gt;show();<br />}
 
LogBrowser::~LogBrowser()<br />{<br /> delete browserDialog;<br />}
 
void LogBrowser::outputMessage(QtMsgType type, const QString &amp;msg)<br />{<br /> emit sendMessage( type, msg );<br />}<br /></code>
 
==== logbrowserdialog.h ====
 
<code>
 
#ifndef DIALOG_H<br />#define DIALOG_H
 
#include &lt;QDialog&amp;gt;
 
class QTextBrowser;<br />class QPushButton;
 
class LogBrowserDialog : public QDialog<br />{<br /> Q_OBJECT
 
public:<br /> LogBrowserDialog(QWidget *parent = 0);<br /> ~LogBrowserDialog();
 
public slots:<br /> void outputMessage( QtMsgType type, const QString &amp;msg );
 
protected slots:<br /> void save();
 
protected:<br /> virtual void keyPressEvent( QKeyEvent *e );<br /> virtual void closeEvent( QCloseEvent *e );
 
QTextBrowser *browser;<br /> QPushButton *clearButton;<br /> QPushButton *saveButton;<br />};
 
#endif // DIALOG_H<br /></code>
 
==== logbrowserdialog.cpp ====
 
<code>
 
#include &quot;logbrowserdialog.h&amp;quot;
 
#include &lt;QVBoxLayout&amp;gt;<br />#include &lt;QHBoxLayout&amp;gt;<br />#include &lt;QTextBrowser&amp;gt;<br />#include &lt;QPushButton&amp;gt;<br />#include &lt;QFileDialog&amp;gt;<br />#include &lt;QDir&amp;gt;<br />#include &lt;QFile&amp;gt;<br />#include &lt;QMessageBox&amp;gt;<br />#include &lt;QTextStream&amp;gt;<br />#include &lt;QCloseEvent&amp;gt;<br />#include &lt;QKeyEvent&amp;gt;
 
LogBrowserDialog::LogBrowserDialog(QWidget *parent)<br /> : QDialog(parent)<br />{<br /> QVBoxLayout *layout = new QVBoxLayout;<br /> setLayout(layout);
 
browser = new QTextBrowser(this);<br /> layout-&gt;addWidget(browser);
 
QHBoxLayout '''buttonLayout = new QHBoxLayout;<br /> buttonLayout-&gt;setContentsMargins(0, 0, 0, 0);<br /> layout-&gt;addLayout(buttonLayout);
<br /> buttonLayout-&gt;addStretch(10);
<br /> clearButton = new QPushButton(this);<br /> clearButton-&gt;setText(&quot;clear&amp;quot;);<br /> buttonLayout-&gt;addWidget(clearButton);<br /> connect(clearButton, SIGNAL (clicked()), browser, SLOT (clear()));
<br /> saveButton = new QPushButton(this);<br /> saveButton-&gt;setText(&quot;save output&amp;quot;);<br /> buttonLayout-&gt;addWidget(saveButton);<br /> connect(saveButton, SIGNAL (clicked()), this, SLOT (save()));
<br /> resize(200, 400);<br />}
 
<br />LogBrowserDialog::~LogBrowserDialog()<br />{
<br />}
 
<br />void LogBrowserDialog::outputMessage(QtMsgType type, const QString &amp;msg)<br />{<br /> switch (type) {<br /> case QtDebugMsg:<br /> browser-&gt;append(msg);<br /> break;
<br /> case QtWarningMsg:<br /> browser-&gt;append(tr(&quot;— WARNING: %1&amp;quot;).arg(msg));<br /> break;
<br /> case QtCriticalMsg:<br /> browser-&gt;append(tr(&quot;— CRITICAL: %1&amp;quot;).arg(msg));<br /> break;
<br /> case QtFatalMsg:<br /> browser-&gt;append(tr(&quot;— FATAL: %1&amp;quot;).arg(msg));<br /> break;<br /> }<br />}
 
<br />void LogBrowserDialog::save()<br />{<br /> QString saveFileName = QFileDialog::getSaveFileName(<br /> this,<br /> tr(&quot;Save Log Output&amp;quot;),<br /> tr(&quot;%1/logfile.txt&amp;quot;).arg(QDir::homePath()),<br /> tr(&quot;Text Files ('''.txt);;All Files (*)&quot;)<br /> );
 
if(saveFileName.isEmpty())<br /> return;
 
QFile file&amp;amp;#40;saveFileName&amp;amp;#41;;<br /> if(!file.open(QIODevice::WriteOnly)) {<br /> QMessageBox::warning(<br /> this,<br /> tr(&quot;Error&amp;quot;),<br /> QString(tr(&quot;&lt;nobr&amp;gt;File '%1'&lt;br/&amp;gt;cannot be opened for writing.&lt;br/&amp;gt;&lt;br/&amp;gt;&quot;<br /> &quot;The log output could &lt;b&amp;gt;not&amp;lt;/b&amp;gt; be saved!&lt;/nobr&amp;gt;&quot;))<br /> .arg(saveFileName));<br /> return;<br /> }
 
QTextStream stream(&amp;file);<br /> stream &lt;&lt; browser-&gt;toPlainText();<br /> file.close();<br />}
 
void LogBrowserDialog::closeEvent(QCloseEvent *e)<br />{<br /> QMessageBox::StandardButton answer = QMessageBox::question(<br /> this,<br /> tr(&quot;Close Log Browser?&quot;),<br /> tr(&quot;Do you really want to close the log browser?&quot;),<br /> QMessageBox::Yes | QMessageBox::No<br /> );
 
if (answer == QMessageBox::Yes)<br /> e-&gt;accept();<br /> else<br /> e-&gt;ignore();<br />}
 
void LogBrowserDialog::keyPressEvent(QKeyEvent *e)<br />{<br /> // ignore all keyboard events<br /> // protects against accidentally closing of the dialog<br /> // without asking the user<br /> e-&gt;ignore();<br />}<br /></code>
 
==== main.cpp ====
 
<code>


====logbrowser.h====
#include &lt;QApplication&amp;gt;<br />#include &lt;QPointer&amp;gt;<br />#include &lt;QDebug&amp;gt;<br />#include &quot;logbrowser.h&amp;quot;


====logbrowser.cpp====
QPointer&amp;lt;LogBrowser&amp;gt; logBrowser;


====logbrowserdialog.h====
void myMessageOutput(QtMsgType type, const char *msg)<br />{<br /> if(logBrowser)<br /> logBrowser-&gt;outputMessage( type, msg );<br />}


====logbrowserdialog.cpp====
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);


====main.cpp====
logBrowser = new LogBrowser;<br /> qInstallMsgHandler(myMessageOutput);


===Categories:===
qDebug() &lt;&lt; &quot;test for debug&amp;quot;;<br /> int result = a.exec&amp;amp;#40;&amp;#41;;<br /> qDebug() &lt;&lt; &quot;application exec return result =&quot; &lt;&lt; result;


* [[:Category:snippets|snippets]]
delete logBrowser;<br /> return result;<br />}<br /></code>

Revision as of 08:55, 24 February 2015


English | Deutsch

[toc align_right="yes&quot; depth="4&quot;]

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

<br />QT ''= core gui
<br />TARGET = DebugWindow<br />TEMPLATE = app
<br />SOURCES''= main.cpp logbrowserdialog.cpp logbrowser.cpp<br />HEADERS += logbrowserdialog.h logbrowser.h<br />

logbrowser.h

#ifndef LOGBROWSER_H<br />#define LOGBROWSER_H

#include &lt;QObject&amp;gt;

class LogBrowserDialog;

class LogBrowser : public QObject<br />{<br /> Q_OBJECT<br />public:<br /> explicit LogBrowser(QObject *parent = 0);<br /> ~LogBrowser();

public slots:<br /> void outputMessage( QtMsgType type, const QString &amp;msg );

signals:<br /> void sendMessage( QtMsgType type, const QString &amp;msg );

private:<br /> LogBrowserDialog *browserDialog;

};

#endif // LOGBROWSER_H<br />

logbrowser.cpp

#include &quot;logbrowser.h&amp;quot;

#include &lt;QMetaType&amp;gt;

#include &quot;logbrowserdialog.h&amp;quot;

LogBrowser::LogBrowser(QObject *parent) :<br /> QObject(parent)<br />{<br /> qRegisterMetaType&amp;lt;QtMsgType&amp;gt;(&quot;QtMsgType&amp;quot;);<br /> browserDialog = new LogBrowserDialog;<br /> connect(this, SIGNAL (sendMessage(QtMsgType,QString)), browserDialog, SLOT (outputMessage(QtMsgType,QString)), Qt::QueuedConnection);<br /> browserDialog-&gt;show();<br />}

LogBrowser::~LogBrowser()<br />{<br /> delete browserDialog;<br />}

void LogBrowser::outputMessage(QtMsgType type, const QString &amp;msg)<br />{<br /> emit sendMessage( type, msg );<br />}<br />

logbrowserdialog.h

#ifndef DIALOG_H<br />#define DIALOG_H

#include &lt;QDialog&amp;gt;

class QTextBrowser;<br />class QPushButton;

class LogBrowserDialog : public QDialog<br />{<br /> Q_OBJECT

public:<br /> LogBrowserDialog(QWidget *parent = 0);<br /> ~LogBrowserDialog();

public slots:<br /> void outputMessage( QtMsgType type, const QString &amp;msg );

protected slots:<br /> void save();

protected:<br /> virtual void keyPressEvent( QKeyEvent *e );<br /> virtual void closeEvent( QCloseEvent *e );

QTextBrowser *browser;<br /> QPushButton *clearButton;<br /> QPushButton *saveButton;<br />};

#endif // DIALOG_H<br />

logbrowserdialog.cpp

#include &quot;logbrowserdialog.h&amp;quot;

#include &lt;QVBoxLayout&amp;gt;<br />#include &lt;QHBoxLayout&amp;gt;<br />#include &lt;QTextBrowser&amp;gt;<br />#include &lt;QPushButton&amp;gt;<br />#include &lt;QFileDialog&amp;gt;<br />#include &lt;QDir&amp;gt;<br />#include &lt;QFile&amp;gt;<br />#include &lt;QMessageBox&amp;gt;<br />#include &lt;QTextStream&amp;gt;<br />#include &lt;QCloseEvent&amp;gt;<br />#include &lt;QKeyEvent&amp;gt;

LogBrowserDialog::LogBrowserDialog(QWidget *parent)<br /> : QDialog(parent)<br />{<br /> QVBoxLayout *layout = new QVBoxLayout;<br /> setLayout(layout);

browser = new QTextBrowser(this);<br /> layout-&gt;addWidget(browser);

QHBoxLayout '''buttonLayout = new QHBoxLayout;<br /> buttonLayout-&gt;setContentsMargins(0, 0, 0, 0);<br /> layout-&gt;addLayout(buttonLayout);
<br /> buttonLayout-&gt;addStretch(10);
<br /> clearButton = new QPushButton(this);<br /> clearButton-&gt;setText(&quot;clear&amp;quot;);<br /> buttonLayout-&gt;addWidget(clearButton);<br /> connect(clearButton, SIGNAL (clicked()), browser, SLOT (clear()));
<br /> saveButton = new QPushButton(this);<br /> saveButton-&gt;setText(&quot;save output&amp;quot;);<br /> buttonLayout-&gt;addWidget(saveButton);<br /> connect(saveButton, SIGNAL (clicked()), this, SLOT (save()));
<br /> resize(200, 400);<br />}

<br />LogBrowserDialog::~LogBrowserDialog()<br />{
<br />}

<br />void LogBrowserDialog::outputMessage(QtMsgType type, const QString &amp;msg)<br />{<br /> switch (type) {<br /> case QtDebugMsg:<br /> browser-&gt;append(msg);<br /> break;
<br /> case QtWarningMsg:<br /> browser-&gt;append(tr(&quot; WARNING: %1&amp;quot;).arg(msg));<br /> break;
<br /> case QtCriticalMsg:<br /> browser-&gt;append(tr(&quot; CRITICAL: %1&amp;quot;).arg(msg));<br /> break;
<br /> case QtFatalMsg:<br /> browser-&gt;append(tr(&quot; FATAL: %1&amp;quot;).arg(msg));<br /> break;<br /> }<br />}

<br />void LogBrowserDialog::save()<br />{<br /> QString saveFileName = QFileDialog::getSaveFileName(<br /> this,<br /> tr(&quot;Save Log Output&amp;quot;),<br /> tr(&quot;%1/logfile.txt&amp;quot;).arg(QDir::homePath()),<br /> tr(&quot;Text Files ('''.txt);;All Files (*)&quot;)<br /> );

if(saveFileName.isEmpty())<br /> return;

QFile file&amp;amp;#40;saveFileName&amp;amp;#41;;<br /> if(!file.open(QIODevice::WriteOnly)) {<br /> QMessageBox::warning(<br /> this,<br /> tr(&quot;Error&amp;quot;),<br /> QString(tr(&quot;&lt;nobr&amp;gt;File '%1'&lt;br/&amp;gt;cannot be opened for writing.&lt;br/&amp;gt;&lt;br/&amp;gt;&quot;<br /> &quot;The log output could &lt;b&amp;gt;not&amp;lt;/b&amp;gt; be saved!&lt;/nobr&amp;gt;&quot;))<br /> .arg(saveFileName));<br /> return;<br /> }

QTextStream stream(&amp;file);<br /> stream &lt;&lt; browser-&gt;toPlainText();<br /> file.close();<br />}

void LogBrowserDialog::closeEvent(QCloseEvent *e)<br />{<br /> QMessageBox::StandardButton answer = QMessageBox::question(<br /> this,<br /> tr(&quot;Close Log Browser?&quot;),<br /> tr(&quot;Do you really want to close the log browser?&quot;),<br /> QMessageBox::Yes | QMessageBox::No<br /> );

if (answer == QMessageBox::Yes)<br /> e-&gt;accept();<br /> else<br /> e-&gt;ignore();<br />}

void LogBrowserDialog::keyPressEvent(QKeyEvent *e)<br />{<br /> // ignore all keyboard events<br /> // protects against accidentally closing of the dialog<br /> // without asking the user<br /> e-&gt;ignore();<br />}<br />

main.cpp

#include &lt;QApplication&amp;gt;<br />#include &lt;QPointer&amp;gt;<br />#include &lt;QDebug&amp;gt;<br />#include &quot;logbrowser.h&amp;quot;

QPointer&amp;lt;LogBrowser&amp;gt; logBrowser;

void myMessageOutput(QtMsgType type, const char *msg)<br />{<br /> if(logBrowser)<br /> logBrowser-&gt;outputMessage( type, msg );<br />}

int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);

logBrowser = new LogBrowser;<br /> qInstallMsgHandler(myMessageOutput);

qDebug() &lt;&lt; &quot;test for debug&amp;quot;;<br /> int result = a.exec&amp;amp;#40;&amp;#41;;<br /> qDebug() &lt;&lt; &quot;application exec return result =&quot; &lt;&lt; result;

delete logBrowser;<br /> return result;<br />}<br />