C++ (Qt)#include "mainwindow.h" #include <QPlainTextEdit>#include <QDebug>#include <QScrollBar>#include <QApplication>#include <QTranslator> static QtMsgHandler oldMsgHandler = 0; static void logMsgHandler(QtMsgType type, const char *msg){ MainWindow::instance()->log(msg); oldMsgHandler(type, msg);} MainWindow *MainWindow::self = 0; MainWindow *MainWindow::instance(){ if (!MainWindow::self) { MainWindow::self = new MainWindow(); } return MainWindow::self;} MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , logView(new QPlainTextEdit(this)){ self = this; setCentralWidget(logView); logView->setReadOnly(true); logView->setMaximumBlockCount(1000); oldMsgHandler = qInstallMsgHandler(logMsgHandler); QTranslator *translator = new QTranslator(this); if (translator->load(QString(":/translations/test_%2.qm").arg(QLocale::system().name()))) qApp->installTranslator(translator); else delete translator; QString text = tr("Test from qDebug"); qDebug() << text.toLocal8Bit().constData(); text = tr("Test from code"); log(text);} MainWindow::~MainWindow(){ qInstallMsgHandler(oldMsgHandler);} void MainWindow::log(const QString &msg){ logView->appendPlainText(msg); logView->verticalScrollBar()->setValue(logView->verticalScrollBar()->maximum());}
qDebug() << text.toLocal8Bit().constData();
defaultLocaleCodec = QTextCodec::codecForName("IBM 866"); QTextCodec::setCodecForLocale(defaultLocaleCodec); qDebug() << text;