Hi folks,
Thank you so much for the help!
I have implemented a custom delegate that returns a custom editor. The NavigableDoubleSpinBoxDelegate returns an editor of the type NavigableDoubleSpinBox.
class NavigableDoubleSpinBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
NavigableDoubleSpinBoxDelegate
(QObject * parent
= nullptr
) : QStyledItemDelegate
(parent
) {}};
{
public:
protected:
virtual void keyPressEvent
(QKeyEvent * event
) override;
};
class NavigableDoubleSpinBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
NavigableDoubleSpinBoxDelegate(QObject * parent = nullptr) : QStyledItemDelegate(parent) {}
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,const QModelIndex &index) const;
};
class NavigableDoubleSpinBox : public QDoubleSpinBox
{
public:
NavigableDoubleSpinBox(QWidget * parent = nullptr) : QDoubleSpinBox(parent) {}
protected:
virtual void keyPressEvent(QKeyEvent * event) override;
};
To copy to clipboard, switch view to plain text mode
Here is where the custom editor is provided:
{
return new NavigableDoubleSpinBox(parent);
}
QWidget * NavigableDoubleSpinBoxDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
return new NavigableDoubleSpinBox(parent);
}
To copy to clipboard, switch view to plain text mode
The custom editor is set to ignore key ups, downs, lefts and rights and pass every other key to it's parent event handler.
void NavigableDoubleSpinBox
::keyPressEvent(QKeyEvent * event
) {
Logger::getLogger().log(Logger::LOG_DEBUG, "Received keyPressEvent");
if (event->key() == Qt::Key_Up
|| event->key() == Qt::Key_Down
|| event->key() == Qt::Key_Left
|| event->key() == Qt::Key_Right)
{
return; // Ignore event;
}
}
void NavigableDoubleSpinBox::keyPressEvent(QKeyEvent * event)
{
Logger::getLogger().log(Logger::LOG_DEBUG, "Received keyPressEvent");
if (event->key() == Qt::Key_Up
|| event->key() == Qt::Key_Down
|| event->key() == Qt::Key_Left
|| event->key() == Qt::Key_Right)
{
return; // Ignore event;
}
QDoubleSpinBox::keyPressEvent(event);
}
To copy to clipboard, switch view to plain text mode
And finally, of course I set the delegate to the column:
NavigableDoubleSpinBoxDelegate * navigableDelegate = new NavigableDoubleSpinBoxDelegate(this);
ui->pairsTable->setItemDelegateForColumn(MomentValuePairTableModel::COL_VALUE, navigableDelegate);
NavigableDoubleSpinBoxDelegate * navigableDelegate = new NavigableDoubleSpinBoxDelegate(this);
ui->pairsTable->setItemDelegateForColumn(MomentValuePairTableModel::COL_VALUE, navigableDelegate);
To copy to clipboard, switch view to plain text mode
Unfortunately, what this code accomplishes is that it removes the arrow key functionality for controlling the spinbox. I still don't get the behavior I want -- that the arrows control the position of the selected cell in the QTableView. At this point, is the event supposed to propagate to the QTableView? Am I missing a step somewhere?
Bookmarks