Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to put Delegates at work in *.ui.qml files?
Qt 6.12 LTS is out! See what's new in the release blog

How to put Delegates at work in *.ui.qml files?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
fil
2 Posts 1 Posters 469 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.
  • E Offline
    E Offline
    ellipse
    wrote on last edited by ellipse
    #1

    So far, I managed to work on data selected from a ListView like that:

        //file: myList.qml
         ListView {
                ...
                delegate:  Item {
                    ...
                    MouseArea {
                        id: mouseArea
    
                        onClicked : doSomething(model.index, model.someData...)
                }
         }
    

    Now I want to split mylist.qml file in a myList.qml + myListForm.ui.qml pair.
    The problem I am facing is that the UI designer does not support function calls (doSomething), nor a can put the delegate in a property of MyListForm.ui.qml with a signal to be used by MyList.qml (the delegate does not even appear in the UI navigator panel).

    Am I trying to do it the wrong way? What is the proper way to handle delegate actions in UI files?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      ellipse
      wrote on last edited by ellipse
      #2

      This is the (admittedly messy) solution I've got so far.
      As I was unable to send a signal from UI file (altough QtQuick Designer accepts its declaration), I resorted to a variable registering the index of last clicked item:

      // file: SettingsPageForm.ui.qml
      Item {
      
          property int clickIndex: -1
          // ....
      
          ListView {
              // ...
              model: dbSettingsModel
      
              delegate:  Item {
                  // UI graphics stuff with model.name, model,value etc...
                  // ...
                  // the delegate sets the index, the handler will reset it to -1
                  MouseArea { onClicked: clickIndex = model.index }
              }
          }
      
      
      }
      
      

      The implementation logic watches for the change signal:

      // file: settingsPage.qml
      SettingsPageForm {
      
          ...
      
          function launchEditor( index )
          {
              // getRole( row, roleName) was added to C++ model because 
              // model properties are available inside the delegate only
              editor.title =    dbSettingsModel.getRole( index, "name"     )
              editor.value =    dbSettingsModel.getRole( index, "value"    )
              editor.unit =     dbSettingsModel.getRole( index, "unit"     )
              editor.minValue = dbSettingsModel.getRole( index, "minValue" )
              editor.maxValue = dbSettingsModel.getRole( index, "maxValue" )
              editor.open()
          }
      
          onClickIndexChanged:  {
              if ( clickIndex >= 0 )
                  launchEditor( clickIndex )
              clickIndex = -1
          }
      }
      
      

      I found no other way but to expand the C++ model (dbSettingsModel) with getRole( index, roleName), as model properties are not available outside delegates.
      Overall it works, but I feel I'm overcomplicating it, I fell there must be a much cleaner way taking advantage of delegates straight inside UI deisgner.

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