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. Icon in TableView
Qt 6.11 is out! See what's new in the release blog

Icon in TableView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 2.4k 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by
    #1

    Hello,
    i'm trying to set an icon depending on its value as a delegate in a table view.
    I can draw the icon but, i have no clue how to change it depending on its value.

            TableView {
                id: tableView
                anchor.fill: parent
    
                TableViewColumn {
                    role: "type"
                    title: "Typ"
                    width: 100
    
                    delegate: Image {
                        fillMode: Image.PreserveAspectFit
                        source:  "qrc:/images/exclamation.png"
                        }
                }
            }
    

    The doc says:

    In the delegate you have access to the following special properties: styleData.value - the value or text for this item

    But how can i combine it with the icon?

    Thanks!

    Julien BJ 1 Reply Last reply
    0
    • beeckscheB beecksche

      Hello,
      i'm trying to set an icon depending on its value as a delegate in a table view.
      I can draw the icon but, i have no clue how to change it depending on its value.

              TableView {
                  id: tableView
                  anchor.fill: parent
      
                  TableViewColumn {
                      role: "type"
                      title: "Typ"
                      width: 100
      
                      delegate: Image {
                          fillMode: Image.PreserveAspectFit
                          source:  "qrc:/images/exclamation.png"
                          }
                  }
              }
      

      The doc says:

      In the delegate you have access to the following special properties: styleData.value - the value or text for this item

      But how can i combine it with the icon?

      Thanks!

      Julien BJ Offline
      Julien BJ Offline
      Julien B
      wrote on last edited by
      #2

      Hello @beecksche,

      What is the test you want to do on styleData.value to change your icon?

      beeckscheB 1 Reply Last reply
      0
      • Julien BJ Julien B

        Hello @beecksche,

        What is the test you want to do on styleData.value to change your icon?

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @Julien-B

        In that role an enum / int is stored. So i would set the source of the image depending on the value:

        switch (styleData.value){
        case Type.Info: image.source = "qrc:/images/info.png"; break;
        case Type.Warning: image.source = "qrc:/images/warning.png"; break;
        case Type.Error: image.source = "qrc:/images/error.png"; break;
        }
        
        
        Julien BJ 1 Reply Last reply
        0
        • beeckscheB beecksche

          @Julien-B

          In that role an enum / int is stored. So i would set the source of the image depending on the value:

          switch (styleData.value){
          case Type.Info: image.source = "qrc:/images/info.png"; break;
          case Type.Warning: image.source = "qrc:/images/warning.png"; break;
          case Type.Error: image.source = "qrc:/images/error.png"; break;
          }
          
          
          Julien BJ Offline
          Julien BJ Offline
          Julien B
          wrote on last edited by Julien B
          #4

          @beecksche

          I think you can create a function (for example defined in tableView)

          function provideIcon(int enumValue) {
              switch (enumValue) {
                  case Type.Info: return "qrc:/images/info.png";
                  case Type.Warning: return "qrc:/images/warning.png";
                  case Type.Error: return "qrc:/images/error.png";
              }
          }
          

          and use it in your delegate:

           delegate: Image {
              fillMode: Image.PreserveAspectFit
              source: tableView.provideIcon(styleData.value)
          }
          
          beeckscheB 1 Reply Last reply
          1
          • Julien BJ Julien B

            @beecksche

            I think you can create a function (for example defined in tableView)

            function provideIcon(int enumValue) {
                switch (enumValue) {
                    case Type.Info: return "qrc:/images/info.png";
                    case Type.Warning: return "qrc:/images/warning.png";
                    case Type.Error: return "qrc:/images/error.png";
                }
            }
            

            and use it in your delegate:

             delegate: Image {
                fillMode: Image.PreserveAspectFit
                source: tableView.provideIcon(styleData.value)
            }
            
            beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by
            #5

            @Julien-B
            It's that simple. Thanks a lot. Works perfectly!

            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