QML performance on sliders - performance

Hmm.. I have 22 of these slider elements placed via a Repeater in my app.. Moving a slider is quite jitter-y and jerky, and that is on the nvidia gpu on my laptop. This was smoother on integrated graphics.. Is there anything plainly obvious I could improve? I'm fairly new to Qt, so this is a puzzle for me..
import QtQuick 2.0
import QtQuick.Controls 2.2
Item {
height: 4*82
width: 82
Rectangle {
height: 4*82
width: 83.55
color: "#0a0a0a"
border.color: "#80cccccc"
Rectangle {
width: 83.55
height: 22
color: "#0a0a0a"
border.color: "#80cccccc"
y:parent.y -22
Label {
x:parent.x+parent.width/2 - width/2
y:5
id: name
text: qsTr("MASTER EFX SP")
color: slider.value > 0 ? "#6eaf71" : "#3c3c3c"
font.pixelSize: 10
}
}
}
Slider {
id: slider
from: 0
to: 255
value: 0
orientation: Qt.Vertical
height: (4*82)-32
width: 83.55
y: 16
stepSize: 1.0
handle: Item {
y: parent.topPadding + parent.visualPosition * (parent.availableHeight - 50)
x: parent.leftPadding + parent.availableWidth / 2 - 30 / 2
Rectangle {
color: parent.parent.pressed || parent.parent.value > 0? "#901fdcf5" : "#90ffffff"
border.color: parent.parent.pressed || parent.parent.value > 0? "#1fdcf5" : "gray"
width: 30
height: 50
radius: 2
}
Rectangle {
color: parent.parent.pressed || parent.parent.value > 0? "#80000831" : "#4e4e4e"
width: 30
height: 2
y: 50/2-2
}
}
background: Rectangle {
x: parent.leftPadding + (horizontal ? 0 : (parent.availableWidth - width) / 2)
y: parent.topPadding + (horizontal ? (parent.availableHeight - height) / 2 : 0)
implicitWidth: horizontal ? 200 : 6
implicitHeight: horizontal ? 6 : 200
width: horizontal ? parent.availableWidth : implicitWidth
height: horizontal ? implicitHeight : parent.availableHeight
radius: 0
border.color: "#3c3c3c"
color: "#3c3c3c"
scale: horizontal && parent.mirrored ? -1 : 1
readonly property bool horizontal: parent.orientation === Qt.Horizontal
}
}
}

First, try to understand what's wrong - it might not be the sliders after all.
use Gammaray to open your Qml application and examine the graphics stack.
From the code that you pasted, the only thing I could think that migth cause an issue is the radius property, as it's the only expensive thing your code is doing.
Also, try to update the library imports to the latest qt version, you are using a really old one and new ones also migth improve performance.

Related

Smooth animation of second hand

I am building a simple animated clock with QML and it works, but I want to have a "sweep second" in which the second hand rotates continuously and smoothly rather than abruptly transitioning from second to second. Without the Behavior line in the code below, I get a stepped-second but otherwise correct behavior. With the Behavior line, it works perfectly until the seconds go from 59 back to 0, and then it goes counter clockwise which is not all the behavior I want.
I read that using SmoothedAnimation would help, but when I tried substituting SmoothedAnimation for NumberAnimation it does a stepped-second until the 59 to 0 transition and then goes counter clockwise.
How do I alter this to have it only rotate clockwise but do so smoothly?
import QtQuick 2.0
Item {
width: 320; height: 320
Item {
property var time: new Date()
id: wallClock
Timer {
interval: 1000
running: true
repeat: true
onTriggered: wallClock.time = new Date()
}
}
Rectangle {
id: rect1
x: parent.width/2
y: parent.height/2 - 100
width: 10; height: 70
color: "red"
transform: Rotation {
origin.x: rect1.width/2
origin.y: rect1.height
angle: wallClock.time.getSeconds()*6
Behavior on angle { NumberAnimation { duration: 1000 } }
}
}
}
Rather than a NumberAnimation or SmoothedAnimation, I think a RotationAnimation is what you want. The advantage there is that it allows you to force the direction to remain clockwise.
transform: Rotation {
origin.x: rect1.width/2
origin.y: rect1.height
angle: wallClock.time.getSeconds()*6
Behavior on angle { RotationAnimation { duration: 1000; direction: RotationAnimation.Clockwise} }
}

QML slow performance animating a lot of rectangles

I'm creating a "dotted paper" component for my QML app. Essentially this components consists of a dotted background that the user can zoom upon. To create this I used the following code:
DottedPaper.qml
import QtQuick 2.15
Rectangle{
id: page
property int spacing: 50
// this property changes the spacing between the dots
property real contentScale: 1.0
Repeater{
id: rowRepeater
model: 200
Repeater{
id: row
property int rowIndex: index
model: 200
Rectangle{
id: circle
color: "red"
width: 4; height: 4
x: index * spacing * page.contentScale + width/2;
y: rowIndex * spacing * page.contentScale+ height/2
radius: width/2
}
}
}
}
Then in my main file I have the following:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Flickable{
clip: true // doesn't improve anything
anchors.fill: parent
contentWidth: paper.width
contentHeight: paper.height
DottedPaper{
id: paper
width: 10000; height: 10000;
clip: true
}
}
// the slider changes the content scale of the dotted paper
Slider{
from: 1
to: 3
onValueChanged: paper.contentScale = value
}
}
The problem with this code is that it's slow. Upon checking the application does not render the content at 60fps, but I get around 45 - 50 fps on my desktop. I suspect the issues is that QML is reevaluating the contentScale property binding for each circle in the background even if the circle is not visible on the screen. What is the best way to prevent this?
It works a lot better if you change your Repeaters to be ListViews. That's because a ListView is smart enough to only create enough delegates as needed for the visible space. (At first I thought a GridView would work, but that doesn't give you enough control over rows/columns.)
Rectangle{
id: page
property int cellWidth: 50 * contentScale
property int dotWidth: 4
// this property changes the spacing between the dots
property real contentScale: 1.0
ListView {
id: grid
model: 200
anchors.fill: parent
anchors.leftMargin: page.dotWidth / 2
anchors.topMargin: page.dotWidth / 2
spacing: page.cellWidth - page.dotWidth
delegate: ListView {
model: 200
width: window.width
height: page.dotWidth
orientation: ListView.Horizontal
spacing: page.cellWidth - page.dotWidth
property int rowIndex: index
delegate: Rectangle{
id: circle
color: "red"
width: page.dotWidth; height: page.dotWidth
radius: width/2
}
}
}
}

Change height of rows based on item depth in QML Treeview

I am trying to change the height of rows in my TreeView based on the item depth in the tree. The itemDelegate has access to the depth property (so I can change height of the item), but the row height can only be set from the rowDelegate, which can't see item depth.
As you can see from example I need to change row heights to match depth. How can the rowdelegate determine the item depth from the model?
Component {
id: delegateMenuItem
Column {
height: 100 // No effect
spacing: 20 // No effect
bottomPadding: 20 // No effect
Rectangle {
border {
color: "blue"
width: 2
}
color: "red"
height: itemText.height * 2
width: 300
Text {
id: itemText
text: qsTr(styleData.value)+"-"+height
color: "white"
font.pointSize: fontSize(styleData.depth)
}
}
}
}
Using TreeView from QQC1, I suggest that you try setting the implicitHeight for the itemDelegate and not setting any height for the rowDelegate. So try changing your snippet to:
itemDelegate: Rectangle {
id: delegateMenuItem
border { color: "blue"; width: 2 }
color: "red"
width: ...
implicitHeight: styleData.depth * ...
Text {
id: itemText
anchors.fill: parent
...
}
}
(In your current implementation delegateMenuItem does not have any implicitHeight. Meaning its height is probably 0. Also the ColumnLayout as you are using it is useless so I have removed it.)
I have not tried the above. However I have tried doing what you want to do with TreeView from Qt marketplace. It is designed to work with QQC2 and is based on TableView from QQC2 so you can use rowHeightProvider.

QML/QtQuick2 Anchor Item to static location on a scaled image

What I am attempting to do is anchor an Item over a particular place over an Image. The Image needs to use Image.PreserveAspectFit for the fillMode. The problem I am having is that the height/width/paintedWidth/paintedHeight of the Image are for the entire image "canvas" (I'm not sure what it's actually called), not the drawn part of the image itself. So I can't anchor to the actual image.
See code below for an example I tried with anchors (red rectangle) and with a child rectangle and x,y coordinates (green rectangle).
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
height: 400
width: 400
Image {
id: image
anchors.fill: parent
fillMode: Image.PreserveAspectFit
source: "image.jpg"
Rectangle {
id: bottomRight
width: 40
height: 40
color: "green"
x: parent.width * 0.75
y: parent.height * 0.75
}
}
Rectangle {
id: topLeft
width: 40
height: 40
color: "red"
anchors.top: image.top
anchors.left: image.left
anchors.topMargin: image.height * 0.25
anchors.leftMargin: image.width * 0.25
}
}
When you change the size of the window the placement of the rectangles is not in the same place over the image. I'd post some screenshots but I don't have enough reputation yet!
I poked around through the widget tree using the debugger but I can't seem to find any properties that provide the information about the scaling that I could use to calculate placement of the rectangles.
Update
I used the following functions to calculate the margins since I will be using potentially lots of these overlays.
function horMargin(val)
{
return ((image.width - image.paintedWidth)/2 + image.paintedWidth * val)
}
function verMargin(val)
{
return ((image.height - image.paintedHeight)/2 + image.paintedHeight * val)
}
Did you try to use paintedWidth and paintedHeight? :)
This will give you a rectangle that fills the painted image:
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
height: 400
width: 400
Image {
id: image
anchors.fill: parent
fillMode: Image.PreserveAspectFit
source: "image.jpg"
}
Rectangle {
x: (image.width - image.paintedWidth) / 2
y: (image.height - image.paintedHeight) / 2
width: image.paintedWidth
height: image.paintedHeight
color: "transparent"
border.color: "darkorange"
border.width: 2
}
}
From there, you can work out how to apply the margins that you need.

Color Change to Mouse effect in JavaFX

I want to change this code so when I click on the ball, it turns red. Can anyone help me? I'm new to java:
http://download.oracle.com/javafx/1.3/tutorials/ui/syntax/index.html
Try this:
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;
var circle: Circle;
Stage {
title: "Declaring Is Easy!"
scene: Scene {
width: 300
height: 250
content: [
circle = Circle {
centerX: 150 centerY: 120 radius: 80
fill: Color.MAROON
stroke: Color.INDIANRED
strokeWidth: 10.0
}, //Circle
Rectangle {
x: 25, y: 80 width: 250, height: 80
arcWidth: 20 arcHeight: 20
fill: Color.web("#6699ff")
stroke: Color.web("#003399")
strokeWidth: 5.0
onMouseClicked:function(e: MouseEvent)
{
if(circle.fill == Color.MAROON)
circle.fill = Color.GREEN
else
circle.fill = Color.MAROON
}
} //Rectangle
] //Content
} //Scene
} //Stage
Two changes here:
1) Extract Circle in a variable (circle), so that it can be used later.
2) Add a mouse clicked event to the rectangle, to flip colors.
you just have to change the word circle into rectangle right here
if(**circle**.fill == Color.MAROON)
**circle**.fill = Color.GREEN
else
**circle**.fill = Color.MAROON
}
if(**rectangle**.fill == Color.MAROON)
**rectangle**.fill = Color.GREEN
else
**rectangle**.fill = Color.MAROON
}
and put in this sentence var rectangle : Rectangle; just like var circle: Circle;

Resources