How to hide an image using transparent rectangle in QML? - qt

In the below code I want to hide the image using transparent rectangle. Please give me some solutions. I have used z value but it is not working. The image is still visible.
main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Image
{
id:underlyingImage
width: 400
height: 400
x:0;
y:0;
source:"qrc:/Jellyfish.jpg"
z:1
}
Rectangle
{
id:hiding_rect
width: 400
height: 400
x:0;
y:0;
color:"transparent"
z:100
}
}

You can use the OpacityMask to achieve what you try, in the following example we hide a quadrant of the image.
import QtQuick 2.5
import QtQuick.Window 2.2
import QtGraphicalEffects 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Image
{
id:underlyingImage
width: 400
height: 400
fillMode: Image.PreserveAspectCrop
layer.enabled: true
layer.effect: OpacityMask {
maskSource: hiding_rect
}
source:"qrc:/Jellyfish.jpg"
}
Rectangle
{
id:hiding_rect
width: underlyingImage.width/2
height: underlyingImage.height/2
}
}

There is another way to use OpacityMask, but your Qt version should be >= 5.7.
import QtQuick 2.0
import QtQuick.Window 2.0
import QtGraphicalEffects 1.0
Window {
width: 1280
height: 800
visible: true
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Image {
id: underlyingImage
width: 1204
height: 682
visible: false
source: "qrc:/timg.jpg"
}
Item {
id: hidingRect
anchors.fill: underlyingImage
visible: false
Rectangle {
width: underlyingImage.width / 2
height: underlyingImage.height / 2
color: "yellow"
}
}
OpacityMask {
anchors.fill: underlyingImage
source: underlyingImage
maskSource: hidingRect
invert: true
}
}
The result

Related

Instead of transparency, the color is black

Can someone tell me why the window has a black background instead of transparency?
Window {
width: 640
height: 480
opacity: 1
visible: true
color: "transparent"
title: qsTr("Hello World")
}
You have to notify a window manager that you want an transparent window , by default it is opaque. just add Qt.WA_TranslucentBackground.
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
id: wnd
width: 400
height: 300
visible: true
color: "#80000000"
//opacity: 0.5 // the same
flags: Qt.WA_TranslucentBackground | Qt.FramelessWindowHint
MouseArea {
anchors.fill: parent
onClicked: wnd.close();
}
}

Qt Quick layout changes on different events

When I execute my QML code, the output is:
When I minimize the window, It becomes like
and finally, when I again maximize the window it changes to
the GUI which I want to make looks like
![][5]
I am not getting what is the issue for all of the changes in GUI at different events. And this is the Qml code which I wrote
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
Window {
visible: true
width: 1080
height: 720
title: qsTr("Login")
GridLayout{
Rectangle{
id:one
Rectangle
{ id:two
color:"black";
width: 700
height:40
}
Image {
id: image
x: 470
y: 0
width: 54
height: 42
source: "qrc:/user.png"
}
Rectangle
{
id:three;
color:"#f47a42";
width: 200
height:40
anchors.left:two.right;
anchors.margins:940
Text {
id: user
text: qsTr("4200")
color:"white"
anchors.top: value.bottom
}
Text
{
id: value;
text: qsTr("User");
color:"yellow"
}}
}
}
Rectangle{
ColumnLayout{
width: 50
height: childrenRect.height+fillHeight;
}
color:"green"
}
}
So why this is happening and how can I solve this problem?
Output of the code below
Here is example of scalable window:
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Layouts 1.11
Window {
visible: true
width: 800
height: 600
title: qsTr("Layout example")
ColumnLayout{
spacing: 0
anchors.fill: parent
Item {
id: titlebar
Layout.preferredHeight: 40
Layout.fillWidth: true
RowLayout {
anchors.fill: parent
spacing: 0
Rectangle {
Layout.fillWidth: true
Layout.fillHeight: true
color: "orange"
Text {
anchors.centerIn: parent
text: "Title"
}
}
Rectangle {
Layout.preferredWidth: 100
Layout.fillHeight: true
color: "lightgreen"
Text {
anchors.centerIn: parent
text: "Actions"
}
}
}
}
Rectangle {
id: content
Layout.fillHeight: true
Layout.fillWidth: true
color: "lightyellow"
Text {
anchors.centerIn: parent
text: "Content"
}
}
}
}

How to use qt-component OpacityMask

There is a question How to hide an image using transparent rectangle in QML?
The accepted answer is to use OpacityMask.
I created a qml file follow this answer, but didn't get the expected result.
Is there anything wrong in my codes?
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtGraphicalEffects 1.0
ApplicationWindow {
visible: true
width: 1280
height: 800
title: qsTr("Hello World")
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Image
{
id:underlyingImage
width: 1204
height: 682
fillMode: Image.PreserveAspectCrop
layer.enabled: true
layer.effect: OpacityMask {
maskSource: hiding_rect
}
source:"qrc:/timg.jpg"
}
Rectangle
{
id:hiding_rect
width: underlyingImage.width/2
height: underlyingImage.height/2
color: "yellow"
}
}
the result picture
I'm not familiar with the approach suggested in the other question.
However, following the approach suggested in the documentation (http://doc.qt.io/qt-5/qml-qtgraphicaleffects-opacitymask.html), this works:
Window {
visible: true
width: 1280
height: 800
title: qsTr("Hello World")
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Image
{
anchors.centerIn: parent
id:underlyingImage
width: 1204
height: 682
fillMode: Image.PreserveAspectCrop
source:"file:///tmp/timg.jpg"
visible: false
}
Item {
id:hiding_rect
anchors.fill: underlyingImage
visible: false
Rectangle
{
anchors.left: parent.left
anchors.top: parent.top
width: underlyingImage.width/2
height: underlyingImage.height/2
color: Qt.rgba(1,1,1,1)
z: underlyingImage.z + 1
}
}
OpacityMask {
anchors.fill: underlyingImage
source: underlyingImage
maskSource: hiding_rect
}
}

how to use topMargin in ColumnLayout in QML

I am trying to use topMargin in ColumnLayout. but i am facing some issues.
Could some one help me out of this.
Here is my code
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
anchors.fill: parent
ColumnLayout{
id: columlayout
Rectangle{
width: 100
height: 100
color: "red"
}
Rectangle{
Layout.topMargin: 50
width: 100
height: 100
color: "green"
}
Rectangle{
width: 100
height: 100
color: "blue"
}
}
}
}
Issue:
Cannot assign to non-existent property "topMargin"
The margin properties were introduced in QtQuick.Layouts 1.2, so you must import that version, not 1.1.

Zoom with finger touch on QML and buttom for reset zoom

How to QML with two fingers to zoom in the photo?
I wrote the following code for the Click also I want a button for reset zoom to screen size and i want a buttom for back but for each screen position is different
please help me.
Thank you
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.1
import QtQuick.Window 2.0
Window {
id: win
visible: true
width: Screen.width
height: Screen.height
Rectangle {
id: ali
width: win.width
height: win.height
ScrollView {
width: win.width
height: win.height
Rectangle {
id: inspector
Image {
id: visibleImg
x: 0
y: 0
source: "qrc:///pic/1.jpg"
width: inspector.width
height: inspector.height
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_VolumeDown) {
inspector.width=inspector.width+50
inspector.height=inspector.height+50
event.accepted = true;
}
}
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton
onClicked:{
if (mouse.button == Qt.LeftButton){
inspector.width=inspector.width+50
inspector.height=inspector.height+50
}
}
}
width: win.width
height: win.height
}
}
Button {
id: button1
x: 1
y: win.width+12
width: 25
height: 25
text: qsTr("+")
onClicked: {
inspector.width=inspector.width+50
inspector.height=inspector.height+50
}
}
Button {
id: button2
x: 1
y: win.width+37
width: 25
height: 25
text: qsTr("-")
onClicked: {
inspector.width=inspector.width-50
inspector.height=inspector.height-50
}
}
anchors.centerIn: ali
}
}
Try with this example :
https://github.com/yekmen/mee500px/blob/master/tags/0.0.5Beta/Mee500px/qml/Mee500px/Tools/ZoomableImage.qml
Work fine !
edit : Update link !
I could change the code as follows:
But only two-finger zoom it down
And the photo can not be moved
Also I'm 10 photos
I want to give everyone the same two buttons display
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
Window {
id: win
visible: true
width: 240
height: 320
title: "aseman-abi"
Rectangle {
id: ali
width: win.width
height: win.height
Rectangle {
id: photoFrame
x: 0
y: 0
width: win.width
height: win.height
color: "#ffffff"
PinchArea {
anchors.fill: parent
pinch.target: photoFrame
pinch.minimumScale: 1
pinch.maximumScale: 5
}
BorderImage {
id: borderImage1
anchors.fill: parent
source: "qrc:///pic/1.jpg"
}
}
Button {
id: button2
x:10
y: win.height-37
width: 50
height:40
text: qsTr("N")
onClicked: {
borderImage1.source="qrc:///pic/2.jpg"
}
}
Button {
id: button3
x: win.width-71
y: win.height-37
width: 50
height: 40
text: qsTr("P")
onClicked: {
borderImage1.source="qrc:///pic/1.jpg"
}
}
}
}

Resources