【问题标题】:Format the output of qDebug for QMaps为 QMaps 格式化 qDebug 的输出
【发布时间】:2009-11-18 18:19:40
【问题描述】:

我目前正在维护旧版应用程序。这有很多结构,例如:

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

由于接口很少使用,我只需要做一些小的调整,我想保持结构不变,尽管无论如何可能需要进行一些重构。 但是为了能够理解发生了什么,目前我只是放了一些 qDebug()

问题是它根本没有格式。有谁知道创建更好理解的显示格式的小脚本?或者可能是 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") ) )  ) )  ) )  ) )

谢谢

【问题讨论】:

    标签: c++ qt formatting


    【解决方案1】:

    这是用于 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】:

    众所周知,四维结构很难可视化。 但是一些小循环呢?

    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() 。希望你能明白。

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 2017-04-20
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多