-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexampleview.h
More file actions
47 lines (42 loc) · 1.39 KB
/
exampleview.h
File metadata and controls
47 lines (42 loc) · 1.39 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
#ifndef EXAMPLEVIEW_H
#define EXAMPLEVIEW_H
#include <QTableView>
#include <QKeyEvent>
class ExampleView : public QTableView
{
public:
ExampleView(QWidget *pParent = Q_NULLPTR) : QTableView(pParent) {}
private:
void keyPressEvent(QKeyEvent *pEvent) override
{
if (pEvent->key() == Qt::Key_Return)
{
// we captured the Enter key press, now we need to move to the next row
qint32 nNextRow = currentIndex().row() + 1;
if (nNextRow + 1 > model()->rowCount(currentIndex()))
{
// we are all the way down, we can't go any further
nNextRow = nNextRow - 1;
}
// now it's time to determine action
if (state() == QAbstractItemView::EditingState)
{
// if we are editing, confirm and move to the row below
QModelIndex oNextIndex = model()->index(nNextRow, currentIndex().column());
setCurrentIndex(oNextIndex);
selectionModel()->select(oNextIndex, QItemSelectionModel::ClearAndSelect);
}
else
{
// if we're not editing, start editing
edit(currentIndex());
}
}
else
{
// any other key was pressed
QAbstractItemView::keyPressEvent(pEvent);
}
}
};
#endif // EXAMPLEVIEW_H