qtreeview 重命名 禁用某些字符

vimacser 2019-01-15 03:53:14
请问,qtreeview在双击选中其中的某一项时,进入该项的重命名模式,这个时候怎么实时监测输入的字符是否合法?比如我想禁止输入某些特殊字符,该怎么处理?
...全文
498 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
弓人水 2019-01-17
  • 打赏
  • 举报
回复
QTreeView设置一个自定义的ItemDelegate即可。

QTreeView tree;
tree.setModel(&model);
tree.setItemDelegate(new MyDelegate);
tree.show();

一个自定义ItemDelegate的例子如下:

#include "mydelegate.h"
#include <QLineEdit>

MyDelegate::MyDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */,
const QModelIndex & index ) const
{
if(index.column() == 0) //只对第 0 列采用此方法编辑
{
QLineEdit* box = new QLineEdit(parent);
box->setPlaceholderText("Please input word characters.");
QRegExp rx("\\w+");
QValidator *validator = new QRegExpValidator(rx, box);
box->setValidator(validator);
return box;
}
return NULL;
}

void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit* box = static_cast<QLineEdit*>(editor);
box->setText(value);
}

void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit* box = static_cast<QLineEdit*>(editor);
model->setData(index, box->text(), Qt::EditRole);
}

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

kerwin liu 2019-01-16
  • 打赏
  • 举报
回复
qtreeview 能双击选中进行编辑。
假设你实现了编辑的方法。同时编辑时使用了QLineEdit。那禁用的方法可以发下

QRegExp regx("^[0-9]{13}");
QRegExpValidator* validator = new QRegExpValidator(regx, edit );
edit->setValidator(validator);

16,211

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧