gpt4 book ai didi

c++ - 将 qDebug 重定向到 QTextEdit

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:10 28 4
gpt4 key购买 nike

我想使用 qInstallMessageHandler(handler)qDebug 重定向到 QTextEdit

我在类中定义了一个处理函数:

void Spider::redirect(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
console->append(msg);
}

并在类 (Spider) 的构造函数中调用 qInstallMessageHandler(redirect)

但是,当我编译这个程序时,我得到了一个错误:

cannot convert 'Spider::redirect' from type 'void (Spider::)(QtMsgType, const QMessageLogContext&, const QString&)' to type 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}'

如果我在全局定义处理函数,没问题。

我无法弄清楚这两种行为之间的区别。

最佳答案

我真的很喜欢这种调试能力。我在最近的几个项目中做过几次。以下是相关的代码片段。

在 mainwindow.h 中,在 MainWindow 类中,在 public

static QTextEdit * s_textEdit;

在 mainwindow.cpp 中,在任何函数之外

QTextEdit * MainWindow::s_textEdit = 0;

在主窗口构造函数中

s_textEdit = new QTextEdit;
// be sure to add the text edit into the GUI somewhere,
// like in a layout or on a tab widget, or in a dock widget

在 main.cpp 中,在 main() 之上

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if(MainWindow::s_textEdit == 0)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
abort();
}
}
else
{
switch (type) {
case QtDebugMsg:
case QtWarningMsg:
case QtCriticalMsg:
// redundant check, could be removed, or the
// upper if statement could be removed
if(MainWindow::s_textEdit != 0)
MainWindow::s_textEdit->append(msg);
break;
case QtFatalMsg:
abort();
}
}
}

在 main.cpp 的 main() 内部,在初始化 QApplication 实例之前。

qInstallMessageHandler(myMessageOutput);

注意:这对任何单线程应用程序都非常有效。一旦开始在 GUI 线程外使用 qDebug(),就会崩溃。然后,您需要从任何线程函数(任何未在您的 GUI 线程上运行的函数)创建一个 QueuedConnection,以连接到您的 MainWindow::s_textEdit 实例,如下所示:

QObject::connect(otherThread, SIGNAL(debug(QString)),
s_textEdit, SLOT(append(QString)), Qt::QueuedConnection);

如果您最终使用 QDockWidget 并使用 QMenu,您还可以做一些很酷的事情。最终结果是一个非常用户友好、易于管理的控制台窗口。

QMenu * menu;
menu = this->menuBar()->addMenu("About");
menu->setObjectName(menu->title());

// later on...

QDockWidget *dock;
dock = new QDockWidget("Console", this);
dock->setObjectName(dock->windowTitle());
dock->setWidget(s_textEdit);
s_textEdit->setReadOnly(true);
this->addDockWidget(Qt::RightDockWidgetArea, dock);
this->findChild<QMenu*>("About")->addAction(dock->toggleViewAction());

希望对您有所帮助。

关于c++ - 将 qDebug 重定向到 QTextEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485208/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com