我有一个QTableView来显示一堆数据,这些数据存储在QAbstractTableModel中。我也想在某个时候编辑数据,所以当我双击单元格时,我使用QStyledItemDelegate创建一个编辑器(QTextEdit类型)。但是,我想自己处理关键的Press事件,但我从未在文本编辑编辑器中按下Key_Escape(可以捕获其他键,如Enter和Ascii)。我检查了代码,发现Escape直接连接到QTableView的closeEditor()函数,这是一个虚方法并自动调用。据我所知,按键事件是事件处理的底层,但这种情况并非如此。
我确实需要捕获Escape键按下事件以便我自己处理它,有人能告诉我该怎么做吗?谢谢!
答案 0 :(得分:1)
我得到了答案,我认为会帮助别人:
覆盖QStyledItemDelegate :: eventFilter()方法:
MyItemDelegate::eventFilter(QObject* editor, QEvent* event)
{
if(event->type()==QEvent::KeyPress)
return false;
return QStyledItemDelegate::eventFilter(editor, event);
}
根据Qt的文档,如果给定的编辑器是有效的QWidget并且处理了给定的事件,QStyledItemDelegate :: eventFilter()将返回true;否则返回false。默认情况下处理Tab,Backtab,Enter,Return和Ecs。因此,如果您想自己处理按键事件,则必须在发生KeyPress事件时让eventFilter返回false。这样就可以调用编辑器的keyPressEvent()方法。
答案 1 :(得分:0)
QStyledItemDelegate :: eventFilter不可能被虚拟保护
为了获取事件,你应该继承你自己的QEditLine并在那里覆盖:: keyPressEvent。注意代码。我传递了QTableWidget单元格的实际行和列,以便知道我们在覆盖的QLineEditor中编辑的内容。
//.h
class MyStyledItemDelegate : public QStyledItemDelegate
{
public:
MyStyledItemDelegate(QObject *parent = 0);
QWidget* createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const;
};
//.cpp
#include "mylineedit.h"
MyStyledItemDelegate::MyStyledItemDelegate(QObject *parent)
:QStyledItemDelegate(parent)
{
}
QWidget* MyStyledItemDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
MyLineEdit* editor = new MyLineEdit(parent,index.row(),index.column());
return editor;
}
/////////////////////////////////////////////////////////////
//My own QLineEdit
/////////////////////////////////////////////////////////////
//.h
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
int _nRow;
int _nCol;
public:
MyLineEdit(QWidget *parent = 0,int nRow=-1, int nCol=-1);
virtual void keyPressEvent(QKeyEvent* event);
signals:
void mySignal(const QVector<QVariant> &);
public slots:
};
//.cpp
MyLineEdit::MyLineEdit(QWidget *parent, int nRow,int nCol):
QLineEdit(parent)
{
_nRow=nRow;
_nCol=nCol;
}
/////////////////////////////////////////////////////////////////////////
void MyLineEdit::keyPressEvent(QKeyEvent* event)
{
qDebug() << "MyLineEdit::OnKeyPressEvent:"<<event->text()<< " row="<<_nRow<<" col=" <<_nCol;
///SET YOUR CODE HERE
QLineEdit::keyPressEvent(event);
}
//.h
class MyStyledItemDelegate : public QStyledItemDelegate
{
public:
MyStyledItemDelegate(QObject *parent = 0);
QWidget* createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const;
};
//.cpp
#include "mylineedit.h"
MyStyledItemDelegate::MyStyledItemDelegate(QObject *parent)
:QStyledItemDelegate(parent)
{
}
QWidget* MyStyledItemDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
MyLineEdit* editor = new MyLineEdit(parent,index.row(),index.column());
return editor;
}
/////////////////////////////////////////////////////////////
//My own QLineEdit
/////////////////////////////////////////////////////////////
//.h
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
int _nRow;
int _nCol;
public:
MyLineEdit(QWidget *parent = 0,int nRow=-1, int nCol=-1);
virtual void keyPressEvent(QKeyEvent* event);
signals:
void mySignal(const QVector<QVariant> &);
public slots:
};
//.cpp
MyLineEdit::MyLineEdit(QWidget *parent, int nRow,int nCol):
QLineEdit(parent)
{
_nRow=nRow;
_nCol=nCol;
}
/////////////////////////////////////////////////////////////////////////
void MyLineEdit::keyPressEvent(QKeyEvent* event)
{
qDebug() << "MyLineEdit::OnKeyPressEvent:"<<event->text()<< " row="<<_nRow<<" col=" <<_nCol;
///SET YOUR CODE HERE
QLineEdit::keyPressEvent(event);
}