0

Hi I have a DelegateChooser for a TableView with 10-20 different DelegateChoices. How can I apply the same background to all the choices? I want to avoid having to add the same background to all choices as that is a lot of repeater code and a maintenance headache:

DelegateChoice: {
   Item {
         Rectangle { id: background; anchors.fill: parent; color: "blue" }
         Choice1 {}
    }
    ...
   Item {
         Rectangle { id: background; anchors.fill: parent; color: "blue" }
         Choice20 {}
    }
}
pooya13
  • 2,060
  • 2
  • 23
  • 29

1 Answers1

1

First, the Items in your example do not serve a purpose - Rectangles are Items, only colored instead of transparent, making the top level Item a duplicate. Secondly, I would just make a new file MyBackground.qml as such:

import QtQuick 2.0

Rectangle {
    color: "blue"
    // any other necessary background properties here
}

Then you make your ChoiceN files inherit from MyBackground, for example:

// ChoiceN.qml file
import QtQuick 2.0

MyBackground  {
    // ChoiceN.qml contents here as normal
}

And your example code becomes:

DelegateChoice: {
    Choice1 {}
    ...
    Choice20 {}
}

Alternately, if you do not have access to your ChoiceN file contents, you can encapsulate them from outside as well:

DelegateChoice: {
    MyBackground {
        Choice1 {}
    }
    ...
    MyBackground {
        Choice20 {}
    }
}
fallerd
  • 1,512
  • 10
  • 15
  • Thank you. The only thing I don't like about this answer is that it makes my Choices non-reusable. I would rather keep my delegates as self contained reusable files and it does not make sense for reusable components to have set background. – pooya13 Apr 28 '21 at 06:00
  • But this seems like the best solution given the limitation of DelegateChooser. I would have preferred to somehow add the background to every delegate at once. – pooya13 Apr 28 '21 at 06:02