我目前正在维护旧版应用。这有很多结构,如:
QMap<QString, QMap<QString, QMap<QString, QMap<QString, QVariant> > > > Dep;
由于接口很难使用,我只需要进行微调,我想保持结构不变,尽管可能还需要一些重构。 但为了能够理解发生了什么,目前我只是放了一些qDebug()&lt;&lt;德普;在那里,并尝试了解输出。
问题是它根本没有格式化。有没有人知道一个小脚本来创建一个更好理解的显示格式?或者可能是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") ) ) ) ) ) ) ) )
由于
答案 0 :(得分: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);
};
答案 1 :(得分:4)
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()。希望你明白这一点。