自定义QTreeView中的条目编辑器
来源:互联网 发布:如何移动数据透视表 编辑:程序博客网 时间:2024/06/06 03:41
默认情况下QTreeView中的条目在用户双击情况下会产生编辑器,允许用户输入,但是自己用了这么久,每个功能都是不需要的,往往都是右键啊,其它功能键啊,激活编辑器。
而同时,在项目中,这种原生的编辑器往往都不能满足需求,因为在某些机器测试情况下,会出现怪怪的样子,不是编辑器的高度过窄,就是不能限制用户输入,而这是非常重要的。今天,我们用QItemDelegate生成自己的编辑器(当然编辑器是QLineEdit)。
EditDelegate::EditDelegate(QObject* parent):QItemDelegate(parent){}QWidget* EditDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const{QLineEdit* editor = NULL;if(index.isValid()){editor = new QLineEdit(parent);editor->setMaxLength(32);//限制最大只能输入32个字符editor->setFixedHeight(20);//调到满意为止}return editor;}void EditDelegate::setEditorData(QWidget* editor,const QModelIndex& index) const{if(index.isValid()){QLineEdit* lineE = qobject_cast<QLineEdit*>(editor);lineE->setText(index.data(Qt::DisplayRole).toString());//text() == data(Qt::DispalyRole).toString()}}void EditDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const{if(index.isValid()){QLineEdit* lineE = qobject_cast<QLineEdit*>(editor);model->setData(index,lineE->text());}}void EditDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const{editor->setGeometry(option.rect);}二话不说,贴代码(主要来自Qt高级编程一书,感谢作者)
使用的话,当使用QTreeView槽函数edit(位置索引)时,就跳出这个编辑器了,而且还可以通过样式表设置外观,挺不错的。
- 自定义QTreeView中的条目编辑器
- 自定义QTreeView
- 自定义 QTreeView
- QSortFilterProxyModel过滤QTreeView中的文件
- QStandardItemModel在QTreeView中的使用
- qtreeview
- QTreeView
- QTreeView
- 树形控件QTreeView使用自定义模型model
- RecyclerView自定义条目加载
- VS2010+QT5编程之巧妙运用定时器解决QTreeView/QTableView等QAbstractItemView实时条目统计问题
- Qt QTreeView 加 自定义Model实现自定义树形控件
- 自定义通用的标题条目
- 自定义RecyclerView之多条目加载
- 自定义绘制表格和树条目
- 自定义绘制表格和树条目
- 自定义组合控件---设置界面条目控件
- 自定义listVIew 实现条目文字渐变效果
- Linux的shell编程
- 汇编编程简介
- 【转】Android上HDMI介绍(基于高通平台)
- 对内存重叠的深入认识
- 鄙视一下转blog不标明"转载"的家伙
- 自定义QTreeView中的条目编辑器
- javascript 十进制加密 十六进制加密
- 第二章 VB语言基础
- oracle启动各个状态下v$pgastat不同,why\?
- IPhone剪卡
- 自己动手写Android Widget —— DoingWidget(未完成版)
- 什么是PE系统?怎样进入PE?PE的文件结构是什么?
- cookie与session(上) 转载 原文链接http://blog.sina.com.cn/s/blog_4745d1c10100ihnq.html
- cookie与session(下) 转载 原文链接http://blog.sina.com.cn/s/blog_4745d1c10100ihnq.html