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. How to change background qt quick controls 2 combobox ?
Forum Updated to NodeBB v4.3 + New Features

How to change background qt quick controls 2 combobox ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 5 Posters 7.3k Views 2 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.
  • T Offline
    T Offline
    TheGringerEye
    wrote on last edited by TheGringerEye
    #1

    Hello all.
    I need help to change background color delegate of combobox.
    I tried change "Material.background" , but not change delegate background color(

    ComboBox
    {
        Material.background: "white"
        textRole: "key"
        model: ListModel
        {
            ListElement { key: "First"; value: 123 }
            ListElement { key: "Second"; value: 456 }
            ListElement { key: "Third"; value: 789 }
        }
    }
    

    Result:
    alt text
    alt text

    I try to learn English.

    M 1 Reply Last reply
    0
    • T TheGringerEye

      Hello all.
      I need help to change background color delegate of combobox.
      I tried change "Material.background" , but not change delegate background color(

      ComboBox
      {
          Material.background: "white"
          textRole: "key"
          model: ListModel
          {
              ListElement { key: "First"; value: 123 }
              ListElement { key: "Second"; value: 456 }
              ListElement { key: "Third"; value: 789 }
          }
      }
      

      Result:
      alt text
      alt text

      M Offline
      M Offline
      Mammamia
      wrote on last edited by
      #2

      @TheGringerEye
      You should customize the combo box control.
      refer this Customizing Combobox

      T 1 Reply Last reply
      1
      • M Mammamia

        @TheGringerEye
        You should customize the combo box control.
        refer this Customizing Combobox

        T Offline
        T Offline
        TheGringerEye
        wrote on last edited by
        #3

        @Mammamia I tried this, but were lost Material Design Effects such as Waves, ElevatorEffect and other.

        I try to learn English.

        E 1 Reply Last reply
        0
        • T TheGringerEye

          @Mammamia I tried this, but were lost Material Design Effects such as Waves, ElevatorEffect and other.

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @TheGringerEye said in How to change background qt quick controls 2 combobox ?:

          @Mammamia I tried this, but were lost Material Design Effects such as Waves, ElevatorEffect and other.

          I believe it shouldn't happen if you if replace only the delegate and not background etc.

          ComboBox {
          id: control
          delegate: MenuItem {
                  width: control.popup.width
                  text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
                  Material.foreground: control.currentIndex === index ? control.popup.Material.accent : control.popup.Material.foreground
                  highlighted: control.highlightedIndex === index
                  hoverEnabled: control.hoverEnabled
              }
          }
          

          Here the delegate is copied from my Qt installation Qt/5.8/[compilerversion]/qml/QtQuick/Controls.2/Material/ComboBox.qml. Try adding Material.background to the delegate.

          T 1 Reply Last reply
          0
          • E Eeli K

            @TheGringerEye said in How to change background qt quick controls 2 combobox ?:

            @Mammamia I tried this, but were lost Material Design Effects such as Waves, ElevatorEffect and other.

            I believe it shouldn't happen if you if replace only the delegate and not background etc.

            ComboBox {
            id: control
            delegate: MenuItem {
                    width: control.popup.width
                    text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
                    Material.foreground: control.currentIndex === index ? control.popup.Material.accent : control.popup.Material.foreground
                    highlighted: control.highlightedIndex === index
                    hoverEnabled: control.hoverEnabled
                }
            }
            

            Here the delegate is copied from my Qt installation Qt/5.8/[compilerversion]/qml/QtQuick/Controls.2/Material/ComboBox.qml. Try adding Material.background to the delegate.

            T Offline
            T Offline
            TheGringerEye
            wrote on last edited by
            #5

            @Eeli-K Not working :(

            I try to learn English.

            ekkescornerE E 2 Replies Last reply
            0
            • T TheGringerEye

              @Eeli-K Not working :(

              ekkescornerE Offline
              ekkescornerE Offline
              ekkescorner
              Qt Champions 2016
              wrote on last edited by ekkescorner
              #6

              @TheGringerEye here's my custom combo box
              perhaps gives you some ideas.
              ComboBoxWithIcon.qml:

              // this custom ComboBox works using a JS datamodel where the model is something like this:
              // [{"imageName":"download.png", "itemText":qsTr("Download")}, ...]
              // using same model as a QML ListModel you must change the delegate: model.imageName
              // and contentItem: listModel.get(comboBox.currentIndex).imageName
              ComboBox {
                  id: comboBox
                  focusPolicy: Qt.NoFocus
                  property bool containsInvalidItems: false
              
                  delegate: ItemDelegate {
                              width: comboBox.width
                              Material.foreground: comboBox.currentIndex === index ? comboBox.Material.accent : comboBox.Material.foreground
                              highlighted: comboBox.highlightedIndex === index
                              contentItem: Row {
                                  spacing: 6
                                  IconInactive {
                                      imageName: modelData.imageName
                                  }
                                  LabelSubheading {
                                      text: modelData.itemText
                                      anchors.verticalCenter: parent.verticalCenter
                                      font.strikeout: comboBox.containsInvalidItems && modelData.invalidItem
                                  }
                              }
                          }
              
                  contentItem:
                      Row {
                      // need some space to see the indicator
                      // need some extra space for the rows to display Icon and text if displayText is used
                      rightPadding: comboBox.displayText.length? 24+16 : 16
                      spacing: 6
                      IconInactive {
                          visible: !comboBox.displayText.length
                          imageName: comboBox.model[comboBox.currentIndex].imageName
                      }
                      LabelSubheading {
                          text: comboBox.displayText.length? comboBox.displayText : comboBox.model[comboBox.currentIndex].itemText
                          anchors.verticalCenter: parent.verticalCenter
                          font.strikeout: comboBox.displayText.length? false : comboBox.containsInvalidItems && comboBox.model[comboBox.currentIndex].invalidItem
                      }
                  } // row
              }
              

              and here's how I'm using this ComboBox:

              ComboBoxWithIcon {
                  id: selectToursDropDown
                  Layout.leftMargin: 6
                  Layout.rightMargin: 6
                  displayText: qsTr("Tour selection")
                  model: [
                      {"imageName":"schedule_my.png", "itemText":qsTr("Show my")},
                      {"imageName":"schedule.png", "itemText":qsTr("Show all")},
                      {"imageName":"directions.png", "itemText":qsTr("Transfer")},
                      {"imageName":"download.png", "itemText":qsTr("Download")}
                  ]
                  onCurrentIndexChanged: {
                      // do something
                  }
              }
              

              LabelSubheading and IconInactive itself are also customized controls.
              Take a look at my sample apps at github https://appbus.wordpress.com/category/qt-for-mobile/overview/

              ekke ... Qt Champion 2016 | 2024 ... mobile business apps
              5.15 --> 6.9 https://t1p.de/ekkeChecklist
              QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

              1 Reply Last reply
              0
              • T TheGringerEye

                @Eeli-K Not working :(

                E Offline
                E Offline
                Eeli K
                wrote on last edited by
                #7

                @TheGringerEye If the given examples don't work, you have to give more information. How the comboboxes are coded, created and used? Can you replicate the problem with a minimal app, created with the Qt Creator's wizard, where the combobox and the using code is in main.qml?

                T 1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Hi! Have a look at ComboBox.qml to see how the default implementation works. Should be easy to modify it to fit your needs.

                  1 Reply Last reply
                  0
                  • E Eeli K

                    @TheGringerEye If the given examples don't work, you have to give more information. How the comboboxes are coded, created and used? Can you replicate the problem with a minimal app, created with the Qt Creator's wizard, where the combobox and the using code is in main.qml?

                    T Offline
                    T Offline
                    TheGringerEye
                    wrote on last edited by
                    #9

                    @Eeli-K Background dont changed

                    I try to learn English.

                    E 1 Reply Last reply
                    0
                    • T TheGringerEye

                      @Eeli-K Background dont changed

                      E Offline
                      E Offline
                      Eeli K
                      wrote on last edited by
                      #10

                      @TheGringerEye So you want to change both the contentItem's and delegate's background to white? Can you paste the minimal app here?

                      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