笔记:Qt开发之定制化qDebug()函数

本文介绍了如何通过qSetMessagePattern函数定制QApplication中QDebug等函数的输出,包括添加文件名、函数名和行号等信息,并说明了Release版本和.pro文件设置对日志的影响以及如何控制不同级别的日志输出。

目标:实现qDebug()函数的定制输出,包含文件名、函数名、行数等信息

1,通过qSetMessagePattern函数,实现定制化输出

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    // 改变缺省消息处理程序的输出,信息:线程:功能函数(行数):时间
    qSetMessagePattern("%{message} :id=%{threadid} %{function}(%{line}):%{time [hh:mm:ss zzz]}");

    // 打印信息
    qDebug("This is a debug message.");
    qInfo("This is a info message.");
    qWarning("This is a warning message.");
    qCritical("This is a critical message.");
    //qFatal("This is a fatal message.");

    ...
    return app.exec();
}

输出信息

This is a debug message. :id=815 main(217):[11:37:24 843]
This is a info message. :id=815 main(218):[11:37:24 843]
This is a warning message. :id=815 main(219):[11:37:24 844]
This is a critical message. :id=815 main(220):[11:37:24 844]
占位符          描述
在`MainWindow`的`mousePressEvent`函数里,左键按下时`qDebug`无法输出信息,可从以下几个方面排查并解决: ### 1. 函数定义与重写 要保证`mousePressEvent`函数在子类中正确定义与重写。此函数为虚函数,形参类型必须是`QMouseEvent *`,并且在函数定义时要加上`override`。 示例代码如下: ```cpp // mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMouseEvent> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); protected: void mousePressEvent(QMouseEvent *event) override; }; #endif // MAINWINDOW_H // mainwindow.cpp #include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } MainWindow::~MainWindow() { } void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 2. 事件传递 要保证事件能正确传递到`mousePressEvent`函数。若有事件过滤器或者其他事件处理机制,可能会拦截鼠标事件。 示例代码如下: ```cpp // mainwindow.cpp bool MainWindow::eventFilter(QObject *object, QEvent *event) { if (object == this->ui->pushButton) { // 若此处拦截了鼠标事件,可能导致mousePressEvent无法接收到 return false; // 不拦截事件,让事件继续传递 } return QMainWindow::eventFilter(object, event); } ``` ### 3. 调试输出 确保调试输出功能正常。可在`mousePressEvent`函数开头添加一些调试信息,查看函数是否被调用。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { qDebug() << "mousePressEvent被调用"; if (event->button() == Qt::LeftButton) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 4. 检查编译与运行环境 要保证编译与运行环境正常,没有错误或者警告信息。可尝试清理项目并重新编译。 ### 5. 检查日志输出设置 要保证日志输出设置正确,`qDebug`输出的信息能在调试窗口或者日志文件中显示。 ### 6. 检查鼠标设备 确保鼠标设备正常工作,左键点击能被系统正确识别。 ### 7. 检查父类的事件处理 若父类对鼠标事件有特殊处理,可能会影响子类的`mousePressEvent`函数。可查看父类的`mousePressEvent`函数实现,确保事件能正确传递。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "左键"; } // 调用父类的mousePressEvent函数 QMainWindow::mousePressEvent(event); } ``` ### 8. 检查信号与槽连接 确保没有其他信号与槽连接干扰`mousePressEvent`函数的正常工作。 示例代码如下: ```cpp // mainwindow.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // 检查此处的信号与槽连接是否会干扰mousePressEvent connect(this->ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "btn clicked!"; }); this->ui->pushButton->installEventFilter(this); } ``` ### 9. 检查事件过滤器 确保事件过滤器没有拦截鼠标事件。 示例代码如下: ```cpp // mainwindow.cpp bool MainWindow::eventFilter(QObject *object, QEvent *event) { if (object == this->ui->pushButton) { // 若此处拦截了鼠标事件,可能导致mousePressEvent无法接收到 return false; // 不拦截事件,让事件继续传递 } return QMainWindow::eventFilter(object, event); } ``` ### 10. 检查鼠标事件类型 确保处理的是正确的鼠标事件类型,如`QEvent::MouseButtonPress`。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->type() == QEvent::MouseButtonPress && event->button() == Qt::LeftButton) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 11. 检查鼠标事件坐标 确保鼠标事件坐标在`MainWindow`的有效范围内。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && this->rect().contains(event->pos())) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 12. 检查鼠标事件状态 确保鼠标事件状态正常,没有被其他操作干扰。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && event->buttons() & Qt::LeftButton) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 13. 检查鼠标事件优先级 确保鼠标事件优先级正确,没有被其他事件覆盖。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "左键"; event->accept(); // 接受事件,防止事件继续传递 } else { QMainWindow::mousePressEvent(event); } } ``` ### 14. 检查鼠标事件响应区域 确保鼠标事件响应区域正确,没有被其他控件覆盖。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && this->ui->centralWidget()->rect().contains(event->pos())) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 15. 检查鼠标事件处理顺序 确保鼠标事件处理顺序正确,没有被其他事件处理函数干扰。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "左键"; } // 确保在合适的位置调用父类的mousePressEvent函数 QMainWindow::mousePressEvent(event); } ``` ### 16. 检查鼠标事件的激活状态 确保鼠标事件的激活状态正确,没有被禁用。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (this->isEnabled() && event->button() == Qt::LeftButton) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 17. 检查鼠标事件的捕获状态 确保鼠标事件的捕获状态正确,没有被其他控件捕获。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && !this->ui->pushButton->underMouse()) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 18. 检查鼠标事件的焦点状态 确保鼠标事件的焦点状态正确,没有被其他控件获取焦点。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && this->hasFocus()) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 19. 检查鼠标事件的模态状态 确保鼠标事件的模态状态正确,没有被模态对话框或者其他模态窗口影响。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && !this->isModal()) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` ### 20. 检查鼠标事件的全局状态 确保鼠标事件的全局状态正确,没有被系统级别的设置或者其他应用程序影响。 示例代码如下: ```cpp void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && QGuiApplication::mouseButtons() & Qt::LeftButton) { qDebug() << "左键"; } QMainWindow::mousePressEvent(event); } ``` 通过以上步骤,可以逐步排查并解决`MainWindow`的`mousePressEvent`函数中左键按下`qDebug`不输出信息的问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值