ComboBox: fix default delegate
ClosedPublic

Authored by apol on Jan 16 2019, 3:19 PM.

Details

Summary

Should use the selected item when it's interacted with using the mouse.

Test Plan

Selecting an item from the test combo box works now

Diff Detail

Repository
R858 Qt Quick Controls 2: Desktop Style
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
apol created this revision.Jan 16 2019, 3:19 PM
Restricted Application added a project: Plasma. · View Herald TranscriptJan 16 2019, 3:19 PM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
apol requested review of this revision.Jan 16 2019, 3:19 PM
davidedmundson accepted this revision.Jan 16 2019, 3:20 PM
This revision is now accepted and ready to land.Jan 16 2019, 3:20 PM
This revision was automatically updated to reflect the committed changes.
broulik added inline comments.
org.kde.desktop/ComboBox.qml
58

Are you sure this is needed? Qt docs say for that using ItemDelegate for a ComboBox is recommended as:
"This ensures that the interaction works as expected, and the popup will automatically close when appropriate. "
The currentIndex change makes sense, though

apol added inline comments.Jan 19 2019, 5:55 AM
org.kde.desktop/ComboBox.qml
58

otherwise it didn't work. I may have overlooked something though.

I checked Qt source code and Qt indeed should handle this on its own, it also works fine hwere without the code.
Worse, the explicit assignment from QML breaks any binding set on currentIndex.
In the handler that's called when the delegate model creates a delegate it does the following

void QQuickComboBoxPrivate::createdItem(int index, QObject *object)
{
    ...
    QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
    if (button) {
        button->setFocusPolicy(Qt::NoFocus);
        connect(button, &QQuickAbstractButton::clicked, this, &QQuickComboBoxPrivate::itemClicked);
        connect(button, &QQuickAbstractButton::hoveredChanged, this, &QQuickComboBoxPrivate::itemHovered);
    }

ItemDelegate is an AbstractButton and in itemClicked it sets the currentIndex and emits activated.