将 qDebug 重定向到发出的信号
Posted
技术标签:
【中文标题】将 qDebug 重定向到发出的信号【英文标题】:Redirect qDebug to an emitted signal 【发布时间】:2018-06-15 07:11:58 【问题描述】:我已成功将 qDebug()
输出重定向到 QTextEdit
小部件。出于几个原因,我希望每个 qDebug()
消息都包含在发出的信号中。一个原因是接收输出的对象不可用。另一个原因是我想根据当前活动的活动将输出重定向到不同的对象(使用信号的连接/断开连接到不同的插槽)。
我制作了一个工作示例代码,将qDebug
重定向到QTextEdit
小部件。有人可以帮我获取此代码以发出包含qDebug
消息的信号吗?
我不确定是否可以让Q_DebugStream
发出信号(我已经尝试过,但未能从中创建 Qt 类)。
在调用Q_DebugStream
时,必须可以传递指向函数/插槽的指针而不是指向QTextEdit
的指针,但我不确定这是如何完成的。
主窗口.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void logSignal(QString);
public slots:
void logSlot(QString);
private:
QTextEdit *logView;
;
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "q_debugstream.h"
#include <QGridLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
QWidget *mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
logView = new QTextEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(logView,0,0,1,1);
mainWidget->setLayout(mainLayout);
connect(this, SIGNAL(logSignal(QString)),
this, SLOT(logSlot(QString)));
emit logSignal("Message from a signal\n");
new Q_DebugStream(std::cout, logView); //Redirect Console output to QTextEdit
Q_DebugStream::registerQDebugMessageHandler(); //Redirect qDebug() output to QTextEdit
qDebug() << "DEBUG MODE ACTIVE";
MainWindow::~MainWindow()
void MainWindow::logSlot(QString log)
logView->append(log);
q_debugstream.h
//As per forum:
//http://www.qtforum.org/article/39768/redirecting-std-cout-std-cerf-qdebug-to-qtextedit.html
//A couple of lines added to ensure newlines go between each call.
//Thanks, James!
#ifndef Q_DEBUGSTREAM_H
#define Q_DEBUGSTREAM_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QTextEdit>
class Q_DebugStream : public std::basic_streambuf<char>
public:
Q_DebugStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream)
log_window = text_edit;
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
~Q_DebugStream()
m_stream.rdbuf(m_old_buf);
static void registerQDebugMessageHandler()
qInstallMessageHandler(myQDebugMessageHandler);
private:
static void myQDebugMessageHandler(QtMsgType, const QMessageLogContext &, const QString &msg)
std::cout << msg.toStdString().c_str();
protected:
//This is called when a std::endl has been inserted into the stream
virtual int_type overflow(int_type v)
if (v == '\n')
log_window->append("");
return v;
virtual std::streamsize xsputn(const char *p, std::streamsize n)
QString str(p);
if(str.contains("\n"))
QStringList strSplitted = str.split("\n");
log_window->moveCursor (QTextCursor::End);
log_window->insertPlainText (strSplitted.at(0)); //Index 0 is still on the same old line
for(int i = 1; i < strSplitted.size(); i++)
log_window->append(strSplitted.at(i));
log_window->append("\n");
else
log_window->moveCursor (QTextCursor::End);
log_window->insertPlainText (str);
log_window->insertPlainText ("\n");
return n;
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
QTextEdit* log_window;
;
#endif // Q_DEBUGSTREAM_H
当应用程序启动时,我在 QTextEdit 中收到两条消息:
"Message from a signal"
"DEBUG MODE ACTIVE"
【问题讨论】:
Redirecting std::cout from DLL in a separate thread to QTextEdit的可能重复 很高兴看到您自己解决了这个问题。但是,正确的做法是添加您自己的答案,而不是编辑问题。请将答案部分转移到下面的“您的答案”部分,并回滚对问题的更改。 (并且正确地做到这一点意味着您的问题和答案都有资格获得投票,因此这对您的声誉也有好处。 我已将您的编辑转换为您的答案 - 请检查我没有不小心误传任何内容。 【参考方案1】:(这个答案是从对问题的编辑中提取的 - 现在回滚了)。
我是这样解决这个问题的:
主窗口.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void logSignal(QString);
public slots:
void logSlot(QString);
private:
void dbgMsg(QString);
QTextEdit *logView;
;
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "q_debugstream.h"
#include <QGridLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
QWidget *mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
logView = new QTextEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(logView,0,0,1,1);
mainWidget->setLayout(mainLayout);
connect(this, SIGNAL(logSignal(QString)),
this, SLOT(logSlot(QString)));
emit logSignal("Now call Q_DebugStream");
//Redirect qDebug() output to dbgMsg(QString)
new Q_DebugStream(std::cout, this, &MainWindow::dbgMsg);
Q_DebugStream::registerQDebugMessageHandler();
qDebug() << "Debug message";
qWarning() << "Warning!";
qCritical() << "Critical issue!";
qInfo() << "Information";
qDebug() << "This\nis\na\nlong\none.";
MainWindow::~MainWindow()
void MainWindow::logSlot(QString log)
logView->append(log);
void MainWindow::dbgMsg(QString log)
emit logSignal(log);
q_debugstream.h
#ifndef Q_DEBUGSTREAM_H
#define Q_DEBUGSTREAM_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QString>
#include "mainwindow.h"
class Q_DebugStream : public std::basic_streambuf<char>
public:
Q_DebugStream(std::ostream &stream, MainWindow* obj, void (MainWindow::*dbgMsgPtr)(QString log)): m_stream(stream)
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
msgObj = obj;
msgHandler = dbgMsgPtr;
~Q_DebugStream()
m_stream.rdbuf(m_old_buf);
static void registerQDebugMessageHandler()
qInstallMessageHandler(myQDebugMessageHandler);
private:
static void myQDebugMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
QString message = msg;
switch (type)
case QtDebugMsg:
message.prepend("qDbg(): ");
break;
case QtWarningMsg:
message.prepend("qWarn(): ");
break;
case QtCriticalMsg:
message.prepend("qCrit(): ");
break;
case QtInfoMsg:
message.prepend("qInfo(): ");
break;
case QtFatalMsg:
message.prepend("qFatal(): ");
abort();
break;
message.append(" (" + QString::fromUtf8(context.file) + ")");
message.append(" line: " + QString::number(context.line));
std::cout << message.toStdString().c_str();
protected:
//This is called when a std::endl has been inserted into the stream
virtual int_type overflow(int_type v)
if (v == '\n')
(msgObj->*msgHandler)("\n");
return v;
virtual std::streamsize xsputn(const char *p, std::streamsize n)
QString str(p);
if(str.contains("\n"))
QStringList strSplitted = str.split("\n");
(msgObj->*msgHandler)(strSplitted.at(0)); //Index 0 is still on the same old line
for(int i = 1; i < strSplitted.size(); i++)
(msgObj->*msgHandler)("\\ " + strSplitted.at(i));
else
(msgObj->*msgHandler)(str);
return n;
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
MainWindow* msgObj;
void (MainWindow::*msgHandler)(QString);
;
#endif // Q_DEBUGSTREAM_H
应用程序启动时,我在 QTextEdit 中收到这些消息:
Now call Q_DebugStream
qDbg(): Debug message (..\qDebugFetch\mainwindow.cpp) line: 25
qWarn(): Warning! (..\qDebugFetch\mainwindow.cpp) line: 26
qCrit(): Critical issue! (..\qDebugFetch\mainwindow.cpp) line: 27
qInfo(): Information (..\qDebugFetch\mainwindow.cpp) line: 28
qDbg(): This
\ is
\ a
\ long
\ one. (..\qDebugFetch\mainwindow.cpp) line: 29
【讨论】:
以上是关于将 qDebug 重定向到发出的信号的主要内容,如果未能解决你的问题,请参考以下文章
使用 django-allauth 登录信号将用户重定向到另一个 url