Qt学习笔记(五)重定向

本文介绍了如何在Qt应用中实现实时捕获程序输出信息,并将其显示到QTextBrowser组件上,主要涉及main文件的配置修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实时获取程序中qt所输出的信息,并显示到QTextBrowser上。

1.在main文件中添加以下内容

//xx为你的界面类名

xx *clk = NULL;
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
	// 加锁
	QString message;
	switch (type)
	{
	case QtDebugMsg:
		message = QString("Debug:");
		break;
	case QtWarningMsg:
		message = QString("Warning:");
		break;
	case QtCriticalMsg:
		message = QString("Critical:");
		break;
	case QtFatalMsg:
		message = QString("Fatal:");
		break;
	}

	// 设置输出信息格式
	//QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
	//QString strMessage = QString::fromLocal8Bit("%1").arg(localMsg.constData());
	message.append(msg);
	clk->showMsg(message);//通过调用showMsg()来显示输出内容
	QFile file("log.txt");//输出到log.txt文件中
	file.open(QIODevice::ReadWrite | QIODevice::Append);
	QTextStream stream(&file);
	stream << message << "\r\n";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值