PDA

View Full Version : QLineEdit with mouse wheel: setModelData is called too late after commitData



ctgrund
11th February 2014, 21:27
Hi,

In a tree view I would like a QLineEdit where I can change the value with the mouse wheel.
The following idea works fine when I slowly scroll the wheel. But when I faster scroll the wheel sometimes I am missing a call to setModelData. This function is then only called when I click somewhere else in the tree view.

Thank you!



QLineMouseEdit::QLineMouseEdit(QWidget *aParent): QLineEdit(aParent) {
setFocusPolicy(Qt::WheelFocus);
}

void QLineMouseEdit::wheelEvent(QWheelEvent *aEvent) {
double LinearChange = 1;
if (LinearChange!=0) {
double Delta = aEvent->delta();
double OldValue = text().toDouble();
double NewValue = OldValue + Delta/120*LinearChange;
setText(QString::number(NewValue));
qDebug() << "";
qDebug() << "wheelEvent emit changed" << NewValue; // ok!!
emit changed();
return;
}
}

QWidget *CModelTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QLineMouseEdit *Editor = new QLineMouseEdit(parent);
connect(Editor, SIGNAL(changed()), this, SLOT(changed()));
return Editor;
}

void CModelTreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
qDebug() << "CModelTreeDelegate::setModelData"; // ??? sometimes too late ???
QItemDelegate::setModelData(editor, model, index);
}

void CModelTreeDelegate::changed() {
QLineMouseEdit *Editor = qobject_cast<QLineMouseEdit *>(sender());
if (Editor) {
qDebug() << "CComboDelegate::changed emit commitData"; // ok!!
emit commitData(Editor);
}
}

Infinity
11th February 2014, 22:56
It seems that the data you're dealing with is a decimal number. Why don't you use a QDoubleSpinBox instead of a QLineEdit.
If I properly understood what you want to achieve using a QDoubleSpinBox will solve your problem because it supports changing the value using the mouse wheel out of the box. To achieve the step to be done like you need it to be done I suggest overwriting void QAbstractSpinBox::stepBy(int steps).

ctgrund
12th February 2014, 21:46
@Infinity: your approach does really fit my situation much more!
But: still got update problems when I scroll the mouse too quickly.

Thanks anyway!

Infinity
12th February 2014, 22:06
still got update problems when I scroll the mouse too quickly
Maybe we are able to help if you post your code.

ctgrund
12th February 2014, 22:29
@Infinity: I definitly will post some code. I 'only' have to strip down the code to some short example.
Thanks for your help!