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. Qml ComboBox and current model item
Forum Updated to NodeBB v4.3 + New Features

Qml ComboBox and current model item

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.9k 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.
  • SiN_BizkitS Offline
    SiN_BizkitS Offline
    SiN_Bizkit
    wrote on last edited by
    #1

    Hi everyone.
    I have a question about Quick Controls 2 ComboBox (2.12), I have a key-value model inherited from QAbstractItemModel, basically it has 2 roles: one I use to display some printable data, other is the key. How can I fetch the key of the selected combobox item? I know that Controls 2.14 added valueRole property to the ComboBox control, but I can't upgrade Qt, at least for now.

    1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      currentIndex

      maybe i misunderstand but can't you just use the index. Because it's model driven, and the one and the same model...

      SiN_BizkitS 1 Reply Last reply
      0
      • 6thC6 6thC

        currentIndex

        maybe i misunderstand but can't you just use the index. Because it's model driven, and the one and the same model...

        SiN_BizkitS Offline
        SiN_BizkitS Offline
        SiN_Bizkit
        wrote on last edited by
        #3

        @6thC I mean I have key value model such as:
        "key-1" - "value-1"
        "key-2" - "value 2";
        I use value field as a "textRole" of the ComboBox, but if the currentIndex is changed I need to determine an Id of the selected item to proceed.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          assuming your model as an invokable get method returning a map of all the role values:

          property string valueRole
          readonly property var currentValue: valueRole && model ? model.get(currentIndex)[valueRole] : undefined
          
          SiN_BizkitS 1 Reply Last reply
          2
          • GrecKoG GrecKo

            assuming your model as an invokable get method returning a map of all the role values:

            property string valueRole
            readonly property var currentValue: valueRole && model ? model.get(currentIndex)[valueRole] : undefined
            
            SiN_BizkitS Offline
            SiN_BizkitS Offline
            SiN_Bizkit
            wrote on last edited by
            #5

            @GrecKo In case if I will manually extend my model's public interface in that way I will lost the possibility to chain models (e.g. set qsortfilterproxymodel in front of my model).
            I decided to expose my custom Roles to the qml part and fetch the data via QAbstractItemModel::data method by hand.

            function getModelKeyByRow(model, row) {
            	let modelIndex = model.index(row, 0);
            	return model.data(modelIndex, QtExtras.KeyValueModel.KeyRole);
            }
            

            I thought maybe there is some more convenient way :) But I'm absolutely fine with it right now. Someday I will upgrade Qt to 5.14 and the problem will disappear.
            Thank you folks!

            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