qDebug() 取消结尾自动换行
时间: 2023-07-04 15:05:22 浏览: 201
在 Qt 中,可以通过设置默认消息处理程序来取消 qDebug() 的结尾自动换行。具体做法如下:
```cpp
#include <QtDebug>
void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
QString txt;
switch (type) {
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
break;
case QtWarningMsg:
txt = QString("Warning: %1").arg(msg);
break;
case QtCriticalMsg:
txt = QString("Critical: %1").arg(msg);
break;
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
break;
}
// 取消结尾自动换行
QTextStream(stdout, QIODevice::WriteOnly) << txt << endl;
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(myMessageOutput);
// ...
return 0;
}
```
在上面的代码中,我们首先定义了一个新的消息处理程序 `myMessageOutput()`,然后通过 `qInstallMessageHandler()` 函数将其设置为默认消息处理程序。在 `myMessageOutput()` 函数中,我们对不同类型的消息进行了不同的处理,并使用 `QTextStream` 类将消息输出到控制台。注意,我们在输出消息之后并没有调用 `endl`,这样就可以取消结尾自动换行了。
阅读全文
相关推荐
![xml](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)