2009-11-18 3 views
3

Je suis actuellement en train de gérer une application existante. Cela a assez peu de structures comme:Formatage de la sortie de qDebug pour QMaps

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

Comme les interfaces ne sont guère utilisés et je ne doivent faire des ajustements mineurs, je voudrais garder la structure telle qu'elle est, bien que certains refactoring pourrait être nécessaire de toute façon. Mais pour être en mesure de comprendre ce qui se passe, actuellement je viens de mettre un certain qDebug() < < Dep; là-bas, et essayez de comprendre la sortie.

Le problème est qu'il n'a aucun formatage du tout. Est-ce que quelqu'un connaît un petit script pour créer un format d'affichage plus compréhensible? Ou peut-être de quelques correctifs à Qt?

Pour vous donner un exemple pour mes souffrances:

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"))) )) )) )) 

Merci

Répondre

9

Celui-ci est pour les dimensions n et utilisera la sortie standard qDebug pour les types connus:

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

utilisation impressionnante de modèles et récursion :) –

4

Une structure à quatre dimensions est notoirement difficile à visualiser. Mais que diriez-vous de quelques petites boucles?

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

En utilisant l'espace de noms std, bien sûr. Ajouter dans setw() comme vous voulez. J'espère que vous avez l'idée.