格式化qDebug的输出以用于QMaps

dat*_*ata 3 c++ formatting qt

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

QMap<QString, QMap<QString, QMap<QString, QMap<QString, QVariant> > > > Dep;
Run Code Online (Sandbox Code Playgroud)

由于界面很难使用,我只需要进行微调,我想保持结构不变,尽管可能还需要一些重构.但是为了能够理解发生了什么,目前我只是放了一些qDebug()<< Dep; 在那里,并尝试了解输出.

问题是它根本没有格式化.有没有人知道一个小脚本来创建一个更好理解的显示格式?或者可能是Qt的一些补丁?

举个例子来说明我的痛苦:

QMap(("Test enable|test enable block", QMap(("disabled", QMap(("testblock1", QMap(("enableblock", QVariant(QString, "false") ) )  ) )  ) ( "enabled" ,  QMap(("testblock1", QMap(("enableblock", QVariant(QString, "true") ) )  ) )  ) )  ) ( "Test enable|test enable key" ,  QMap(("disabled", QMap(("testblock1|testkey", QMap(("enablekey", QVariant(QString, "false") ) )  ) )  ) ( "enabled" ,  QMap(("testblock1|testkey", QMap(("enablekey", QVariant(QString, "true") ) )  ) )  ) )  ) ( "testinsertitems|Insert item" ,  QMap(("test1", QMap(("testinsertitems|testinsert", QMap(("insertitems", QVariant(QVariantMap, QMap(("test1", QVariant(QString, "test1") ) )  ) ) )  ) ( "testinsertitems|testremove" ,  QMap(("removeitems", QVariant(QVariantMap, QMap(("test1", QVariant(QString, "test1") ) )  ) ) )  ) )  ) ( "test2" ,  QMap(("testinsertitems|testinsert", QMap(("insertitems", QVariant(QVariantMap, QMap(("test2", QVariant(QString, "test2") ) )  ) ) )  ) ( "testinsertitems|testremove" ,  QMap(("removeitems", QVariant(QVariantMap, QMap(("test2", QVariant(QString, "test2") ) )  ) ) )  ) )  ) )  ) ( "testsetminmax|test setmin" ,  QMap(("2", QMap(("testsetminmax|testkey1", QMap(("setmin", QVariant(int, 2) ) )  ) ( "testsetminmax|testkey2" ,  QMap(("setmax", QVariant(int, 2) ) )  ) )  ) ( "3" ,  QMap(("testsetminmax|testkey1", QMap(("setmin", QVariant(int, 3) ) )  ) ( "testsetminmax|testkey2" ,  QMap(("setmax", QVariant(int, 3) ) )  ) )  ) )  ) ( "testsetvalue|test set value" ,  QMap(("2", QMap(("testsetvalue|testkey1", QMap(("setvalue", QVariant(QString, "2") ) )  ) ( "testsetvalue|testkey2" ,  QMap(("setvalue", QVariant(QString, "2") ) )  ) ( "testsetvalue|testkey3" ,  QMap(("setvalue", QVariant(QString, "2") ) )  ) )  ) ( "3" ,  QMap(("testsetvalue|testkey1", QMap(("setvalue", QVariant(QString, "3") ) )  ) ( "testsetvalue|testkey2" ,  QMap(("setvalue", QVariant(QString, "3") ) )  ) ( "testsetvalue|testkey3" ,  QMap(("setvalue", QVariant(QString, "3") ) )  ) )  ) )  ) )
Run Code Online (Sandbox Code Playgroud)

谢谢

Tim*_*imW 9

这个用于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);
};
Run Code Online (Sandbox Code Playgroud)