如何重载运算符<<用于 qDebug

发布于 2024-08-30 03:12:05 字数 647 浏览 4 评论 0 原文

我正在尝试为存储数据的类创建更有用的调试消息。我的代码看起来像这样,

#include <QAbstractTableModel>
#include <QDebug>

/**
  * Model for storing data. 
  */
class DataModel : public QAbstractTableModel {
    // for debugging purposes
    friend QDebug operator<< (QDebug d, const DataModel &model);

    //other stuff
};

/**
  * Overloading operator for debugging purposes
  */
QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}

我希望 qDebug() << model 将打印“Hello world!”。但是,输出中始终存在类似“QAbstractTableModel(0x1c7e520)”的内容。

你知道出了什么问题吗?

I'm trying to create more useful debug messages for my class where store data. My code is looking something like this

#include <QAbstractTableModel>
#include <QDebug>

/**
  * Model for storing data. 
  */
class DataModel : public QAbstractTableModel {
    // for debugging purposes
    friend QDebug operator<< (QDebug d, const DataModel &model);

    //other stuff
};

/**
  * Overloading operator for debugging purposes
  */
QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}

I expect qDebug() << model will print "Hello world!". However, there is alway something like "QAbstractTableModel(0x1c7e520)" on the output.

Do you have any idea what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-09-06 03:12:05

我知道它很长时间了,但只是为了记录并帮助最终来到这里有同样疑问的任何其他人,获得 qDebug() << 的最简单方法使用您自己的类打印“Hello World”或其他任何内容,就是实现您的类到可打印类型的隐式转换,如 QString(QDebug 很好地支持)。

class Foo {
public:
   Foo() { }
   operator QString() const { return <put your QString here>; }   

};

I know it long time now, but just to be documented and to help any other people who eventually come here having the same doubt, the easiest way to get qDebug() << working with your own class printing something like "Hello World" or whatever else, is to implement implicit conversion of your class to a printable type, like QString (which is well supported by QDebug).

class Foo {
public:
   Foo() { }
   operator QString() const { return <put your QString here>; }   

};
小…红帽 2024-09-06 03:12:05

在您的示例中, qDebug() 打印变量的地址,这是未知类型的默认行为。

事实上,似乎有两件事你必须注意:

  • 按值获取项目(eugen 已经指出了!)。
  • 在使用之前定义重载运算符,将其签名放在头文件中,或者在使用之前将其定义为forward(否则您将得到默认的“qDebug() <<”行为)。

这会给你:

QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}
DataModel m;
qDebug() << "m" << m;

或者

QDebug operator<< (QDebug d, const DataModel &model);

DataModel m;
qDebug() << "m" << m;

QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}

我也通过艰难的方式学会了它......

In your example, qDebug() prints the address of your variable, which is the default behavior for unknown types.

In fact, there seem to be two things you have to take care of:

  • Get the item by value (and eugen already pointed it out!).
  • Define the overloading operator before you use it, put its signature in the header file, or define it as forward before using it (otherwise you will get the default "qDebug() <<" behavior).

This will give you:

QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}
DataModel m;
qDebug() << "m" << m;

or

QDebug operator<< (QDebug d, const DataModel &model);

DataModel m;
qDebug() << "m" << m;

QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}

I've learned it the hard way, too...

一笔一画续写前缘 2024-09-06 03:12:05

经过一个小时的思考这个问题后,我发现 model 是指向 DataModel 的指针,而我的运算符 << 仅接受引用。

After an hour of playing with this question I figured out model is pointer to DataModel and my operator << takes only references.

°如果伤别离去 2024-09-06 03:12:05

您仅实现了 <<运营商供参考。如果您的 model 变量是指针,它将使用另一个实现(不是您的)。

要使用您的实现,您可以执行以下操作:

qDebug() << *model

顺便说一下,实现 QDebug 运算符<<(QDebug dbg, const T &data) 重载的正确方法是使用 QDebugStateSaver< /code> class:

QDebug operator<<(QDebug dbg, const QDataflowModelOutlet &outlet)
{
    QDebugStateSaver stateSaver(dbg);
    dbg.nospace() << ...;
    return dbg;
}

这样,退出该功能时,设置(即是否在打印之间插入空格)将被正确恢复。

You implemented only the << operator for a reference. If your model variable is a pointer, it will use another implementation (not yours).

To use your implementation you can do:

qDebug() << *model

By the way, the correct way to implement a QDebug operator<<(QDebug dbg, const T &data) overload is to use the QDebugStateSaver class:

QDebug operator<<(QDebug dbg, const QDataflowModelOutlet &outlet)
{
    QDebugStateSaver stateSaver(dbg);
    dbg.nospace() << ...;
    return dbg;
}

In this way the settings (i.e. wether to insert or not spaces between prints) will be correctly restored when exiting the function.

桃扇骨 2024-09-06 03:12:05

我发现 这个答案在 QT 论坛上,作者:raven-worx(在应得的地方给予奖励!)

.h 文件中

QDebug operator<<(QDebug dbg, const MyType &type);

: code>MyType 是您的类,就像 DataModel 一样,而 type 是您将显示的实例。

而在.cpp文件中:

QDebug operator<<(QDebug dbg, const MyType &type)
{
    dbg.nospace() << "MyType(" << .... << ")";
    return dbg.maybeSpace();
}

并且可以使用QDebug的space()nospace()等方法来控制确切的显示流的。

因此对于OP,我们将使用:

// in the .h file:
class DataModel : public QAbstractTableModel {
// stuff
};
QDebug operator<<(QDebug dbg, const DataModel &data);

// in the .cpp file:
QDebug operator<<(QDebug dbg, const DataModel &data)
{
    dbg.nospace() << "My data {" << data.someField << ',' << data.another << "}";
    return dbg.maybeSpace();
}

// in some .cpp user of the class:
DataModel myData;

. . .

QDebug() << "The current value of myData is" << myData;

I found this answer on the QT Forum by raven-worx (giving credit where credit is due!)

In the .h file:

QDebug operator<<(QDebug dbg, const MyType &type);

where MyType is your class, like DataModel and type is the instance you will display.

And in the .cpp file:

QDebug operator<<(QDebug dbg, const MyType &type)
{
    dbg.nospace() << "MyType(" << .... << ")";
    return dbg.maybeSpace();
}

and you can use the QDebug's space(), nospace(), and other methods to control the exact display of the stream.

So for the OP, we would use:

// in the .h file:
class DataModel : public QAbstractTableModel {
// stuff
};
QDebug operator<<(QDebug dbg, const DataModel &data);

// in the .cpp file:
QDebug operator<<(QDebug dbg, const DataModel &data)
{
    dbg.nospace() << "My data {" << data.someField << ',' << data.another << "}";
    return dbg.maybeSpace();
}

// in some .cpp user of the class:
DataModel myData;

. . .

QDebug() << "The current value of myData is" << myData;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文