forked from xiaozai511/LuaMemorySnapShot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytreeview.cpp
More file actions
124 lines (105 loc) · 2.71 KB
/
mytreeview.cpp
File metadata and controls
124 lines (105 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "mytreeview.h"
#include <qdebug.h>
#include <QStyledItemDelegate>
#include <QMessageBox>
#include <QFileInfo>
#include <QLineEdit>
class MyTextEditorDelegate : public QStyledItemDelegate
{
public:
explicit MyTextEditorDelegate(QObject *parent = Q_NULLPTR) :
mStartSuffix(new QString()),
QStyledItemDelegate(parent)
{
}
~MyTextEditorDelegate()
{
delete (mStartSuffix);
}
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
{
QStyledItemDelegate::setEditorData(editor, index);
QString text = index.model()->data(index).toString();
//获取后缀名
QFileInfo tmp(text);
*mStartSuffix = tmp.suffix();
MyTreeView* _par = dynamic_cast<MyTreeView*>(parent());
if(_par) {
_par->setEditing(true);
}
}
virtual void setModelData(QWidget *editor,
QAbstractItemModel *mModel,
const QModelIndex &index) const
{
QLineEdit* textBox = dynamic_cast<QLineEdit*>(editor);
QString text = textBox->text();
//获取后缀名
QFileInfo tmp(text);
QString suffix = tmp.suffix();
//不允许修改扩展名
if(*mStartSuffix != suffix) {
QMessageBox::information(nullptr,QString(tr("重命名失败")),QString(tr("不允许修改扩展名,重命名失败,请重试!")),QMessageBox::Ok);
return;
} else {
mModel->setData(index, text);
}
}
virtual void destroyEditor(QWidget *editor, const QModelIndex &index) const
{
qDebug() << "destroyEditor";
MyTreeView* _par = dynamic_cast<MyTreeView*>(parent());
if(_par) {
_par->setEditing(false);
}
}
protected:
QString* mStartSuffix;
};
MyTreeView::MyTreeView(QWidget *parent):
QTreeView(parent),
bIsEditing(false)
{
mDelegage = new MyTextEditorDelegate(this);
setItemDelegate(mDelegage);
}
MyTreeView::~MyTreeView()
{
delete mDelegage;
}
bool MyTreeView::isEditing()
{
return bIsEditing;
}
void MyTreeView::setEditing(bool bEditing)
{
bIsEditing = bEditing;
}
void MyTreeView::keyPressEvent(QKeyEvent *event)
{
QTreeView::keyPressEvent(event);
if(!isEditing()) { //非编辑状态才发消息
emit onKeyPressEvent(event);
}
}
void MyTreeView::keyReleaseEvent(QKeyEvent *event)
{
QTreeView::keyReleaseEvent(event);
if(!isEditing()) { //非编辑状态才发消息
emit onKeyReleaseEvent(event);
}
}
void MyTreeView::mousePressEvent(QMouseEvent *event)
{
QTreeView::mousePressEvent(event);
if(!isEditing()) { //非编辑状态才发消息
emit onMousePressEvent(event);
}
}
void MyTreeView::mouseReleaseEvent(QMouseEvent *event)
{
QTreeView::mouseReleaseEvent(event);
if(!isEditing()) { //非编辑状态才发消息
emit onMouseReleaseEvent(event);
}
}