Expose QComboBox to QML



  • This code initially somewhat draws the combobox in my QML document. But it does not seem to react on clicks or keyboard events. The paint handler is called only once.

    @
    class CountryComboBoxQmlAdapter : public QQuickPaintedItem {
    Q_OBJECT
    public:
    explicit CountryComboBoxQmlAdapter(QQuickItem* parent = nullptr);

    void paint(QPainter * painter) override;
    private:
    QComboBox* m_combo;
    QGraphicsProxyWidget* m_proxy;
    };
    @

    @

    CountryComboBoxQmlAdapter::CountryComboBoxQmlAdapter(QQuickItem* parent) : QQuickPaintedItem(parent) {
    setFlag(ItemHasContents, true);
    m_combo = new QComboBox;
    m_combo->setEditable(true);
    m_combo->setFrame(false);

    CountriesListModel* model = new CountriesListModel(this);
    m_combo->setModel(model);
    m_proxy = new QGraphicsProxyWidget;
    m_proxy->setWidget(m_combo);
    }

    void CountryComboBoxQmlAdapter::paint(QPainter * painter) {
    QStyleOptionGraphicsItem style;
    style.exposedRect = this->boundingRect();
    m_proxy->paint(painter, &style, m_proxy->widget());
    }
    @



  • What version of Qt are you using? Qt 5 ??



  • 5.1



  • Sorry that i make another question but in your QML are you importing qtquick2 or qtquick1?

    In case of Qtquick2, I am not sure that a QGraphicsProxyWidget works under Qt5 using QtQuickView classes to load your QML.

    But I think your project builds and run so could you please post your code where you load your qml file and register this class?



  • qtquick2. I will create a runnable test project and post it here


  • Moderators



  • No, unfortunately not. This QML ComboBox is such a bad design. It uses a Popup Menu for the dropdown. Who would ever thought of that? Try to put a list of countries into a QML CB. Not only is it slow as hell, the menu it drops out is also unusable.



  • philk: Please file a proper bug or a feature if you are having issues with the qml ComboBox. There is no proper way to embed a widget in Qt Quick 2 so working around it that way is not recommended.

    I am not aware of any performance issues with using a popup window in the combo box so a proper bug report with platform and info would be good. (on mac those combo box popups are native)

    FYI I also started working on a patch against the dev branch that will enable an editable mode and initial keyboard-index support in ComboBox: https://codereview.qt-project.org/#change,63156



  • the QML2 combo box does not use a popup window, it uses a popup menu That is the problem.



  • philk: That is an implementation detail and does not describe an actual problem. The QComboBox widget also uses a QMenu to show the actual popup as part of its implementation.



  • Then this is a very awkward implementation on Qt side. Nowhere else (in Windows) a popup menu is used for Combo Dropdowns, for obvious reasons. Menus are not (easily) scrollable.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.