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. How to access textRole of combobox in different files

How to access textRole of combobox in different files

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 1.1k 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.
  • E Offline
    E Offline
    eswar
    wrote on last edited by eswar
    #1

    Hi

    I used the Qt qml combox style example in the page link text.

    I created the above code in MyCombobox.qml and assign the following property to access different files

    property alias cbx_textRole:control.textRole
    property alias cbx_model: control.model
    In main.qml

    MyComboboxQML{
    cbx_model: nameModel
    cbx_textRole:"Role"
    }

    it will produce the attached result
    0_1551865218019_combo.png

    delegate: ItemDelegate {
    width: control.width
    contentItem: Text {
    text: cbx_textRole
    color: "#21be2b"
    font: control.font
    elide: Text.ElideRight
    verticalAlignment: Text.AlignVCenter
    }
    highlighted: control.highlightedIndex === index
    }

    if assign the alias of text roles names, it will produce the similar attached result

    How can i assign the c++ abstractmodel roles to the itemDelegate text ?

    Gojir4G 1 Reply Last reply
    0
    • JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      Admin: This question might be more appropriately placed in the QML forum....?

      Christian EhrlicherC 1 Reply Last reply
      0
      • JonBJ JonB

        Admin: This question might be more appropriately placed in the QML forum....?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @JonB Done :)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • E eswar

          Hi

          I used the Qt qml combox style example in the page link text.

          I created the above code in MyCombobox.qml and assign the following property to access different files

          property alias cbx_textRole:control.textRole
          property alias cbx_model: control.model
          In main.qml

          MyComboboxQML{
          cbx_model: nameModel
          cbx_textRole:"Role"
          }

          it will produce the attached result
          0_1551865218019_combo.png

          delegate: ItemDelegate {
          width: control.width
          contentItem: Text {
          text: cbx_textRole
          color: "#21be2b"
          font: control.font
          elide: Text.ElideRight
          verticalAlignment: Text.AlignVCenter
          }
          highlighted: control.highlightedIndex === index
          }

          if assign the alias of text roles names, it will produce the similar attached result

          How can i assign the c++ abstractmodel roles to the itemDelegate text ?

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          Hi @eswar,

          I guess you need to implement roleNames() in your model, and then use the role you want from QML.

          //.h
          
          class CustomerModel : public QAbstractItemModel
          {
          ...
          public:
           enum CustomerRoles {
                  UidRole = Qt::UserRole + 1,
                  NameRole,
                  LastNameRole,
                  CompanyRole,
                  MailAddressRole,
              };
          
              QHash<int, QByteArray> roleNames() const;
          ...
          };
          
          //.cpp
          QHash<int, QByteArray> CustomerModel::roleNames() const
          {
              QHash<int, QByteArray> roles;
              roles[UidRole] = "uid";
              roles[NameRole] = "name";
              roles[LastNameRole] = "lastName";
              roles[CompanyRole] = "company";
              roles[MailAddressRole] = "email";
              return roles;
          }
          
          1 Reply Last reply
          0
          • E Offline
            E Offline
            eswar
            wrote on last edited by
            #5

            Hi @Gojir4

            Thanks for the reply. I have implement the above mentioned roles in my QAbstractmodel in C++ named as "Role" only. So only i can use the name as "Role" in textRole. But i can't access the model data in qml. It will display similar to the above attached image.

            Gojir4G 1 Reply Last reply
            0
            • E eswar

              Hi @Gojir4

              Thanks for the reply. I have implement the above mentioned roles in my QAbstractmodel in C++ named as "Role" only. So only i can use the name as "Role" in textRole. But i can't access the model data in qml. It will display similar to the above attached image.

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #6

              @eswar said in How to access textRole of combobox in different files:

              I have implement the above mentioned roles in my QAbstractmodel in C++ named as "Role" only. So only i can use the name as "Role" in textRole

              Sorry, I don't understand. Can you show code of your model ?

              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