Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Expose QComboBox to QML
Qt 6.11 is out! See what's new in the release blog

Expose QComboBox to QML

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    philk
    wrote on last edited by
    #1

    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());
    }
    @

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      Qnoobish
      wrote on last edited by
      #2

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

      In short, software is eating the world.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        philk
        wrote on last edited by
        #3

        5.1

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qnoobish
          wrote on last edited by
          #4

          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?

          In short, software is eating the world.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            philk
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              Would it be easier to use a QML combobox?
              http://qt-project.org/doc/qt-5.1/qtquickcontrols/qml-qtquick-controls1-combobox.html

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • P Offline
                P Offline
                philk
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jens
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    philk
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jens
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        philk
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved