加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 格式化qDebug for QMaps的输出

发布时间:2020-12-16 10:26:51 所属栏目:百科 来源:网络整理
导读:我目前正在维护遗留应用程序.这有很多结构,如: QMapQString,QMapQString,QVariant Dep; 由于界面很难使用,我只需要进行微调,我想保持结构不变,尽管可能还需要一些重构. 但为了能够理解发生了什么,目前我只是放了一些qDebug()德普;在那里,并尝试了解输出. 问
我目前正在维护遗留应用程序.这有很多结构,如:

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

由于界面很难使用,我只需要进行微调,我想保持结构不变,尽管可能还需要一些重构.
但为了能够理解发生了什么,目前我只是放了一些qDebug()<<德普;在那里,并尝试了解输出. 问题是它根本没有格式化.有没有人知道一个小脚本来创建一个更好理解的显示格式?或者可能是Qt的一些补丁? 举个例子来说明我的痛苦:

QMap(("Test enable|test enable block",QMap(("disabled",QMap(("testblock1",QMap(("enableblock",QVariant(QString,"false") ) )  ) )  ) ( "enabled","true") ) )  ) )  ) )  ) ( "Test enable|test enable key",QMap(("testblock1|testkey",QMap(("enablekey","true") ) )  ) )  ) )  ) ( "testinsertitems|Insert item",QMap(("test1",QMap(("testinsertitems|testinsert",QMap(("insertitems",QVariant(QVariantMap,"test1") ) )  ) ) )  ) ( "testinsertitems|testremove",QMap(("removeitems","test1") ) )  ) ) )  ) )  ) ( "test2",QMap(("test2","test2") ) )  ) ) )  ) ( "testinsertitems|testremove","test2") ) )  ) ) )  ) )  ) )  ) ( "testsetminmax|test setmin",QMap(("2",QMap(("testsetminmax|testkey1",QMap(("setmin",QVariant(int,2) ) )  ) ( "testsetminmax|testkey2",QMap(("setmax",2) ) )  ) )  ) ( "3",3) ) )  ) ( "testsetminmax|testkey2",3) ) )  ) )  ) )  ) ( "testsetvalue|test set value",QMap(("testsetvalue|testkey1",QMap(("setvalue","2") ) )  ) ( "testsetvalue|testkey2","2") ) )  ) ( "testsetvalue|testkey3","2") ) )  ) )  ) ( "3","3") ) )  ) ( "testsetvalue|testkey2","3") ) )  ) ( "testsetvalue|testkey3","3") ) )  ) )  ) )  ) )

谢谢

解决方法

这个用于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);
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读