• 5786阅读
  • 3回复

qDebug为何这样输出数据? [复制链接]

上一主题 下一主题
离线sakiola
 
只看楼主 正序阅读 楼主  发表于: 2009-08-14
QList <QString> list;
{
  QString str("This is a test string");
  list < < str;
}
  qDebug() < < list[0] < < "How are you";


请问上述代码为什么输出结果是:

"This is a test string" How are you

呢?

1. "This is a test string" ----引号哪来的?

3. [0]去掉只对list输出的时候 输出结果变成:

("This is a test string") How are you ----------括号哪来的?

达人帮解释解释哈~
[ 此帖被sakiola在2009-08-14 09:40重新编辑 ]
NB才是王道
离线dbzhang800

只看该作者 3楼 发表于: 2009-08-14
还有这个

#if defined(FORCE_UREF)
template <class T>
inline QDebug &operator<<(QDebug debug, const QList<T> &list)
#else
template <class T>
inline QDebug operator<<(QDebug debug, const QList<T> &list)
#endif
{
    debug.nospace() << "(";
    for (Q_TYPENAME QList<T>::size_type i = 0; i < list.count(); ++i) {
        if (i)
            debug << ", ";
        debug << list.at(i);
    }
    debug << ")";
    return debug.space();
}
离线dbzhang800

只看该作者 2楼 发表于: 2009-08-14
没有多少为什么,因为QDebug 重载的 << 就是这么实现的。  

感兴趣的话,请自行翻看代码 QTDIR/src/corelib/io/qdebug.h

下面贴出其中的一行

    inline QDebug &operator<<(const QString & t) { stream->ts << "\"" << t  << "\""; return maybeSpace(); }
离线sakiola
只看该作者 1楼 发表于: 2009-08-14
顶起
NB才是王道
快速回复
限100 字节
 
上一个 下一个