Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to access textRole of combobox in different files



  • 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 ?



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


  • Lifetime Qt Champion

    @JonB Done :)



  • 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;
    }
    


  • 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.



  • @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 ?


Log in to reply