Why can't I change an objects attribute in QML? - qt

I made 2 different rectangels in QML. The fisrts one's color is red and the other one is blue. Then I wanted to change the first one's color to white. QT builds and runs the code perfectly but the color doesn't change. How can I fix the problem?
Edit: I already did clean and rebuild
Rectangle{
id: rect1
x: 10
y: 10
width: 50
height: 50
color: "red"
}
Rectangle{
id: rect2
x: 90
y: 10
width: 50
height: 50
color: "blue"
}
to
Rectangle{
id: rect1
x: 10
y: 10
width: 50
height: 50
color: "white"
}
Rectangle{
id: rect2
x: 90
y: 10
width: 50
height: 50
color: "blue"
}

Try to run qmake because QML & QSS files need to generate new make files again to have an effect on your program..

You can also try Tools->QML/JS->Reset code model

Related

Qml rounded corner of one side with border

Is there a way to have a Rectangle with one side rounded edges and also a border in Qt without using the Canvas.
Something like below.
I did try below code and I am able to create the rounded corner on one side.
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
width: 200
height: 200
visible: true
Item {
width: 100
height: 50
opacity: 0.5
layer.enabled: true
anchors.centerIn: parent
Rectangle {
color: "blue"
radius: 10
anchors.fill: parent
}
Rectangle {
color: "blue"
anchors.fill: parent
anchors.leftMargin: 10
}
}
}
With above code I am able to get the one side rounded corners but when I add border then I see overlapping borders.
Is there a clean way of doing this in Qml?
I can think of two ways to do that.
Not the "cleanest" way, but probably the simplest performance-wise. You can keep using the code you have above, but just draw another non-bordered rectangle that covers up the extra border line that you are seeing.
property int borderWidth: 4
Item {
width: 100
height: 50
opacity: 0.5
layer.enabled: true
anchors.centerIn: parent
Rectangle {
id: roundCorners
color: "blue"
radius: 10
border.width: borderWidth
anchors.fill: parent
}
Rectangle {
id: squareCorners
color: "blue"
border.width: borderWidth
anchors.fill: parent
anchors.leftMargin: 10
}
Rectangle {
anchors.left: squareCorners.left
anchors.verticalCenter: squareCorners.verticalCenter
width: borderWidth
height: squareCorners.height - borderWidth * 2
color: "blue"
}
}
You can use QML's Shape object and use a ShapePath to define it. The docs can be found here.
Shape {
ShapePath {
strokeWidth: 4
strokeColor: "black"
fillColor: "blue"
PathLine { ... }
PathLine { ... }
PathLine { ... }
PathArc { ... }
}
}

QT QML Circle and a text listview

I wanted to create an UI similar to below images. I understand Qt Pathview helps me to move the items in an circular fashion. But I couldn't get how to create the background similar to the images with equal space between the text. I tried with rectangle (radius:360) to draw circle, but the pathview items doesn't move along the center of the rectangle-circle.
Maybe this simple example could help you with PathView:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 400
height: 400
Item {
id: container
width: 300
height: 300
anchors.centerIn: parent
Rectangle {
anchors.fill: parent
radius: width /2
color: "orange"
Rectangle {
x: 50
y: 50
width: 200
height: 200
radius: 100
color: "white"
}
}
PathView {
anchors.fill: parent
model: 10
delegate: Rectangle {
width: 50
height: 50
radius: 25
color: "green"
}
path: Path {
startX: 150; startY: 25
PathArc {
x: 150; y: 275
radiusX: 125; radiusY: 125
direction: PathArc.Clockwise
}
PathArc {
x: 150; y: 25
radiusX: 125; radiusY: 125
direction: PathArc.Clockwise
}
}
}
}
}

QML Slider Handle is not moving

I am having issues in moving a slider handle when I try to style "handle:Rectangle" ,Below is the code:
Slider {
id: slider
x: 372
y: 70
width: 33
height: 457
spacing: 0
anchors.rightMargin: 395
rotation: 0
orientation: Qt.Vertical
font.family: "Arial"
value: 0.5
anchors.right: parent.right
background: Rectangle {
anchors.centerIn: parent
x: slider.topPadding + slider.availableWidth / 2 - width / 2
y: slider.leftPadding
implicitWidth: 200
implicitHeight: 4
width: slider.availableHeight
height: implicitHeight
radius: 2
rotation: 90
color: "#bdbebf"
}
handle: Rectangle {
anchors.centerIn: parent
color: slider.pressed ? "white" : "red"
border.color: "gray"
//border.width: 2
width: 20
height: 20
radius: 6
}
}
I am able to move the slider if i am not styling handle:Rectangle.
please Suggest.
That is, because you can't anchor it to the center. You need to specify the x and y values in dependance of the visualPosition.
See the example on how to customize a slider.
Be aware, that the customization provided there does not account for vertical sliders, so you need to adapt it:
y: slider.topPadding + slider.visualPosition * (slider.availableHeight - height)
x: slider.leftPadding + slider.availableWidth / 2 - width / 2
should do it for you. Remove the anchoring of course!

custom Slider without using the QML one?

I'm looking for sliders examples without using the QML one. Just by using rectabgle but i dont really know how to handle it?
The QML one dont have so much properties:
Slider {
id: sliderHorizontal1
x: 69
y: 52
activeFocusOnPress: true
tickmarksEnabled: true
minimumValue: 0
}
Thanks,
I found answer by using QML slider.
Slider {
id: slider
x: 56
y: 53
width: 450
height: 30
tickmarksEnabled: true
activeFocusOnPress: true
updateValueWhileDragging: true
value: 10
maximumValue: 30
style: SliderStyle {
handle: Rectangle {
height: 40
width: height
radius: width/2
color: "#fff"
}
groove: Rectangle {
implicitHeight: 10
implicitWidth: 100
radius: height/2
border.color: "#333"
color: "#222"
Rectangle {
height: parent.height
width: styleData.handlePosition
implicitHeight: 6
implicitWidth: 100
radius: height/2
color: "red"
}
}
}
}
I can add modification inorder to match to my needs...
Thanks #Tarod

How to create drop shadow for Rectangle on QtQuick 2.0

How can i draw a drop shadow for a Rectangle visual item on QtQuick 2.0?
I like to draw a drop shadow for my main window (I have a transparent and no-decorated window)
As a workaround for the clipped shadow issue, you can put your Rectangle in an Item, with additionnal margin to take blur radius in account, and apply shadow on that container:
import QtQuick 2.0
import QtGraphicalEffects 1.0
Item {
width: 320
height: 240
Item {
id: container
anchors.centerIn: parent
width: rect.width + (2 * rectShadow.radius)
height: rect.height + (2 * rectShadow.radius)
visible: false
Rectangle {
id: rect
width: 100
height: 50
color: "orange"
radius: 7
antialiasing: true
border {
width: 2
color: "red"
}
anchors.centerIn: parent
}
}
DropShadow {
id: rectShadow
anchors.fill: source
cached: true
horizontalOffset: 3
verticalOffset: 3
radius: 8.0
samples: 16
color: "#80000000"
smooth: true
source: container
}
}
Just use DropShadow from the QtGraphicalEffects module.
A complete, working example:
import QtQuick 2.0
import QtGraphicalEffects 1.0
Rectangle {
width: 640
height: 480
color: "blue"
Rectangle {
id: rect
anchors.centerIn: parent
width: 100
height: 100
color: "red"
}
DropShadow {
anchors.fill: rect
cached: true
horizontalOffset: 3
verticalOffset: 3
radius: 8.0
samples: 16
color: "#80000000"
source: rect
}
}
Note that you will see a number of warnings like this:
file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/DropShadow.qml:391:5:
QML SourceProxy: Binding loop detected for property "output"
file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml:66:5:
QML SourceProxy: Binding loop detected for property "output"
file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml:61:5:
QML SourceProxy: Binding loop detected for property "output"
file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml:66:5:
QML SourceProxy: Binding loop detected for property "output"
file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianDirectionalBlur.qml:61:5:
QML SourceProxy: Binding loop detected for property "output"
file:///opt/Qt5.0.1/5.0.1/gcc_64/qml/QtGraphicalEffects/private/GaussianGlow.qml:53:5: QML SourceProxy: Binding loop detected for property "output"
Those warnings are QTBUG-28521, which has been fixed in Qt 5.0.2 (which at the time of this writing has not yet been released). Fortunately, there's no actual problem, aside from the annoying console output.
Interesting question... I've been searching for a better way to do this. This is my quick and dirty way of accomplishing a drop shadow effect for a QML Rectangle for the time being.
Rectangle{
width: 500
height: 500
color: "dark grey"
Rectangle {
id: backgroundRect
width: 200
height: 150
radius: 5
anchors.centerIn: parent
color: "red"
Rectangle {
id: dropShadowRect
property real offset: Math.min(parent.width*0.025, parent.height*0.025)
color: "purple"
width: parent.width
height: parent.height
z: -1
opacity: 0.75
radius: backgroundRect.radius + 2
anchors.left: parent.left
anchors.leftMargin: -offset
anchors.top: parent.top
anchors.topMargin: offset
}
}
}
I tried the code above and it in fact adds a shadow, although in my case simply adding another rectangle with a bit on an offset gave me an effect that I liked more.
Rectangle{
id: rec_Shadow
height:rect_withShadow.height
width: rect_withShadow.width
border.color: "#B3B3B3"
color: "#C5C5C5"
anchors{
verticalCenter: rect_withShadow.verticalCenter
horizontalCenter: rect_withShadow.horizontalCenter
horizontalCenterOffset: 5
verticalCenterOffset: 5
}
radius: rect_withShadow.radius
}
Next you add the Rectangle on which you want the shadow, and you call it rect_withShadow

Resources