Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to fill QML Combobox on C++ side by ListElement
QtWS25 Last Chance

How to fill QML Combobox on C++ side by ListElement

Scheduled Pinned Locked Moved General and Desktop
qmlcomboboxc++qt5combobox model
3 Posts 2 Posters 11.3k Views
  • 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.
  • D Offline
    D Offline
    desperado_43
    wrote on last edited by desperado_43
    #1

    Hi Everybody. I write a code which fill combobox from C++ side. My code is Ok by using QStringList

    class ComboboxUpdate:public  QObject
    {
        Q_OBJECT
        Q_PROPERTY(QStringList comboList READ comboList)
    
    public:
    
        ComboboxUpdate(QObject *parent = 0);
    
    
        const QStringList comboList();
        void setComboList( const QStringList &comboList); 
    ......
    

    Qml part is:

       ComboBox {
            
          model:combomodel.comboList
    ......
    

    I don't want to put details here.I can fill it from c++ side with code blocks above.But I want to fill with ListElement not only text For Exaplem qml:

       ComboBox {
                 ListModel {
              ListElement {text:"Orange" ;sqlid:1;}
                ListElement {text:"Apple"  ;sqlid:2;}
               ListElement {text:"Greeps";sqlid:3;}
           }
    

    How to pass stuff like that to combobox from c++ side.Because sqlid is necessary for me.I use them on OnCurrrentTextChange event that put the sqlid in Javascript code.Any ideas ? Thank you ....

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mike_student
      wrote on last edited by mike_student
      #2

      So assuming your QObject subclassed class has a property say for eg. text then to populate ComboBox with it you will have to set textRole to 'text'

      ComboBox {        
            model: combomodel.comboList
            textRole: "text"
       }
      
      

      after that you can get your sqlid attribute from model

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mike_student
        wrote on last edited by
        #3

        You need to change QStringList to QList<QObject*>
        From

        class ComboboxUpdate:public  QObject
        {
            Q_OBJECT
            Q_PROPERTY(QStringList comboList READ comboList)
        ...
        }
        

        to

        class ComboboxUpdate:public  QObject
        {
            Q_OBJECT
            Q_PROPERTY(QList<QObject*> comboList READ comboList)
        }
        

        for example you could change your code in this way or add a subclass

        class ComboboxUpdate:public  QObject
        {
            Q_OBJECT
            Q_PROPERTY(QStringList comboList READ comboList)
            Q_PROPERTY(int sqlid READ sqlid WRITE setSqlid NOTIFY sqlidChanged)
            Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
        }
        

        and in ComboBox you can use

        ComboBox {
                            id: typeComboBox
                            model:combomodel.comboList
                            textRole: "text"
                            onCurrentIndexChanged: {
                                var sqlid = typeComboBox.model[typeComboBox.currentIndex].sqlid;
                                console.log(sqlid);
                            }
                        }
        

        I hope that my answer will be helpful for you.

        1 Reply Last reply
        1

        • Login

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