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. Rounded Image in Qt6
QtWS25 Last Chance

Rounded Image in Qt6

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 6 Posters 3.7k 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.
  • Marko StankeM Offline
    Marko StankeM Offline
    Marko Stanke
    wrote on last edited by
    #1

    Qt switched to MultiEffect for all effects of image processing.
    And I can't manage to make a working example for an image that has rounded corners.
    The "mask" property is not working as expected, or I'm just doing something wrong.

    If anyone could post an example code of an Image with rounded corners I'd really appreciate it. (By using only Qt6)

    For referrence I'm posting a working example of it in Qt5:

    Image{
                id: profileImage
                source: contactData.imageURL
                Layout.preferredHeight: parent.height
                Layout.preferredWidth: height
                asynchronous: true
                fillMode: Image.PreserveAspectCrop
                layer.enabled: true
                layer.effect: OpacityMask{
                    maskSource: Item{
                        width: profileImage.width
                        height: profileImage.height
                        Rectangle{
                            anchors.centerIn: parent
                            width: Math.min(profileImage.width, profileImage.height)
                            height: width
                            radius: 50
                        }
                    }
                }
            }
    
    1 Reply Last reply
    0
    • I Offline
      I Offline
      imahgin
      wrote on last edited by
      #2

      The easyest way in Qt6 would be using the Qt5Compat.GraphicalEffects.

      This is how to use it:

      import QtQuick
      import Qt5Compat.GraphicalEffects
      
      Item {
          anchors.fill: parent
      
              Image {
                  id: img
                  source: 'your image path'
                  width: parent.width/2
                  height: width
                  anchors.centerIn: parent
                  fillMode: Image.PreserveAspectCrop
                  layer.enabled: true
                  layer.effect: OpacityMask {
                      maskSource: mask
                  }
              }
          
              Rectangle {
                  id: mask
                  width: parent.width/2
                  height: width
                  radius: width/2
                  visible: false
              }
      }
      
      
      GrecKoG 1 Reply Last reply
      0
      • I imahgin

        The easyest way in Qt6 would be using the Qt5Compat.GraphicalEffects.

        This is how to use it:

        import QtQuick
        import Qt5Compat.GraphicalEffects
        
        Item {
            anchors.fill: parent
        
                Image {
                    id: img
                    source: 'your image path'
                    width: parent.width/2
                    height: width
                    anchors.centerIn: parent
                    fillMode: Image.PreserveAspectCrop
                    layer.enabled: true
                    layer.effect: OpacityMask {
                        maskSource: mask
                    }
                }
            
                Rectangle {
                    id: mask
                    width: parent.width/2
                    height: width
                    radius: width/2
                    visible: false
                }
        }
        
        
        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        @imahgin There's MultiEffect in Qt 6 : https://doc.qt.io/qt-6/qml-qtquick-effects-multieffect.html

        I 1 Reply Last reply
        0
        • GrecKoG GrecKo

          @imahgin There's MultiEffect in Qt 6 : https://doc.qt.io/qt-6/qml-qtquick-effects-multieffect.html

          I Offline
          I Offline
          imahgin
          wrote on last edited by imahgin
          #4

          @GrecKo Good point.
          If background can be a solid color, one can also use Canvas with "source-out" option. Not sure about performance compared with MultiEffect, though

          1 Reply Last reply
          0
          • Marko StankeM Offline
            Marko StankeM Offline
            Marko Stanke
            wrote on last edited by
            #5

            As I said, the Qt5 example works. I would appreciate a Qt6 example using MultiEffect.

            Tried it myself, but I just can't get it to work. This is my code:

            Item {
                        id: _avatarRoot
            
                        Image {
                            id: _avatar
            
                            anchors.fill: parent
            
                            visible: source !== ""
                            asynchronous: true
                            fillMode: Image.PreserveAspectCrop
                            source: _root.sourceImage
                        }
            
                        MultiEffect {
                            source: _avatar
                            anchors.fill: _avatar
                            maskEnabled: true
                            maskSource: Rectangle {
                                radius: 20
                                height: _avatarRoot.height
                                width: _avatarRoot.width
                                visible: false
                            }
            
                            maskSpreadAtMin: 0.4
                            maskThresholdMin: 0.6
                        }
                    }
            
            1 Reply Last reply
            0
            • K Offline
              K Offline
              keith pham
              wrote on last edited by
              #6

              try this:

              Image {
                      id: sourceItem
                      source: ""
                      anchors.centerIn: parent
                      width: 300
                      height: 300
                      visible: false
                  }
              
                  MultiEffect {
                      source: sourceItem
                      anchors.fill: sourceItem
                      maskEnabled: true
                      maskSource: mask
                  }
              
                  Item {
                      id: mask
                      width: sourceItem.width
                      height: sourceItem.height
                      layer.enabled: true
                      visible: false
              
                      Rectangle {
                          width: sourceItem.width
                          height: sourceItem.height
                          radius: width/2
                          color: "black"
                      }
                  }
              

              maskSource : Item

              Source item for the mask effect. Should point to ShaderEffectSource, item with layer.enabled set to true, or to an item that can be directly used as a texture source (e.g. Image). The alpha channel of the source item is used for masking.

              Marko StankeM 1 Reply Last reply
              2
              • K keith pham

                try this:

                Image {
                        id: sourceItem
                        source: ""
                        anchors.centerIn: parent
                        width: 300
                        height: 300
                        visible: false
                    }
                
                    MultiEffect {
                        source: sourceItem
                        anchors.fill: sourceItem
                        maskEnabled: true
                        maskSource: mask
                    }
                
                    Item {
                        id: mask
                        width: sourceItem.width
                        height: sourceItem.height
                        layer.enabled: true
                        visible: false
                
                        Rectangle {
                            width: sourceItem.width
                            height: sourceItem.height
                            radius: width/2
                            color: "black"
                        }
                    }
                

                maskSource : Item

                Source item for the mask effect. Should point to ShaderEffectSource, item with layer.enabled set to true, or to an item that can be directly used as a texture source (e.g. Image). The alpha channel of the source item is used for masking.

                Marko StankeM Offline
                Marko StankeM Offline
                Marko Stanke
                wrote on last edited by
                #7

                @keith-pham it works! Thank you so much. Marking it as solved.

                1 Reply Last reply
                0
                • Marko StankeM Marko Stanke has marked this topic as solved on
                • mzimmersM mzimmers referenced this topic on
                • G Offline
                  G Offline
                  Gilberrt
                  wrote on last edited by
                  #8

                  Your solution works! Thanks a lot.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist referenced this topic on
                  • J Offline
                    J Offline
                    Jacoco
                    wrote on last edited by
                    #9

                    This has sorta worked for me, but there is what I would consider to be a major problem. There doesn't seem to be any antialiasing, meaning that curved edges are jagged and don't look that good. Setting layer.smooth to true on the mask item does help a little bit, but it's still not as good as OpacityMask which didn't have any antialiasing problems

                    I've been trying a lot but I can't seem to get it to work properly. It's seemingly related to this where the layer property causes issues, but I haven't gotten the solutions from there to work here no matter which layer properties I set in which elements

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jacoco
                      wrote on last edited by Jacoco
                      #10

                      To anyone seeing this in the future who has the same problem as me with the jagged edges: The problem seems to actually be caused by the smoothstep that the Qt6 MultiEffect uses but the Qt5 OpacityMask doesn't. This is used for the maskThreshold- and maskSpreadAt- properties of MultiEffect. I don't really understand how these properties work, but by recreating this part of the shader in my own project I found out that if the first two elements of mask are about 0 and 1 respectively, the edges will be smooth. By looking into how these values are calculated, I found that setting maskThresholdMin to 0.5 and maskSpreadAtMin to 1.0 reaches basically this and the edges will then be smooth enough

                      TLDR: Set these values in the MultiEffect:
                      maskThresholdMin: 0.5
                      maskSpreadAtMin: 1.0

                      Edit: And just to clarify, layer.smooth: true also has to be set on the mask item

                      1 Reply Last reply
                      3

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved