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. Can I specify the ListView's delegate text from outside the ListView?
Qt 6.11 is out! See what's new in the release blog

Can I specify the ListView's delegate text from outside the ListView?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I need to create a ListView with very specific formatting requirements in multiple places around my application. So it seems to make sense to create a Qml to define it:

    @MyListView.qml
    ListView
    {
    // interesting formatting goes here
    delegate: Text
    {
    text: itemNameInTheModel
    // more interesting formatting goes here
    }
    }@

    But then when I want to instantiate a MyListView, I can specify the model, but what if the model is outside my control and doesn't contain an item named "itemNameInTheModel"?

    I want to be able to say:

    @MyWindow.qml
    Rectangle
    {
    MyListView
    {
    id: accountList
    model: myData
    delegate.text: accountNumber
    }
    }@

    Is something like this possible?

    Thanks,
    Chris

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      You can try something like this. It may help you.

      @Rectangle {
      width: 200;height: 500;color :"red"
      property variant role1;
      property variant role2;
      Component{
      id : del
      Item {
      height: 50;width: 200
      Row {
      Text { text : role1}
      Text { text : role2}
      }
      }
      }

      ListView {
          id : view
          anchors.fill: parent
          model : MyModel {id :mymodel }
          delegate: del
      }
      Timer {
          id : tim
          interval: 5000
          running: true
          repeat: true
          property int count : 10
          onTriggered: {
              role1 = "cost1";
              role2 = "name1";
              mymodel.append({role1: count, role2:"Pizza"})
              count++;
          }
      }
      

      }

      ====MyModel.qml===
      ListModel {
      id :mod
      dynamicRoles: true
      }
      @

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

      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