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

Using subscripts in text inside comboBox



  • Hi,

    I use the QuickControls2 ComboBox in the Material style. I wanted to know whether it is possible, to use rich text for the single items, e.g. have subscripts via <sub>1</sub>. If I simply add this to the text, the <sub> are also printed in the text.

    Doing my own delegate might destroy the Material style, which I want to avoid



  • Alternateively: If I need to do my own delegate, where would I find the Material style source file, so that I could copy the delegate from there?





  • If I understand this right, a problem with trying to copy the material style delegates for e.g. the popup is that things like ElevationEffect are in the implementation and thus simply copying the Material style does not work, right?



  • Hi @maxwell31 ,

    • You can customize the text inside the ComboBox and provide the RichText for the text format.
      For example like this:-

      textFormat: Text.RichText
      
    • To give the text Material colors, you need to provide the colors like this:-

      color: Material.color(Material.Pink)
      

    Here is the sample code:-

    ComboBox {
            id: control
    
            width: 100
            height: 100
            model: ["x<sub>2</sub>" ,"x<sub>4</sub>","x<sub>3</sub>","x<sub>3</sub>"]
    
            contentItem: Text {
                    leftPadding: 10
                    text: control.displayText
                    font: control.font
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                    textFormat: Text.RichText
                }
    
            delegate: ItemDelegate {
                   width: control.width
                   contentItem: Text {
                       text: modelData
                       color: Material.color(Material.Pink)
                       font: control.font
                       elide: Text.ElideRight
                       verticalAlignment: Text.AlignVCenter
                       textFormat: Text.RichText
                   }
                   highlighted: control.highlightedIndex === index
               }
        }
    

    Sample Output:-

    0_1559208483247_6a446b37-fb6b-4f69-a009-8c585e653116-image.png

    0_1559208542160_561efd40-4f24-4fa5-950c-1ef497ed3ae1-image.png



  • Hi,

    Thank you. I didn't see that I can change the delegate instead of changing popup!



  • A question though: Is there a simpler way to just set the textFormat property of the existing deleagte?



  • This post is deleted!

Log in to reply