关闭qdebug,如何在Visual Studio中查看qDebug()?

In Qt Creator, I could view the qDebug(), qWarning() etc. output directly in the IDE. How could I do this in Visual Studio?

解决方案

When you run the program with an attached debugger, it will show in the Output Window of Visual Studio, but for Debug purposes i often redirect the debug output to some kind of nice log window, which you can do by using the function qInstallMsgHandler:

the code that i use:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

QWidget *DEBUG_MESSAGE_DISPLAY_WIDGET = NULL;

QPlainTextEdit *DEBUG_MESSAGE_DISPLAY_TEXTEDIT = NULL;

void setupDebugDisplay();

void debugMessageDisplayFunc(QtMsgType type, const char *msg);

int main( int argc, char* argv[] )

{

QApplication a( argc, argv );

a.setQuitOnLastWindowClosed( true );

setupDebugDisplay();

// your code here.... e.g:

// YourMainWindow mainWindow;

int ret = a.exec();

delete DEBUG_MESSAGE_DISPLAY_WIDGET;

return ret;

}

void setupDebugDisplay()

{

QWidget *widget = new QWidget();

widget->setWindowTitle( "Debug Log" );

widget->setAttribute( Qt::WA_QuitOnClose, false ); //quit only when mainwindow is closed

QBoxLayout* layout = new QVBoxLayout();

widget->setLayout( layout );

QPlainTextEdit *textEdit = new QPlainTextEdit( widget );

QFont font = QFont( "Monospace" );

font.setStyleHint(QFont::TypeWriter);

textEdit->setFont( font );

textEdit->setReadOnly(true);

layout->addWidget( textEdit );

widget->show();

DEBUG_MESSAGE_DISPLAY_WIDGET = widget;

DEBUG_MESSAGE_DISPLAY_TEXTEDIT = textEdit;

qInstallMsgHandler(debugMessageDisplayFunc);

}

void debugMessageDisplayFunc(QtMsgType type, const char *msg)

{

bool do_abort = false;

const char* msgTypeStr = NULL;

switch (type) {

case QtDebugMsg:

msgTypeStr = "Debug";

break;

case QtWarningMsg:

msgTypeStr = "Warning";

break;

case QtCriticalMsg:

msgTypeStr = "Critical";

break;

case QtFatalMsg:

msgTypeStr = "Fatal";

do_abort = true;

default:

assert(0);

return;

}

QTime now = QTime::currentTime();

QString formattedMessage =

QString::fromLatin1("%1 %2 %3")

.arg(now.toString("hh:mm:ss:zzz"))

.arg(msgTypeStr).arg(msg);

// print on console:

fprintf( stderr, "%s\n", formattedMessage.toLocal8Bit().constData() );

// print in debug log window

{

bool isMainThread = QThread::currentThread() == QApplication::instance()->thread();

if(DEBUG_MESSAGE_DISPLAY_TEXTEDIT)

{

if( isMainThread )

DEBUG_MESSAGE_DISPLAY_TEXTEDIT->appendPlainText( formattedMessage );

else // additional code, so that qDebug calls in threads will work aswell

QMetaObject::invokeMethod( DEBUG_MESSAGE_DISPLAY_TEXTEDIT, "appendPlainText", Qt::QueuedConnection, Q_ARG( QString, formattedMessage ) );

}

}

}

### 解决 Visual Studio 2022 中文显示乱码的方法 #### 设置源文件编码格式 为了防止编译器误读源文件的编码,可以在项目的源文件顶部加入预处理指令来指定执行字符集。对于 UTF-8 编码的支持尤为重要,因为这是国际化的标准编码方式之一。 ```cpp #pragma execution_character_set("utf-8") ``` 这条语句确保了程序内部使用的字符串是以 UTF-8 形式解释的[^3]。 #### 修改项目属性配置 进入Visual Studio 的项目设置界面,在“配置属性”-> “常规”选项卡里找到“字符集”,将其更改为`使用 Unicode 字符集 (Unicode)`而不是多字节字符集(MBCS),这有助于统一整个工程内的文字编码规则[^1]。 #### 调整控制台输出环境变量 如果遇到命令行工具或调试窗口内出现中文乱码的情况,则可能是因为 Windows 控制台默认采用的是 OEM 码页而非 ANSI 或者 Unicode。可以通过修改系统的区域和语言设置或者临时改变当前会话的活动代码页为支持中文的版本(比如 GBK),具体操作是在启动应用程序之前通过 `chcp 936` 命令切换到合适的页面[^4]。 #### 安装必要的字体和支持包 确认安装了能够正确渲染汉字所需的 TrueType 字体以及任何其他依赖项;另外,某些情况下还需要额外安装 Microsoft 提供的语言接口包(LIPs)或者其他形式的文化特性补充材料以增强软件对本地化内容的理解能力。 #### 使用 Qt 框架时特别注意的地方 当在基于Qt的应用开发过程中遭遇中文乱码难题时,除了上述措施外还需考虑框架本身的行为模式。例如,在定义QString对象并赋值给定的文字串前加上QLatin1String转换函数可以有效改善部分场景下的表现: ```cpp QString str = QLatin1String("百香果真是一只可爱的小猫咪"); qDebug()<<str<<Qt::endl; ``` 此外,也可以尝试调整 qmake 配置文件(.pro 文件)里的相关参数,像这样添加一行告诉构建系统应该怎样对待资源文件中的文本数据: ```plaintext QMAKE_CXXFLAGS += -finput-charset=UTF-8 ``` 这样做可以让 Qt Creator 和 MinGW 工具链更好地协作处理非ASCII字符[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值