<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: LorenDB</title>
    <description>The latest articles on DEV Community by LorenDB (@lorendb).</description>
    <link>https://dev.to/lorendb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F537347%2Ffefa74be-f612-4d55-8a27-b663ff9476a5.jpeg</url>
      <title>DEV Community: LorenDB</title>
      <link>https://dev.to/lorendb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lorendb"/>
    <language>en</language>
    <item>
      <title>Replicating Microsoft Acrylic with QML</title>
      <dc:creator>LorenDB</dc:creator>
      <pubDate>Mon, 05 Apr 2021 19:21:07 +0000</pubDate>
      <link>https://dev.to/lorendb/replicating-microsoft-acrylic-with-qml-4jig</link>
      <guid>https://dev.to/lorendb/replicating-microsoft-acrylic-with-qml-4jig</guid>
      <description>&lt;p&gt;A while back, I got to thinking about the great look of &lt;a href="https://docs.microsoft.com/en-us/windows/uwp/design/style/acrylic" rel="noopener noreferrer"&gt;Microsoft Acrylic&lt;/a&gt;. Being a Linux user, I didn't actually have Acrylic available on my system, but I wanted to try to create something similar in QML. Unfortunately, I wasn't able to figure out how to use it as an app background material with the desktop showing through, but I did manage to create something that should be useful for creating impressive apps.&lt;/p&gt;

&lt;p&gt;The current implementation of QML Acrylic is not perfect. Putting items on top of the Acrylic can influence its appearance, and if the items are changing (think a blinking text cursor), the Acrylic will change with it. However, I hope to improve it. (Who knows? Maybe I'll have to post a second, improved Acrylic later on.)&lt;/p&gt;

&lt;p&gt;To start off, let's create a QML window that displays an image. Fire up Qt Creator (or whatever IDE you use) and create &lt;code&gt;main.qml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    id: root

    visible: true
    width: 640
    height: 480
    title: qsTr("Acrylic")


    Image {
        id: img

        fillMode: Image.PreserveAspectCrop
        anchors.fill: parent
        source: "pic.jpeg"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;pic.jpeg&lt;/code&gt; is the image you're using. Run this to make sure it displays a window containing an image before you move on.&lt;/p&gt;

&lt;p&gt;Now let's think a bit about how to create an Acrylic-style rectangular pane. From screenshots available on the Web, we can tell that it's rectangular, white (OK, depends on your system theme, but white is common), and partly transparent. So, let's create a Rectangle that meets these criteria and that is a child of &lt;code&gt;img&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rectangle {
    id: rect

    anchors.centerIn: parent
    color: "white"
    opacity: 0.4
    width: img.width * 1/2
    height: img.height * 1/2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this should produce a boring-looking rectangle in the middle of the window. Now, let's add a blur as a child of &lt;code&gt;rect&lt;/code&gt; (after adding &lt;code&gt;import QtGraphicalEffects 1.0&lt;/code&gt; to the top of &lt;code&gt;main.qml&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GaussianBlur {
    anchors.fill: parent
    source: parent
    radius: 50
    // this formula is suggested by Qt
    samples: 1 + radius * 2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait, that doesn't do anything! What's going on?&lt;/p&gt;

&lt;p&gt;Well, it turns out that this only blurs the rectangle, and leaves the image alone. To blur the whole thing, we'll need to add a &lt;code&gt;ShaderEffectSource&lt;/code&gt;. Also, we'll need to move the &lt;code&gt;ShaderEffectSource&lt;/code&gt; and the &lt;code&gt;GaussianBlur&lt;/code&gt; out of the Rectangle to make everything behave properly (if you don't believe me, just try it yourself!). This gives us a final result of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import QtQuick 2.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0

Window {
    id: root

    visible: true
    width: 640
    height: 480
    title: qsTr("Acrylic")


    Image {
        id: img

        fillMode: Image.PreserveAspectCrop
        anchors.fill: parent
        source: "pic.jpeg"

        Rectangle {
            id: rect

            anchors.centerIn: parent
            color: "white"
            opacity: 0.4
            width: img.width * 1/2
            height: img.height * 1/2
        }

        ShaderEffectSource {
            id: ses

            anchors.fill: rect
            sourceItem: img
            sourceRect: Qt.rect(x, y, width, height)
        }

        GaussianBlur {
            radius: 50
            anchors.fill: ses
            source: ses
            samples: 1 + radius * 2
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which gives us a nice, Acrylic-y pane. You can also do like I did and split the Acrylic out into a reusable file with some &lt;code&gt;alias&lt;/code&gt;es for easy configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import QtQuick 2.12
import QtGraphicalEffects 1.0

Item {
    id: acrylicRoot

    property alias color: rect.color
    property alias acrylicOpacity: rect.opacity

    Rectangle {
        id: rect

        anchors.fill: parent
        color: "white"
        opacity: 0.4
    }

    ShaderEffectSource {
        id: ses

        anchors.fill: parent
        sourceItem: acrylicRoot.parent
        sourceRect: Qt.rect(acrylicRoot.x, acrylicRoot.y, acrylicRoot.width, acrylicRoot.height)
    }

    GaussianBlur {
        radius: 50
        anchors.fill: ses
        source: ses
        samples: 1 + radius * 2
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then stick it into any old QML scene, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    id: root

    visible: true
    width: 640
    height: 480
    title: qsTr("Acrylic")


    Image {
        id: img

        fillMode: Image.PreserveAspectCrop
        anchors.fill: parent
        source: "pic.jpeg"

        Acrylic {
            anchors.centerIn: parent
            width: parent.width * 1/2
            height: parent.height * 1/2

            Label {
                anchors.centerIn: parent
                text: "Acrylic"
                font.pointSize: 32
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(This basic code, by the way, was used to generate the screenshot at the top of this article.)&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Label&lt;/code&gt; in the above code provides an interesting bug: the &lt;code&gt;ShaderEffectSource&lt;/code&gt; is grabbing the &lt;code&gt;Label&lt;/code&gt; for part of its source, which means that the &lt;code&gt;Label&lt;/code&gt; influences the blur. In fact, whenever the Acrylic is updated, the &lt;code&gt;ShaderEffectSource&lt;/code&gt; is grabbing the blur as part of its source instead of just getting what's underneath of it. This is rather undesirable behavior. Do I know how to fix this? No. But it certainly will be an interesting topic to investigate.&lt;/p&gt;

&lt;p&gt;Found this useful? Let me know in the comments below what you're doing with this.&lt;/p&gt;

</description>
      <category>qt</category>
      <category>qml</category>
    </item>
  </channel>
</rss>
