当前位置: 首页 > wzjs >正文

网站设计建议新东方雅思培训价目表

网站设计建议,新东方雅思培训价目表,甘肃网站建设哪家好,湖南易图做推广送网站QItemDelegate 当我们想重新实现一个代理时,可以子类化QItemDelegate。实现item编辑时特定的效果,比如在item编辑时我们设置一个QSpinBox返回。 创建一个QApplication项目,然后我们新增一个类,类名叫做spinboxdelegate。 class S…

QItemDelegate

当我们想重新实现一个代理时,可以子类化QItemDelegate。实现item编辑时特定的效果,比如在item编辑时我们设置一个QSpinBox返回。
创建一个QApplication项目,然后我们新增一个类,类名叫做spinboxdelegate。

class SpinBoxDelegate : public QItemDelegate
{Q_OBJECT
public:explicit SpinBoxDelegate(QObject * parent=0);QWidget * createEditor(QWidget* parent,   const QStyleOptionViewItem &option,const QModelIndex &index) const override;void setEditorData(QWidget *editor, const QModelIndex &index) const override;void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option,const QModelIndex &index) const override;
};

SpinBoxDelegate类中声明了几个函数,这些函数在QItemDelegate继承而来,通过重写实现我们自己定义的代理功能。
createEditor函数是在item被双击后进入编辑状态时触发的,返回一个QWidget控件用来管理编辑。
setModelData是在item被修改后触发的,将改动的内容写入model中。
setEditorData是在item被双击进入编辑状态时,将model的内容写入editor中。
updateEditorGeometry是刷新editor的矩形区域,因为随着item变大或者拉伸,它的区域也要随之刷新。

具体实现

1 创建editor, 返回一个spinbox

QWidget * SpinBoxDelegate::createEditor(QWidget* parent,   const QStyleOptionViewItem &option,const QModelIndex &index) const{QSpinBox * editor = new QSpinBox(parent);editor->setMinimum(0);editor->setMaximum(100);return editor;
}

2 在编辑状态时,将model的数据写入editor

void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {int value = index.model()->data(index, Qt::EditRole).toInt();QSpinBox * spinBox = static_cast<QSpinBox*>(editor);spinBox->setValue(value);
}

3 编辑完成时将editor的内容写入model

void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {QSpinBox * spinBox = static_cast<QSpinBox*>(editor);spinBox->interpretText();int value = spinBox->value();model->setData(index, value, Qt::EditRole);
}

4 刷新矩形区域

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option,const QModelIndex &index) const {editor->setGeometry(option.rect);
}

接下来我们在MainWindow的构造函数里创建两个model和view,我们对其中的一个view使用我们自定义的delegate

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);QStandardItemModel * model = new QStandardItemModel(7,4,this);for(int row=0; row < 7; row++){for(int column = 0; column < 4; column++){QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));model->setItem(row, column, item);}}QTableView* _table_view = new QTableView;_table_view->setModel(model);setCentralWidget(_table_view);this->resize(800,800);QTableView* _table_view2 = new QTableView;SpinBoxDelegate * delegate = new SpinBoxDelegate(this);QStandardItemModel * model2 = new QStandardItemModel(7,4,this);for(int row=0; row < 7; row++){for(int column = 0; column < 4; column++){QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));model2->setItem(row, column, item);}}_table_view2->setModel(model2);_table_view2->setItemDelegate(delegate);_table_view2->show();_table_view2->resize(800,800);
}

运行程序后,双击两个view的item,可以看到效果的不同
https://cdn.llfc.club/1671606628462.jpg

源码链接

源码链接
https://gitee.com/secondtonone1/qt-learning-notes

http://www.dtcms.com/wzjs/35598.html

相关文章:

  • 做评测好的视频网站有哪些百度网站建设
  • 网站开发语言哪种好搜狗竞价推广效果怎么样
  • 做pc网站最大分辨率想做百度推广找谁
  • 做app还是做网站如何自己创造一个网站平台
  • 做啥类型网站网站排名优化公司
  • 个人网站做百度推广营销策略怎么写
  • 怎么做网站安全运维网站搭建需要什么
  • 鞍山晟宇网站建设百度seo关键词优化工具
  • 怎么自己开一个平台杭州专业seo公司
  • 广州做手机网站建设广州seo网站推广
  • 有了代刷网的源码怎么做网站seo排名系统源码
  • 如何制作推广网站郑州疫情最新情况
  • 做网站需要服务器吗口碑营销的定义
  • 新疆生产建设兵团人社厅网站网络推广哪家做得比较好
  • 网站建设书籍免费学校教育培训机构
  • sempre音乐术语seo技巧
  • 购物网站开发实例seo综合查询
  • 公司网站建设高端网站建设网页设计平台推广费用一般是多少
  • 哪里有学做视频的网站3中国站长工具
  • 国内优秀个人网站国内十大搜索引擎排名
  • 河北制作网站模板建站公司营销手段有哪些
  • 网站创造无锡网站制作优化
  • 怎么做网站平台刚刚传来最新消息
  • 手机当服务器建网站找小网站的关键词
  • 怎么写网站建设与运营seo综合查询怎么用的
  • phpcms做的网站百度游戏官网
  • phpweb手机网站程序百度问答
  • 点金wordpressseo在线论坛
  • 高端品牌网站设计电话上海seo推广
  • 网站店铺vr场景可以做吗域名收录批量查询