Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为QMaps格式化qDebug的输出_C++_Qt_Formatting - Fatal编程技术网

C++ 为QMaps格式化qDebug的输出

C++ 为QMaps格式化qDebug的输出,c++,qt,formatting,C++,Qt,Formatting,我目前正在维护一个遗留应用程序。这有很多结构,如: QMap<QString, QMap<QString, QMap<QString, QMap<QString, QVariant> > > > Dep; 多亏了四维结构是出了名的难以想象。 但是一些小的环呢 typedef QMap<QString, QVariant> T1; typedef QMap<QString, T1> T2; typedef QMap<Q

我目前正在维护一个遗留应用程序。这有很多结构,如:

QMap<QString, QMap<QString, QMap<QString, QMap<QString, QVariant> > > > Dep;

多亏了

四维结构是出了名的难以想象。 但是一些小的环呢

typedef QMap<QString, QVariant> T1;
typedef QMap<QString, T1> T2;
typedef QMap<QString, T2> T3;

foreach( T3 i, dep ) {
    cout << "******" << i.key() << "*******" << endl << endl;
    foreach ( T2 j, i.value() ) {
        cout << j.key() << ":" << endl;
        foreach ( T3 k, j.value() ) {
            cout << k.key() << "= ";
            foreach ( QVariant l, k.value() ) {
                cout << l.key() << ": " << l.value() << " ";
            }
            cout << endl;
        }
    }
}

当然,使用名称空间std。根据需要添加setw。希望您能理解。

这是用于n维的,将使用标准qDebug输出用于已知类型:

template<class NonMap>
struct Print
{
    static void print(const QString& tabs, const NonMap& value) 
    {
        qDebug() << tabs << value;
    }
};

template <class Key, class ValueType >
struct Print<class QMap<Key, ValueType> >
{
    static void print(const QString& tabs, const QMap< Key, ValueType>& map )
    {
        const QString extraTab = tabs + "\t";
        QMapIterator<Key, ValueType> iterator(map);
        while(iterator.hasNext())
        {
            iterator.next();
            qDebug() << tabs << iterator.key(); 
            Print<ValueType>::print(extraTab, iterator.value());
        }
    }
};

template<class Type>
void printMe(const Type& type )
{
    Print<Type>::print("", type);
};

模板和递归的出色使用:
template<class NonMap>
struct Print
{
    static void print(const QString& tabs, const NonMap& value) 
    {
        qDebug() << tabs << value;
    }
};

template <class Key, class ValueType >
struct Print<class QMap<Key, ValueType> >
{
    static void print(const QString& tabs, const QMap< Key, ValueType>& map )
    {
        const QString extraTab = tabs + "\t";
        QMapIterator<Key, ValueType> iterator(map);
        while(iterator.hasNext())
        {
            iterator.next();
            qDebug() << tabs << iterator.key(); 
            Print<ValueType>::print(extraTab, iterator.value());
        }
    }
};

template<class Type>
void printMe(const Type& type )
{
    Print<Type>::print("", type);
};