欧洲杯线上买球-2024欧洲杯投注官网

qt—mvc架构-欧洲杯线上买球

【1】代理应用示例源码

用代码说事,比较靠谱。

代码目录:三个自定义类,重实现qstyleditemdelegate类。main函数应用示例。

(1)combodelegate.h

  #ifndef combodelegate_h
  #define combodelegate_h
  
  #include 
  
  class combodelegate : public qstyleditemdelegate
  {
  public:
      combodelegate(qobject *parent = null);
 
 protected:
     qwidget *createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const;
     void seteditordata(qwidget *editor, const qmodelindex &index) const;
     void setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const;
     void updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const;
 };
 
 #endif // combodelegate_h

(2)combodelegate.cpp

  #include "combodelegate.h"
  #include 
  
  combodelegate::combodelegate(qobject *parent)
      : qstyleditemdelegate(parent)
  {}
  
  qwidget *combodelegate::createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const
  {
     q_unused(option);
     q_unused(index);
 
     qstringlist list;
     list << "工人" << "农民" << "军人" << "律师";
 
     qcombobox *peditor = new qcombobox(parent);
     peditor->additems(list);
     peditor->installeventfilter(const_cast(this));
     return peditor;
 }
 
 void combodelegate::seteditordata(qwidget *editor, const qmodelindex &index) const
 {
     qstring strtext = index.model()->data(index).tostring();
     qcombobox *pcombox = null;
     pcombox = static_cast(editor);
     if (pcombox != null)
     {
         int nindex = pcombox->findtext(strtext);
         pcombox->setcurrentindex(nindex);
     }
 }
 
 void combodelegate::setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index)const
 {
     qcombobox *pcombox = null;
     pcombox = static_cast(editor);
     if (pcombox != null)
     {
         qstring strtext = pcombox->currenttext();
         model->setdata(index, strtext);
     }
 }
 
 void combodelegate::updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index)const
 {
     q_unused(index);
    editor->setgeometry(option.rect);
 }

(3)datedelegate.h

  #ifndef datedelegate_h
  #define datedelegate_h
  
  #include 
  
  class datedelegate : public qstyleditemdelegate
  {
  public:
      datedelegate(qobject *parent = null);
 
 protected:
     qwidget *createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const;
     void seteditordata(qwidget *editor, const qmodelindex &index) const;
     void setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const;
     void updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const;
 };
 
 #endif // datedelegate_h

(4)datedelegate.cpp

  #include "datedelegate.h"
  #include 
  
  datedelegate::datedelegate(qobject *parent)
      : qstyleditemdelegate(parent)
  {}
  
  // 首先创建要进行代理的窗体
  qwidget *datedelegate::createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const
 {
     q_unused(option);
     q_unused(index);
 
     qdatetimeedit *peditor = new qdatetimeedit(parent);      // 一个日历的控件
     peditor->setdisplayformat("yyyy-mm-dd");   // 日期时间的显示格式
     peditor->setcalendarpopup(true);   // 以下拉的方式显示
     peditor->installeventfilter(const_cast(this));  // 调用这个函数安装事件过滤器,使这个对象可以捕获qdatetimeedit对象的事件
     return peditor;
 }
 
 // 这个是初始化作用,初始化代理控件的数据
 void datedelegate::seteditordata(qwidget *editor, const qmodelindex &index) const
 {
   // 先用这个index返回这个model然后用这个model得到index对应的数据
   qstring strdate = index.model()->data(index).tostring();
   qdate date = qdate::fromstring(strdate, qt::isodate);     // 根据qstring类型得到相应的时间类型
   qdatetimeedit *peditor = null;
   peditor = static_cast(editor);    // 强转为qdatetimeedit*类型
   if (peditor != null)
   {
       peditor->setdate(date);      // 设置代理控件的显示数据
   }
 }
 
 // 将代理控件里面的数据更新到视图控件中
 // void setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const;
 void datedelegate::setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const
 {
   qdatetimeedit *peditor = null;
   peditor = static_cast(editor);    // 得到时间
   if (peditor != null)
   {
       qdate date = peditor->date();    // 得到时间
       model->setdata(index, qvariant(date.tostring(qt::isodate)));    // 把值放到相应的index里面
   }
 }
 
 // 代理中数据的改变放到model中
 // void updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const;
 void datedelegate::updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const
 {
     q_unused(index);
     editor->setgeometry(option.rect);
 }

(5)spindelegate.h

  #ifndef spindelegate_h
  #define spindelegate_h
  
 #include 
  
  class spindelegate : public qstyleditemdelegate
  {
  public:
      spindelegate(qobject *parent = null);
 
 protected:
     qwidget *createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const;
     void seteditordata(qwidget *editor, const qmodelindex &index) const;
     void setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const;
     void updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const;
 };
 
 #endif // spindelegate_h

(6)spindelegate.cpp

  #include "spindelegate.h"
  
  #include 
  
  spindelegate::spindelegate(qobject *parent)
      : qstyleditemdelegate(parent)
  {
  }
  
 qwidget *spindelegate::createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const
 {
     q_unused(option);
     q_unused(index);
 
     qspinbox *peditor = new qspinbox(parent);
     peditor->setrange(0, 30000);
     peditor->installeventfilter(const_cast(this));
     return peditor;
 }
 
 void spindelegate::seteditordata(qwidget *editor, const qmodelindex &index) const
 {
     int value = index.model()->data(index).toint();
     qspinbox *pspinbox = null;
     pspinbox = static_cast(editor);
     if (pspinbox != null)
     {
         pspinbox->setvalue(value);
     }
 }
 
 void spindelegate::setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const
 {
     qspinbox *pspinbox = null;
     pspinbox = static_cast(editor);
     if (pspinbox != null)
     {
         int value = pspinbox->value();
         model->setdata(index, value);
     }
 }
 
 void spindelegate::updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const
 {
     q_unused(index);
 
     editor->setgeometry(option.rect);
 }

(7)main.cpp

  #include 
  #include 
  #include 
  #include 
  #include 
  #include 
  #include 
  #include "datedelegate.h"
  #include "combodelegate.h"
 #include "spindelegate.h"
 
 int main(int argc, char *argv[])
 {
     qapplication a(argc, argv);
 
     qstandarditemmodel model(4, 4);
     model.setheaderdata(0, qt::horizontal, qlatin1string("name"));
     model.setheaderdata(1, qt::horizontal, qlatin1string("birthday"));
     model.setheaderdata(2, qt::horizontal, qlatin1string("job"));
     model.setheaderdata(3, qt::horizontal, qlatin1string("income"));
 
     qfile file(qlatin1string("/mnt/liuy/info"));
     if (!file.open(qiodevice::readonly | qiodevice::text))
     {
         qdebug() << "open the file failed...";
         return -1;
     }
 
     qtextstream out(&file);
     qstring line;
     model.removerows(0, model.rowcount(qmodelindex()), qmodelindex());
     int row = 0;
     do
     {
         line = out.readline();
         if (!line.isempty())
         {
             model.insertrows(row, 1, qmodelindex());
             qstringlist pieces = line.split(",", qstring::skipemptyparts);
             model.setdata(model.index(row, 0, qmodelindex()), pieces.value(0));
             model.setdata(model.index(row, 1, qmodelindex()), pieces.value(1));
             model.setdata(model.index(row, 2, qmodelindex()), pieces.value(2));
             model.setdata(model.index(row, 3, qmodelindex()), pieces.value(3));
               row;
         }
     } while(!line.isempty());
     file.close();
 
     qtableview tableview;
     tableview.setmodel(&model); // 绑定model
     tableview.setwindowtitle(qlatin1string("delegate"));
 
     datedelegate datedelegate;
     tableview.setitemdelegateforcolumn(1, &datedelegate); // 第一列代理
     combodelegate combodelegate;
     tableview.setitemdelegateforcolumn(2, &combodelegate);// 第二列代理
     spindelegate spindelegate;
     tableview.setitemdelegateforcolumn(3, &spindelegate); // 第三列代理
 
     tableview.resize(500, 300); // 重置大小
     tableview.show();
 
     return a.exec();
 }

备注:此示例运行环境为ubuntu qt5.3.2

【2】读取的信息文件

文件info 内容如下(注意:文件格式):

liu,1977-01-05,工人,1500
wang,1987-11-25,医生,2500
sun,1967-10-05,军人,500
zhang,1978-01-12,律师,4500

【3】运行效果图

运行效果图:

第二列编辑图(时间日期控件):

第三列编辑图(下拉框控件):

第四列编辑图(微调框控件):

投稿时间:2023-03-15  最后更新:2023-03-15

标签:             对象     类型   时间   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
2024欧洲杯投注官网的友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有欧洲杯线上买球的版权问题,请与我联系,qq:4156828  

© 欧洲杯线上买球 copyright 2008-2023 all rights reserved. powered by 欧洲杯线上买球-2024欧洲杯投注官网

网站地图