1. 在头文件中添加Q_DECLARE_LOGGING_CATEGORY(USER_CAT)
#ifndef _USER_H_
#define _USER_H_
#include <QObject>
#include <QLoggingCategory>
class User : public QObject
{
Q_OBJECT
public:
User(const QString &name = QString());
QString name() const;
private:
QString m_name;
};
Q_DECLARE_LOGGING_CATEGORY(USER_CAT)
#endif //_USER_H_
2. 在源文件中添加Q_LOGGING_CATEGORY
#include "user.h"
Q_LOGGING_CATEGORY(USER_CAT, "user", QtCriticalMsg)
User::User(const QString &name)
: m_name(name)
{
}
QString User::name() const
{
return m_name;
}
3. 使用qCDebug()输入调试信息
#include <QApplication>
#include "user.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
User user("GuanYu");
qCDebug(USER_CAT)<<user.name();
return app.exec();;
}
4. 在deepin系统中打开输出相关的设置
export QT_LOGGING_RULES="user=true"
5. 执行程序,即可看到相关输出