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. [Solved] QML ComboBox model from C++
Forum Updated to NodeBB v4.3 + New Features

[Solved] QML ComboBox model from C++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 7.5k 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.
  • S Offline
    S Offline
    skammers
    wrote on last edited by
    #1

    Hey!

    I have a list from C++ containing language code, language description and a flag for the corresponding country.
    I made the connection to QML, but cant get to set the model right.
    I tried different approaches and either I get an empty ComboBox, or I get a ComboBox containing only one alternative, which is "0".

    This is the code for the ComboBox:

    @
    ComboBox{
    id: langSelection;
    editable: false;
    width: caText.width;
    model: LanguageList.languageList;

                    textRole:{
                        if(LanguageList.langList.langCode !== undefined){
                            LanguageList.langList.langCode;
                        }
    
                    }
    

    @

    1 Reply Last reply
    0
    • shavS Offline
      shavS Offline
      shav
      wrote on last edited by
      #2

      Hi,

      I'm not sure if this is correct but I use QVariantList with strings for ComboBox. For example:
      @
      QVariantList MslHelpers::keychainActionTypeList()
      {
      QVariantList list;
      list.clear();

      list.append("Unknown Action");
      list.append("Save Datas Action");
      list.append("Remove Datas By Kyes Action");
      
      return list;
      

      }
      @

      and used it like this:
      @
      ComboBox {
      id: keychaninActionBox
      width: 200
      model: manager.typeManager.keychainActionTypeList()
      }
      @

      Mac OS and iOS Developer

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        You can look at the examples given in <QtInstallDir>\examples\quick\models about exporting C++ models to QML ?

        Also ComboBox has 'model' property. You should be able to set this property by model which is exposed from C++.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • S Offline
          S Offline
          skammers
          wrote on last edited by
          #4

          Thanks for the replies, but I got it to work, I set it up like this:

          @
          ComboBox{
          id: comboBox;
          width: caText.width;
          model: languageList.langList;
          textRole: "langDescription";
          }
          @

          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