This is my solution for a very simple error logging using QTextEdit.
// In some common header file
enum class ReportLevel {
Info,
Warning,
Error
};
// Signal in classes who report something
void reportStatus(ReportLevel level,
const QString& tag,
const QString& report);
// Slot in the class which receives the reports
void MyGreatClass::handleStatusReport(ReportLevel level,
const QString& tag,
const QString& report)
{
switch(level) {
case ReportLevel::Info:
mTeReports->setTextColor(Qt::blue);
break;
case ReportLevel::Warning:
mTeReports->setTextColor(QColor::fromRgb(255, 165, 0)); // Orange
break;
case ReportLevel::Error:
mTeReports->setTextColor(Qt::red);
break;
}
// mTeReoports is just an instance of QTextEdit
mTeReports->insertPlainText(tag + "\t");
mTeReports->setTextColor(Qt::black); // set color back to black
// might want ot use #ifdef for windows or linux....
mTeReports->insertPlainText(report + "\r\n");
// Force the scroll bar (if visible) to jump to bottom
mTeReports->ensureCursorVisible();
}
This is how it looks like:
Of course, you can go ahead and add date/time and other cool stuff :)