forgottenduck
27th October 2014, 20:48
So I've implemented a custom text edit widget which only accepts numbers and other math symbols as edit, as well as doing some other stuff with the key press event. My widget works fine when tested on it's own but it seems that when it is used in the delegate that I created it doesn't receive the keypressevents.
After some research I determined that I was missing a crucial part of my custom delegate implementation. I still needed to implement eventFilter() and return false on a keypress event so that the editor would receive the event. What I discovered while doing this is that my delegate seems to be using a QExpandingLineEdit instead of my custom editor. So even with the event filter, my custom widget is not getting the keypress events. I don't understand what I'm missing from my implementation.
MathEditDelegate::MathEditDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void MathEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
if(MathEdit* edit = qobject_cast<MathEdit*>(editor)){
QString currentText = index.data(Qt::DisplayRole | Qt::EditRole).toString();
edit->setHtml(currentText);
}
else
QStyledItemDelegate::setEditorData(editor, index);
}
bool MathEditDelegate::eventFilter(QObject *editor, QEvent *event){
if(event->type()==QEvent::KeyPress){
qDebug()<<editor->metaObject()->className(); //This should be MathEdit, but instead is always QExpandingLineEdit
return false;
}
return QStyledItemDelegate::eventFilter(editor, event);
}
void MathEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
if(MathEdit* edit = qobject_cast<MathEdit*>(editor)){
model->setData(index, edit->toHtml(), Qt::DisplayRole | Qt::EditRole);
}
else
QStyledItemDelegate::setModelData(editor, model, index);
}
QWidget* MathEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/){
MathEdit* edit = new MathEdit(parent);
return edit;
}
I've implemented createEditor so shouldn't the editor in eventFilter be the editor I created and not the default "QExpandingLineEdit"?
Upon further testing, I noticed that all of my functions with editor as an argument are coming in as QExpandingLineEdits and not MathEdits. I even made a minimal example with just a standard table view and model with the delegate assigned, and I got the same results as I did in the larger project. I have no idea what I'm missing.
Any insight would be appreciated.
EDIT:
Turns out I was just missing the override keyword on my methods. How embarrassing.
After some research I determined that I was missing a crucial part of my custom delegate implementation. I still needed to implement eventFilter() and return false on a keypress event so that the editor would receive the event. What I discovered while doing this is that my delegate seems to be using a QExpandingLineEdit instead of my custom editor. So even with the event filter, my custom widget is not getting the keypress events. I don't understand what I'm missing from my implementation.
MathEditDelegate::MathEditDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void MathEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
if(MathEdit* edit = qobject_cast<MathEdit*>(editor)){
QString currentText = index.data(Qt::DisplayRole | Qt::EditRole).toString();
edit->setHtml(currentText);
}
else
QStyledItemDelegate::setEditorData(editor, index);
}
bool MathEditDelegate::eventFilter(QObject *editor, QEvent *event){
if(event->type()==QEvent::KeyPress){
qDebug()<<editor->metaObject()->className(); //This should be MathEdit, but instead is always QExpandingLineEdit
return false;
}
return QStyledItemDelegate::eventFilter(editor, event);
}
void MathEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
if(MathEdit* edit = qobject_cast<MathEdit*>(editor)){
model->setData(index, edit->toHtml(), Qt::DisplayRole | Qt::EditRole);
}
else
QStyledItemDelegate::setModelData(editor, model, index);
}
QWidget* MathEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/){
MathEdit* edit = new MathEdit(parent);
return edit;
}
I've implemented createEditor so shouldn't the editor in eventFilter be the editor I created and not the default "QExpandingLineEdit"?
Upon further testing, I noticed that all of my functions with editor as an argument are coming in as QExpandingLineEdits and not MathEdits. I even made a minimal example with just a standard table view and model with the delegate assigned, and I got the same results as I did in the larger project. I have no idea what I'm missing.
Any insight would be appreciated.
EDIT:
Turns out I was just missing the override keyword on my methods. How embarrassing.