QTableView tab focus on cell widget
-
Hello,
I have setup a QTableView with my own delegate to create a cell widget. This widget contains two QDateTimeEdit elements. My problem if I tab through date/time values of first widget is that the focus leave the cell after last time value, close the editor and enter the next cell.
How can I prevent this behavior and set focus to next QDateTimeEdit widget of the same cell?
-
Here the dual value editor that is instantiated in delegate.createEditor method with
@
QWidget * QStyledItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index )
{
return new EvaDualValEditor(new QDateTimeEdit(parent), new QDateTimeEdit(parent), parent);
}
@... and it looks like in table edit mode. After tabbing (cursor on blue marked) the editor is closed and the next cell is selected.
!http://andreas-franke.org/home/home@andreas-franke.org/public/dualValEditor.jpg(dual val editor)!@
// header
class EvaDualValEditor : public QWidget
{
Q_OBJECTpublic:
explicit EvaDualValEditor(QWidget* editor1, QWidget* editor2, QWidget *parent = 0);
~EvaDualValEditor();void setValues( QVariant& param1, QVariant& param2 );
protected:
private:
QHBoxLayout *horizontalLayout_2;
QLabel *label;
QWidget *wgt1;
QLabel *label_2;
QWidget *wgt2;void init();
};// implementation
EvaDualValEditor::EvaDualValEditor(QWidget* editor1, QWidget* editor2, QWidget* parent /= 0/)
: QWidget(parent)
, wgt1(editor1)
, wgt2(editor2)
{
init();
}EvaDualValEditor::~EvaDualValEditor()
{
}void EvaDualValEditor::init()
{setFocusPolicy (Qt::StrongFocus);
setFocusProxy(wgt1);
setTabOrder(wgt1, wgt2);setObjectName(QString::fromUtf8("EvaDualValEditor"));horizontalLayout_2 = new QHBoxLayout(this);
horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
horizontalLayout_2->setSpacing(1);
horizontalLayout_2->setContentsMargins(1, 1, 1, 1);label = new QLabel(this);
label->setObjectName(QString::fromUtf8("label"));horizontalLayout_2->addWidget(label);
wgt1->setObjectName(QString::fromUtf8("widget"));
horizontalLayout_2->addWidget(wgt1);
label_2 = new QLabel(this);
label_2->setObjectName(QString::fromUtf8("label_2"));horizontalLayout_2->addWidget(label_2);
wgt2->setObjectName(QString::fromUtf8("widget_2"));
horizontalLayout_2->addWidget(wgt2);
}
void EvaDualValEditor::setValues( QVariant& param1, QVariant& param2 )
{
if (wgt1 && !param1.isNull())
{
qobject_cast<QDateTimeEdit*>(wgt1)->setDateTime(param1.toDateTime());
}
if (wgt2 && !param2.isNull())
{
qobject_cast<QDateTimeEdit*>(wgt2)->setDateTime(param2.toDateTime());
}
}
@ -
you can reimplement "QStyledItemDelegate::eventFilter()":http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html#eventFilter and react on Tab and BackTab keys.
Note the delegate is installed as an event filter on the created editor widget by default. The widget parameter should be your editor widget which you returned in createEditor(). -
hi,
thank you for your response. But what I have to do in QStyledItemDelegate::eventFilter() when I react on the events above? The dual editor should be stay open and next QDateTimeEdit selected. I've tried a lot of things without success.
-
you can take a look into the Qt sources of QStyledItemDelegate::eventFilter(). There you can see that some signals are fired which result in the closing of the editor.
I especially mean these "signals":http://qt-project.org/doc/qt-4.8/qabstractitemdelegate.html#signals.
These are triggered by the default implementation of eventFilter() in the delegate.