2009-11-18 6 views
3

현재 레거시 앱을 유지 관리하고 있습니다. 이 같은 꽤 많은 구조를 가지고QMaps 용 qDebug의 출력 형식을 지정하십시오.

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

인터페이스는 거의 사용되지 않고 난 단지 약간의 조정을해야 할 때, 나는 그대로 리팩토링 어쨌든 필요할 수 있지만, 구조를 유지하고 싶습니다. 하지만 무슨 일이 벌어지고 있는지 이해하려면 현재 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"))) )) )) )) 

감사

답변

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; 
     } 
    } 
} 

물론 네임 스페이스 표준을 사용합니다. setw()에 원하는대로 추가하십시오. 당신이 아이디어를 얻기를 바랍니다.

관련 문제