免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3594 | 回复: 1
打印 上一主题 下一主题

qt中数据存储方法(接口)的思路 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-14 14:32 |只看该作者 |倒序浏览
声明:由于其它原因,只谈思路,具体实现还要靠自己在实际开发中总结
在写这个之前,一直在想叫什么标题比较合适
在写这个之前,在qtcn论坛查了“数据”关键字,大概只要35条左右记录,提到的与我想要说的无关


     先说一下现象。以前自己写过程序,也看过别人写的程序,感觉代码比较长(个人觉得代码多维护比较麻烦,当然代码多少与业务逻辑有关),我说的长是指:在实 现同样功能的情况下,它本身有很大的精简空间。由于对数据存储的认识不同,造成的大量精力在为数据存储,数据同步及数据显示写很多逻辑。

由于不同的数据打过多年的交道,特别欣赏数据库精简的接口调用,常用的大概就select, update, delete, insert, commit。自己也想实现这样的类似的接口,以便写程序时不要为数据花太多精力。

自己在刚接触QT时,也是先看MVC模式,感觉QT把MVC发挥得淋漓尽致。随着时间的推移,发现MVC结构还是比较俗套,有点按步就班的感觉,在这种 模式下代码精简下来也不是一件容易的事。后来就把MVC模式改成xxTable(xxWidget)模式,不再理会其中的Model、View怎么结合的 了。但这样也有一个问题, 就数据传递、同步问题。当存在多个xxTable(xxWidget)时,它们之间需要数据同步显示,于是写了个itemList。这个itemList 类似一栋大厦,可以自由向上增加(记录Item数量),每层也能水平自由增加(Item中的字段),不用关心每层需要对齐问题,说得通俗一点就类似 Table结构,只是每条记录Item中的字段不受约束,可自己更改。记录中的每个节点是parent->self->children关 系,也就说每个节点都可以向上找到root_node节点,由root_node可以遍历所有的节点。大概就这样的一个立体结构。

这个类提供了常用接口setData,getData,setItemData, getItemData还有其它的呢,当然还有,不过个人常用的就setData,getData,setItemData, getItemData(代名词),可能是以前用java用习惯了而采用这样的命名
前两个用于key->value结构,后两个用于List结构。
  1. void ImEffectWidget::slotComBox(int index)  //slot 处理
  2. {
  3. QString text;
  4. QColor color;
  5. QFont font;
  6. QObject *obj = sender();
  7. /* 1. for crop  ***************************/
  8. if (obj->objectName() == "comboxZoom"){
  9.   doComboxValue(m_ui.comboxZoom, "effect_crop_aspect_name", index);
  10. }
  11. /* 2. for effect  ***************************/
  12. else if (obj->objectName() == "comboxEffect"){
  13.   doComboxValue(m_ui.comboxEffect, "filter_effect_name", index);
  14.   m_treeItem->setMediaData("filter_effect_index", index); //for player
  15. }
  16. /* 3. for watermark  ***************************/
  17. else if (obj->objectName() == "comboxFont"){
  18.   doComboxValue(m_ui.comboxFont, "effect_item_font_family", index, true);
  19. }else if (obj->objectName() == "comboxColor"){
  20.   doComboxValue(m_ui.comboxColor, "effect_item_color", index, true);
  21. }else if (obj->objectName() == "comboxSize"){
  22.   doComboxValue(m_ui.comboxFont, "effect_item_font_size", index, true);
  23. }else if (obj->objectName() == "comboxStyle"){
  24.   slotChangeStyle(index);
  25. }
  26. /* 5. for subtitle  ***************************/
  27. else if (obj->objectName() == "comboxSTCodec"){
  28.   text = m_ui.comboxSTCodec->itemText(index);
  29.   m_treeItem->setMediaData("effect_subtitle_codec", text);
  30. }else if (obj->objectName() == "comboxSTFrontColor"){
  31.   doComboxValue(m_ui.comboxSTFrontColor, "effect_subtitle_front_color", index);
  32. }else if (obj->objectName() == "comboxSTBackColor"){
  33.   doComboxValue(m_ui.comboxSTBackColor, "effect_subtitle_back_color", index);
  34. }else if (obj->objectName() == "comboxSTOutlineColor"){
  35.   doComboxValue(m_ui.comboxSTOutlineColor, "effect_subtitle_outline_color", index);
  36. }else if (obj->objectName() == "comboxSTScaleX"){
  37.   doComboxValue(m_ui.comboxSTScaleX, "effect_subtitle_scalex", index);  
  38. }else if (obj->objectName() == "comboxSTScaleY"){
  39.   doComboxValue(m_ui.comboxSTScaleY, "effect_subtitle_scaley", index);
  40. }else if (obj->objectName() == "comboxSTFont"){
  41.   doComboxValue(m_ui.comboxSTFont, "effect_subtitle_font_family", index);
  42. }else if (obj->objectName() == "comboxSTSize"){
  43.   doComboxValue(m_ui.comboxSTSize, "effect_subtitle_font_size", index);
  44. }else if (obj->objectName() == "comboxSTStyle"){
  45.   slotChangeStyle(index);
  46. }else if (obj->objectName() == "comboxSTSpacing"){
  47.   doComboxValue(m_ui.comboxSTSpacing, "effect_subtitle_spacing", index);
  48. }else if (obj->objectName() == "comboxSTTimer"){
  49.   doComboxValue(m_ui.comboxSTTimer, "effect_subtitle_spacing", index);
  50. }

  51. sendEffectEvent(); //for sdl event
  52. }

  53. #define COLORVAL() \
  54. if (comboxIndex < 0) return; \
  55. ImColorComboBox *oc = qobject_cast<ImColorComboBox *>(o); \
  56. color = oc->color(comboxIndex); \

  57. #define FONTVAL() \
  58. if (saveKey.indexOf("subtitle") != -1) \
  59.   fontKey = "effect_subtitle_font"; \
  60. else \
  61.   fontKey = "effect_item_font"; \
  62. if (bSaveList==true) { \
  63.   font = qVariantValue<QFont>(m_treeItem->mdata(fontKey)); \
  64. }else{ \
  65.   font = qVariantValue<QFont>(m_treeItem->getDataItem(fontKey)); \
  66. }

  67. void ImEffectWidget::doComboxValue(QComboBox *o, const QString &saveKey, int comboxIndex, bool bSaveList)
  68. {
  69. QString text, dataKey, fontKey;
  70. QFont font;
  71. QColor color;
  72. if (comboxIndex > -1)
  73.   text = o->itemText(comboxIndex);
  74. else
  75.   text = o->currentText();

  76. bool bFont  = o->property("font").toBool();
  77. bool bColor = o->property("color").toBool();
  78. if (bFont || bColor) dataKey=QString("%1_data").arg(saveKey);

  79. if (bFont)
  80. {
  81.   FONTVAL();

  82.   if (text == "effect_item_font_family")
  83.    font.setFamily(text);
  84.   else if(text == "effect_item_font_size")
  85.    font.setPointSize(text.toInt());
  86. }

  87. if (bSaveList)
  88. {
  89.   if (bFont){  // font
  90.    m_treeItem->setDataItem(saveKey, text);
  91.    m_treeItem->setDataItem(fontKey, qVariantFromValue<QFont>(font));
  92.   }else if(bColor){ //color
  93.    COLORVAL();

  94.    m_treeItem->setDataItem(saveKey, text);
  95.    m_treeItem->setDataItem(dataKey, qVariantFromValue(color));
  96.   }else   //other
  97.    m_treeItem->setDataItem(saveKey, text);
  98. }else{
  99.   if (bFont){
  100.    m_treeItem->setMediaData(saveKey, text);
  101.    m_treeItem->setMediaData(fontKey, qVariantFromValue<QFont>(font));
  102.   }else if(bColor){
  103.    COLORVAL();

  104.    m_treeItem->setMediaData(saveKey, text);
  105.    m_treeItem->setMediaData(dataKey, qVariantFromValue(color));
  106.   }else
  107.    m_treeItem->setMediaData(saveKey, text);
  108. }
  109. }
复制代码

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:56:11
2 [报告]
发表于 2011-03-21 10:53 |只看该作者
qt和java命名一样
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP