How to overload operator<< for qDebug

本秂侑毒 提交于 2019-12-12 09:29:33

问题


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?


回答1:


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>; }   

};



回答2:


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




回答3:


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...




回答4:


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;



回答5:


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.



来源:https://stackoverflow.com/questions/2677577/how-to-overload-operator-for-qdebug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!