Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Overriding QDebug& operator<<
Forum Updated to NodeBB v4.3 + New Features

Overriding QDebug& operator<<

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    u2gilles
    wrote on last edited by
    #1

    Hi all,
    In Qt 5.1 , I have problems overriding QDebug& operator<<
    The following program works fine

    @
    #include <QtCore>

    class CData1 {
    public:
    CData1(int i=6,double d=3.14,QString s="Ni Hao"):i1(i),d1(d),s1(s) {}
    int i1;
    double d1;
    QString s1;
    friend QDebug& operator<< (QDebug&,const CData1& );
    };

    QDebug& operator<<(QDebug& dbg, const CData1& d){
    dbg.nospace() << "[" << d.i1 << ";"<< d.d1 << ";" << d.s1 << "]" ;
    return dbg.space();
    }

    int main() {
    CData1 d1;
    qDebug() << "" << d1;
    }

    @

    … and displays in the console : [6;3.14;”Ni Hao”]

    But in main() , when I replace

    @
    qDebug() << "" << d1;
    @

    by
    @
    qDebug() << d1;
    @

    I have the following compilation error :
    @
    D:\Source\Cpp\qt5\tpQDebug1\main.cpp:20: error: no match for 'operator<<' (operand types are 'QDebug' and 'CData1')
    @

    Thanks in advance for your help or comments

    Gilles

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Not references to QDebug
      @
      QDebug operator<<(QDebug dbg, const CData1& d){
      ...
      }
      @

      1 Reply Last reply
      0
      • U Offline
        U Offline
        u2gilles
        wrote on last edited by
        #3

        QDebug is already included in <QtCore>
        and the version with :
        @
        qDebug() << "" << d1;
        @

        compiles and works.

        For me, it looks like a Qt bug....

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Have a "look to this post":http://qt-project.org/forums/viewthread/18973/

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • U Offline
            U Offline
            u2gilles
            wrote on last edited by
            #5

            I got it this time. ChrisW67 and koahnig, thanks a million.

            I have another question if you don't mind . How would you suggest to get rid of the LF in this case?

            I know that the LF or CR is added when the QDebug object is destroyed.

            Using a message handler does not seem to work as the qInstallMessageHandler function accepts only function with the following signature :
            @
            msgHandler1(QtMsgType , const QMessageLogContext &, const QString &)
            @

            So I can't pass my type CData1 to the message handler. I can't pass only QString.

            Is there any solution to display CData1 without LF using qDebug() ?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              There is "another post on this":http://qt-project.org/forums/viewthread/21334/ Respectively, it has been added also as "doc note to QDebug":http://qt-project.org/doc/qt-5.0/qtcore/qdebug.html . Check the very last entry. If I understand correctly your question, this might solve your problem.

              Otherwise, the message handler is general and not restricted to QString output. You can use the handler to redirect your output to anywhere. QDebug does the conversion of all your output to QString.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dbzhang800
                wrote on last edited by
                #7

                Hi, this is a pure C++ problem, and the real reason is that:

                bq. A temporary cannot be bound to a non-const reference.

                You can try to play with following code:

                @
                class CData1 {
                public:
                CData1(int i=6,double d=3.14,QString s="Ni Hao"):i1(i),d1(d),s1(s) {}
                int i1;
                double d1;
                QString s1;

                CData1 &ref() {return *this;}
                

                };

                void test(CData1 &)
                {
                }

                int main()
                {
                test(CData1().ref()); //Compile successful
                //test(CData1()); //Can not be compiled.
                return 0;
                }
                @

                And what you confused is following lines

                @
                operator <<(QDebug(QtDebugMsg).operator <<(""), d1); //Compile successful
                operator <<(QDebug(QtDebugMsg), d1); //Can not be compiled.
                @

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  u2gilles
                  wrote on last edited by
                  #8

                  Thanks koahnig, I almost got the big picture now.

                  My tests just showed that the message handler is called only when the QDebug instance is suppressed.
                  Is there a kind of "flush" to display data with the message handler before the suppression of QDebug. With this last feature, all my needs would be met I guess.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    QTextStream is used inside of QDebug. When QDebug destroyed, the string contained in this QTextStream will be output. That's why qDebug() return a temporary object.

                    While std::cout is a Global stream object, so flush is needed.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved