代码之家  ›  专栏  ›  技术社区  ›  data

为qmaps格式化qdebug的输出

  •  3
  • data  · 技术社区  · 16 年前

    我目前正在维护一个传统应用程序。这有相当多的结构,如:

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

    谢谢

    2 回复  |  直到 16 年前
        1
  •  9
  •   TimW    16 年前

    这一个用于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);
    };
    
        2
  •  4
  •   Gunther Piez    16 年前

    四维结构是众所周知的难以想象。 但是一些小的环呢?

    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()。希望你能理解。