├── main.cpp ├── point.h ├── point.cpp ├── qpoint.h ├── qpoint.cpp └── qdebug-overload-example.pro /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "point.h" 5 | #include "qpoint.h" 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | QCoreApplication a(argc, argv); 10 | 11 | Point point(10,20); 12 | qDebug() << point; 13 | 14 | QPoint qpoint(15,30); 15 | qDebug() << qpoint; 16 | 17 | a.exit(); 18 | } 19 | -------------------------------------------------------------------------------- /point.h: -------------------------------------------------------------------------------- 1 | #ifndef POINT_H 2 | #define POINT_H 3 | 4 | #include 5 | #include 6 | 7 | class Point 8 | { 9 | public: 10 | Point(const int &x, const int &y); 11 | 12 | int x() const; 13 | int y() const; 14 | 15 | private: 16 | int _x; 17 | int _y; 18 | }; 19 | 20 | QDebug operator<<(QDebug dbg, const Point &point); 21 | 22 | #endif // POINT_H 23 | -------------------------------------------------------------------------------- /point.cpp: -------------------------------------------------------------------------------- 1 | #include "point.h" 2 | 3 | Point::Point(const int &x, const int &y) 4 | { 5 | _x = x; 6 | _y = y; 7 | } 8 | 9 | int Point::x() const 10 | { 11 | return _x; 12 | } 13 | 14 | int Point::y() const 15 | { 16 | return _y; 17 | } 18 | 19 | QDebug operator <<(QDebug dbg, const Point &point) 20 | { 21 | dbg.nospace() << "Point(" << point.x() << "," << point.y() << ")"; 22 | return dbg.maybeSpace(); 23 | } 24 | -------------------------------------------------------------------------------- /qpoint.h: -------------------------------------------------------------------------------- 1 | #ifndef QPOINT_H 2 | #define QPOINT_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class QPoint : public QObject 9 | { 10 | Q_OBJECT 11 | public: 12 | explicit QPoint(const int &x, const int &y, QObject *parent = 0); 13 | 14 | int x() const; 15 | int y() const; 16 | 17 | private: 18 | int _x; 19 | int _y; 20 | }; 21 | 22 | QDebug operator<<(QDebug dbg, const QPoint &point); 23 | 24 | #endif // QPOINT_H 25 | -------------------------------------------------------------------------------- /qpoint.cpp: -------------------------------------------------------------------------------- 1 | #include "qpoint.h" 2 | 3 | QPoint::QPoint(const int &x, const int &y, QObject *parent) : 4 | QObject(parent) 5 | { 6 | _x = x; 7 | _y = y; 8 | } 9 | 10 | int QPoint::x() const 11 | { 12 | return _x; 13 | } 14 | 15 | int QPoint::y() const 16 | { 17 | return _y; 18 | } 19 | 20 | QDebug operator <<(QDebug dbg, const QPoint &point) 21 | { 22 | dbg.nospace() << "QPoint(" << point.x() << "," << point.y() << ")"; 23 | return dbg.maybeSpace(); 24 | } 25 | -------------------------------------------------------------------------------- /qdebug-overload-example.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2015-01-02T10:36:16 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core 8 | 9 | QT -= gui 10 | 11 | TARGET = qdebug-overload-example 12 | CONFIG += console 13 | CONFIG -= app_bundle 14 | 15 | TEMPLATE = app 16 | 17 | 18 | SOURCES += main.cpp \ 19 | point.cpp \ 20 | qpoint.cpp 21 | 22 | !host_build:QMAKE_MAC_SDK = macosx10.9 23 | 24 | HEADERS += \ 25 | point.h \ 26 | qpoint.h 27 | --------------------------------------------------------------------------------