328bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
329 QAbstractItemView *view,
330 const QStyleOptionViewItem &option,
331 const QModelIndex &index)
337 switch (event->type()) {
338#if QT_CONFIG(tooltip)
339 case QEvent::ToolTip: {
340 Q_D(QAbstractItemDelegate);
341 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
342 const int precision = inherits(
"QItemDelegate") ? 10 : 6;
343 const QString tooltip = index.isValid() ?
344 d->textForRole(Qt::ToolTipRole, index.data(Qt::ToolTipRole), option.locale, precision) :
346 QToolTip::showText(he->globalPos(), tooltip, view->viewport(), option.rect);
347 event->setAccepted(!tooltip.isEmpty());
351#if QT_CONFIG(whatsthis)
352 case QEvent::QueryWhatsThis:
353 event->setAccepted(index.data(Qt::WhatsThisRole).isValid());
355 case QEvent::WhatsThis: {
356 Q_D(QAbstractItemDelegate);
357 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
358 const int precision = inherits(
"QItemDelegate") ? 10 : 6;
359 const QString whatsthis = index.isValid() ?
360 d->textForRole(Qt::WhatsThisRole, index.data(Qt::WhatsThisRole), option.locale, precision) :
362 QWhatsThis::showText(he->globalPos(), whatsthis, view);
363 event->setAccepted(!whatsthis.isEmpty());
371 return event->isAccepted();
455bool QAbstractItemDelegatePrivate::handleEditorEvent(QObject *object, QEvent *event)
457 Q_Q(QAbstractItemDelegate);
459 QWidget *editor = qobject_cast<QWidget*>(object);
462 if (event->type() == QEvent::KeyPress) {
463 QKeyEvent *keyEvent =
static_cast<QKeyEvent *>(event);
464 if (editorHandlesKeyEvent(editor, keyEvent))
467#ifndef QT_NO_SHORTCUT
468 if (keyEvent->matches(QKeySequence::Cancel)) {
470 emit q->closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
475 switch (keyEvent->key()) {
477 if (tryFixup(editor)) {
478 emit q->commitData(editor);
479 emit q->closeEditor(editor, QAbstractItemDelegate::EditNextItem);
482 case Qt::Key_Backtab:
483 if (tryFixup(editor)) {
484 emit q->commitData(editor);
485 emit q->closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
493 if (!tryFixup(editor))
496 QMetaObject::invokeMethod(q,
"_q_commitDataAndCloseEditor",
497 Qt::QueuedConnection, Q_ARG(QWidget*, editor));
502 }
else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
504 if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {
505 QWidget *w = QApplication::focusWidget();
509 w = w->parentWidget();
511#if QT_CONFIG(draganddrop)
514 QPlatformDrag *platformDrag = QGuiApplicationPrivate::instance()->platformIntegration()->drag();
515 if (platformDrag && platformDrag->currentDrag()) {
519 if (tryFixup(editor))
520 emit q->commitData(editor);
525 QWidget *editorParent = editor->parentWidget();
526 const bool manuallyFixFocus = (event->type() == QEvent::FocusOut) && !editor->hasFocus() &&
528 (
static_cast<QFocusEvent *>(event)->reason() == Qt::ActiveWindowFocusReason);
529 emit q->closeEditor(editor, QAbstractItemDelegate::NoHint);
530 if (manuallyFixFocus)
531 editorParent->setFocus();
533#ifndef QT_NO_SHORTCUT
534 }
else if (event->type() == QEvent::ShortcutOverride) {
535 if (
static_cast<QKeyEvent*>(event)->matches(QKeySequence::Cancel)) {
544bool QAbstractItemDelegatePrivate::tryFixup(QWidget *editor)
546#if QT_CONFIG(lineedit)
547 if (QLineEdit *e = qobject_cast<QLineEdit*>(editor)) {
548 if (!e->hasAcceptableInput()) {
549#if QT_CONFIG(validator)
550 if (
const QValidator *validator = e->validator()) {
551 QString text = e->text();
552 validator->fixup(text);
556 return e->hasAcceptableInput();
560#if QT_CONFIG(spinbox)
563 if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox *>(editor)) {
564 if (!sb->keyboardTracking())
574QString QAbstractItemDelegatePrivate::textForRole(Qt::ItemDataRole role,
const QVariant &value,
const QLocale &locale,
int precision)
const
576 const QLocale::FormatType formatType = (role == Qt::DisplayRole) ? QLocale::ShortFormat : QLocale::LongFormat;
578 switch (value.userType()) {
579 case QMetaType::Float:
580 text = locale.toString(value.toFloat());
582 case QMetaType::Double:
583 text = locale.toString(value.toDouble(),
'g', precision);
586 case QMetaType::LongLong:
587 text = locale.toString(value.toLongLong());
589 case QMetaType::UInt:
590 case QMetaType::ULongLong:
591 text = locale.toString(value.toULongLong());
593 case QMetaType::QDate:
594 text = locale.toString(value.toDate(), formatType);
596 case QMetaType::QTime:
597 text = locale.toString(value.toTime(), formatType);
599 case QMetaType::QDateTime:
600 text = locale.toString(value.toDateTime(), formatType);
602 case QMetaType::QJsonValue: {
603 const QJsonValue val = value.toJsonValue();
605 text = QVariant(val.toBool()).toString();
608 if (val.isDouble()) {
609 text = locale.toString(val.toDouble(),
'g', precision);
616 text = value.toString();
617 if (role == Qt::DisplayRole)
618 text.replace(u'\n', QChar::LineSeparator);
625void QAbstractItemDelegatePrivate::_q_commitDataAndCloseEditor(QWidget *editor)
627 Q_Q(QAbstractItemDelegate);
628 emit q->commitData(editor);
629 emit q->closeEditor(editor, QAbstractItemDelegate::SubmitModelCache);