I have a background image a and i want to right the text align from bottom to top.Just like the image showed as following. In my case, the background image is fixed, i cannot rotate it,so i just tried to rotate text!However, i cannot align it well!
Background Image(cannot be rotated)
The result i want(just like left align,center align and rotate -90)
I tried this code,but not work! I think the reason is the rotation center!
Text {
text: name;
anchors.verticalCenter: parent.verticalCenter; // Centering text
anchors.left: parent.left;
anchors.leftMargin: 18;
rotation: -90
}
If i rotate the background image 90 degree. Then apply my code and rotate the content(background and text) will get good result! However i cannot rotate the background image! How can i set text align from bottom to top in qml?
Anchoring with rotation might be at odds to manage. You can make it simple:
Rectangle{
color: "grey"
width: 50
height: 300
Text{
//1. Place Text box symmetrically below background, edge to edge
width: parent.height
height: parent.width
y:parent.height
text: "TableA Description 15"
verticalAlignment: Text.AlignVCenter
// comment below 2 lines to see how it looks before rotation
//2 pivot top left of text box and rotate text -90
transformOrigin: Item.TopLeft
rotation: -90
}
}
Transform provides more complex transformations for the QML item. In your case, check demo in below, it shows Text rotate itself -90 degree. and you can adjust the rotate point by setting the origin.x origin.y properties.
Rectangle{
width: 100; height: 300
color: "lightblue"
Text{
id: test
text: "TestMe"
transform: [
Rotation {
origin.x: test.paintedWidth/2; origin.y: test.paintedWidth/2; angle: -90
},
Translate{
y: (test.parent.height - test.paintedWidth)/2
}
]
}
}
Related
I have this simple Rectangle and i want to animate it's color change in a way that the new color fills in from right to the left of it.
Rectangle{
width:100
height:150
color:"green"
}
What should I do? What kind of Animation or Transition do I need in this case?
First draw a simple Rectangle with the original color, "green" in this example:
Rectangle{
width:100
height:150
color:"green"
}
Then draw another rectangle on top but with width equal to 0 and the new color, "red" in this example:
Rectangle{
id: newColorRect
width:0
height:150
color:"red"
}
Finally with a animation you can animate the color change, changing the width of the newColorRect rectangle.
PropertyAnimation {
id: colorAnimation
target: newColorRect
properties: "width"
to: 100
duration: 1000
}
Component.onCompleted: {
colorAnimation.start()
}
I have implemented a cover flow similar to Itunes. But there is still one thing that I can't solve, even after researching and reading the documentation...
I have the following result, but I would like to display only half of the reflection.
I have found the following StackOverflow post : How do I make a gradient opacity in an image - QML which is very similar to what I would like to achieve. Except that as my background is a gradient, I can't select the white color (or any plain color for the last GradientStop element in the solution code offered as solution).
Here is my code so far:
Image {
id: rectDelegate
anchors.fill: parent
source: images[index % images.length]
}
ShaderEffectSource {
id: reflection
sourceItem: rectDelegate
y: sourceItem.height
width: sourceItem.width
height: sourceItem.height
transform: [
Rotation {
origin.x: reflection.width / 2
origin.y: reflection.height / 2
axis.x: 1
axis.y: 0
axis.z: 0
angle: 180
}
]
visible: reflection_visible
}
Rectangle {
anchors.fill: reflection
gradient: Gradient {
GradientStop {
position: 0.0
color: "#55ffffff"
}
GradientStop {
// This determines the point at which the reflection fades out.
position: 1
color: "#ffffff"
}
}
visible: reflection_visible
}
I tried to embed the ShaderEffectSource element inside a transparent rectangle whose height would be half of the reflection height and to clip the ShaderEffectSource inside, but the reflection wouldn't appear :/
Here is the code :
Rectangle {
anchors.top: rectDelegate.bottom
color: "transparent"
width: rectDelegate.width
height: rectDelegate.height - 300
visible: reflection_visible
clip: true
border.color: "yellow"
ShaderEffectSource {
id: reflection
sourceItem: rectDelegate
y: sourceItem.height
width: sourceItem.width
height: sourceItem.height
transform: [
Rotation {
origin.x: reflection.width / 2
origin.y: reflection.height / 2
axis.x: 1
axis.y: 0
axis.z: 0
angle: 180
}
]
visible: reflection_visible
}
}
Any idea would be welcome :) I tried to make my post as clear as possible but if there is something missing for understanding, please, ask :)
read this post:
How do I make a gradient opacity in an image? - QML
or you can use Canvas type to draw your image with transparent gradient , but the proper way is the first one.
Can anyone help me how to round only one corner of a rectangle like shown in attached pic where red rectangle is my child rectangle.
Actually, I have a rectangle where all four corners rounded(radius 10). Now, i want to draw a new rectangle inside this and expecting only that particular corner should be rounded who touches the parent's round corner.
Rectangle
{
id: parent
radius: 10
width: 168
height: 168
visible: true
color: "black"
Rectangle
{
id: child
width: 100
height: 40
color: "red"
}
}
I tried to do this with adding clip property in child but nothing happened.
Here is a simple example.
It is rounded in the upper left corner, but is easily adjusted to any other corner. Only one corner is supported in this solution, but it might be enough for you?
More corners are little more complex, so ask again if you would need those aswell.
Rectangle {
anchors.centerIn: parent
id: root
radius: 20
width: 300
height: 300
Rectangle {
id: clipper
width: 100
height: 100
color: 'transparent'
clip: true
Rectangle {
id: clipped
width: parent.width + radius
height: parent.height + radius
radius: root.radius
color: 'red'
}
}
}
Not with the stock Rectangle:
The same radius is used by all 4 corners; there is currently no way to
specify different radii for different corners.
In C++ you could specify a horizontal and vertical radius, but still not per-corner radius. If you want such functionality, you will have to implement your own QQuickItem with the geometry node and all.
The result you want to achieve in the image could also be achieved with clipping, however unfortunately, in QML clipping only works for the item's rectangle, not the actual item geometry.
It will be easiest to achieve the desired effect using a BorderImage element. It enables to specify a different sub-image for every corner:
It is possible using Shape item as below:
Shape {
id: advancedShape
width: 100; height: 40
vendorExtensionsEnabled: true
layer.enabled: true
layer.samples: 4
layer.smooth: true
// set following properties to specify radius
property real tlRadius: 0.0
property real trRadius: 15.0
property real brRadius: 0.0
property real blRadius: 0.0
ShapePath {
strokeColor: "transparent"
fillColor: "red"
startX: 0; startY: advancedShape.tlRadius
PathArc {
x: advancedShape.tlRadius; y: 0
radiusX: advancedShape.tlRadius; radiusY: advancedShape.tlRadius
useLargeArc: false
}
PathLine {
x: advancedShape.width - advancedShape.trRadius; y: 0
}
PathArc {
x: advancedShape.width; y: advancedShape.trRadius
radiusX: advancedShape.trRadius; radiusY: advancedShape.trRadius
useLargeArc: false
}
PathLine {
x: advancedShape.width; y: advancedShape.height - advancedShape.brRadius
}
PathArc {
x: advancedShape.width - advancedShape.brRadius; y: advancedShape.height
radiusX: advancedShape.brRadius; radiusY: advancedShape.brRadius
useLargeArc: false
}
PathLine {
x: advancedShape.blRadius; y: advancedShape.height
}
PathArc {
x: 0; y: advancedShape.height - advancedShape.blRadius
radiusX: advancedShape.blRadius; radiusY: advancedShape.blRadius
useLargeArc: false
}
PathLine {
x: 0; y: advancedShape.tlRadius
}
}
}
and result will be as this:
NOTE The builtin Rectangle has more performance than Shape, but I recommend Shape over masking because it works on any environment.
NOTE 2 I think the most true way in production is using BorderImage as #dtech suggested IF the radius is known and you don't need to change radius dynamically.
Created this from a bunch of rectangles. Two big rectangles and 4 smaller optionally rounded squares. This way you can also make different radii for each corner, not only turning them on and off. You just need to make sure that you don't want a too big rounding, because then the rounded rect would stick out.
Rectangle {
id: clipper
width: 10
height: 10
color: "transparent"
clip: true
Rectangle {
id: clipped
width: 30
height: 30
radius: 8
color: "red"
}
}
I have a centered rectangle with a drop shadow behind it and some text within it.
import QtQuick 2.5
import QtQuick.Window 2.2
import QtGraphicalEffects 1.0
Window {
visible: true
width: 800; height: 640
Rectangle{
id: centerRect
width: parent.width * 0.7; height: parent.height * 0.7
anchors{
horizontalCenter: parent.horizontalCenter
verticalCenter: parent.verticalCenter
}
radius: 7
border.color: "#C0C0C0"
Text{
text: "Hello World!"
font.pixelSize: 0.07 * parent.height
anchors{
horizontalCenter: parent.horizontalCenter
verticalCenter: parent.verticalCenter
}
}
}
DropShadow
{
anchors.fill: centerRect
horizontalOffset: 1; verticalOffset: 1
radius: 5
samples: 11
color: "#CDCDCD"
source: centerRect
}
}
When I resize the window the text becomes slightly blurred or out of focus. I thought it may have been an issue with how I'm scaling the font pixel size to the rectangle height but the problem is the same with static values. If I remove the drop shadow effect the text's visibility is fine when I resize the window.
How can I maintain good text visibility when using a drop shadow and resizing the window? I'm using Qt 5.5.1 on OpenSUSE Leap 42.1 (Plasma 5.5.5).
Solution 1: Use DropShadow only for the background rectangle and draw the text on top of that.
Solution 2: Use integral width and height for the centerRect. The graphical effect renders the centerRect first into a texture. If the source width or height are not integers, the texture size will not correspond to the original item size. When painting the texture, texture coordinates will not hit the pixel positions accurately and some interpolation is needed.
For me, the simplest is to move the Text out of centerRect, so it will not be its child obj, so not influced by the side effect of DropShadow.
eg:
move the text outside and modify its conditioning like below:
Text{
text: "Hello World!"
font.pixelSize: 0.07 * centerRect.height
anchors.centerIn: centerRect
}
I'm trying to make a simple audio meter with QtQuick. The meter graphic comes from a PNG file, and when the level changes, I want to change the height of the displayed part of the image, without scaling it at all. I simply want to mask some of the image off without changing it.
I have tried the following code:
import QtQuick 2.0
Rectangle {
width: 360
height: 482
Image {
x: 165
source: "meter.png"
fillMode: Image.Pad
anchors.bottom: parent.bottom
height: 482
}
Image {
x: 175
source: "meter.png"
fillMode: Image.Pad
anchors.bottom: parent.bottom
height: 440
}
Image {
x: 185
source: "meter.png"
fillMode: Image.Pad
anchors.bottom: parent.bottom
height: 400
}
}
But this gives the following undesried results, with some kind of scaling happening. I want where the yellow and green meet in all 3 images to be identical.
The Image component fills from top to bottom by default, but instead of wrapping Image in an outer Item, and using clip which is bad for performance, just add to all your Image items :
width: sourceSize.width; // to be sure the width is the exact same as the source
verticalAlignment: Image.AlignBottom; // to force image to start from the bottom
So the content will not be scaled and aligned to the bottom of your Image.
The Image Item fills its content top-down, so if you want to anchor it to the bottom you will need an additional container Item to clip out the Image's top portion:
Item {
x: 165
width: childrenRect.width
height: 482
clip: true // This is the key
Image {
source: "meter.png"
anchors.bottom: parent.bottom
}
}