How to invert mouse cursor behaviour in Mouse Area - qt

I want to invert direction of mouse cursor. For example when I move mouse right - cursor move to left, when I move mouse up - cursor move to down. And I want do it if mouse on pressed left button.

You can hide the real cursor and place a fake cursor in a different position. In the following example:
A define a Rectangle where the reverse mouse area applies
MouseArea requires containsMouse and pressed before reverse mode is actived
MouseArea hides the real mouse with Qt.BlankCursor
Will display a fake mouse using an SVG recreation of the mouse cursor
When the reverse mode is enabled we flip the coordinates of the fake mouse with parent.width - mouseArea.mouseX and parent.height - mouseArea.mouseY
import QtQuick
import QtQuick.Controls
Page {
Rectangle {
anchors.centerIn: parent
color: "#ffe"
border.color: "grey"
width: Math.floor(parent.width / 2)
height: Math.floor(parent.height / 2)
Image {
id: fakeMouse
x: mouseArea.reversed
? parent.width - mouseArea.mouseX
: mouseArea.mouseX
y: mouseArea.reversed
? parent.height - mouseArea.mouseY
: mouseArea.mouseY
visible: mouseArea.containsMouse
source: "cursor-32.svg"
}
MouseArea {
id: mouseArea
property bool reversed: pressed & containsMouse
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.BlankCursor
}
Frame {
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
anchors.margins: 10
visible: fakeMouse.visible
Label {
text: fakeMouse.x + " " + fakeMouse.y
}
}
}
}
//cursor-32.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M9 25.282l4.3-5.894 4.062 8.043 5.016-2.523L18.416 17h7.01L9 5.016zm.99-18.318l12.398 9.046h-5.575l4.237 8.457-3.25 1.635-4.346-8.606-3.464 4.749z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
You can Try it Online!

Related

How to create a vertical line on the chart view to get the positions of the plots?

I put a chart view inside the qml. Now I want to draw a vertical line like the image image:
, when the mouse enters the chart area and return the junction of the vertical line with all the plots drawn.
These kinds of customizations are kind of tricky. There are other ways to achieve this, but I solve it using a Custom Component (the bar) on top of the ChartView in conjunction with a MouseArea (detection of the mouse position).
See the example:
(...)
ChartView {
id: mychart
anchors.fill: parent
antialiasing: true
// Your chart particular customization code
// This is your line marker
Rectangle{
id: linemarker
y: parent.plotArea.y
height: parent.plotArea.height
width: 10
radius: 5
visible: false
border.width: 1
color: "red"
anchors.top: parent.top
anchors.topMargin: parent.height
anchors.horizontalCenter: parent.horizontalCenter
}
// This is your mouse position detector
MouseArea{
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.AllButtons
onMouseXChanged: {
linemarker.visible = true
var theValue = mychart.mapToValue(Qt.point(mouseX, mouseY), mychart.series(0))
linemarker.x = mouseX - linemarker.width/2
console.log(theValue)
}
}
}
PS. It can be optimized more. It is a simple representation of the functionality.

QML SplitView - manually resize childs

I have a QtQuick Controls 1.3 SplitView(as I am on QT 5.11), which contains 3 rectangles in vertical orientation. It displays fine, and I can drag-resize the childs as intended.
Now I want to add a button which allows the user to completely hide the bottom most rectangle, effectively collapsing it. However, nothing I am trying to resize the rect works:
import QtQuick.Controls 1.3 as C1
[...]
C1.SplitView {
id: splitView
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: parent.left
width: parent.width - flightSelector.width - separator.width
orientation: Qt.Vertical
LateralChart {
id: lateralChart
width: parent.width
height: mainPage.height * 0.75
Layout.minimumHeight: 30
Layout.maximumHeight: mainPage.height - 30 - 30
}
UtilityBar {
id: utilityBar
Layout.minimumHeight: 30
Layout.maximumHeight: 30
onCollapse: {
console.log("minimize")
flightList.height = 0 // no effect
flightList.preferredHeight = 0 // no effect
flightlist.Layout.maximumHeight = 0 // no effect
flightList.shouldCollapse = true // no effect
}
}
FlightListTable {
id: flightList
width: parent.width
Layout.minimumHeight: 0
Layout.maximumHeight: mainPage.height - 60 - 30
model: flightListFilterModel
property bool shouldCollapse: false
C1.SplitView.preferredHeight: shouldCollapse ? 0 : 300
}
}
[...]
If I just go flightList.visible = false, then the rect will be hidden, but the position of the middle rect remains, so it is positioned wrong (it should move to the bottom).
How can I resize SplitView child contents dynamically via JS code?
According to the docs, there must always be one (and only one) child object that has Layout.fillheight set to true. By default, it will choose the last visible child in the SplitView. In your case, it sounds like you want that to actually be the first child. So adding Layout.fillHeight: true to your LateralChart should give you the desired output.

QT/QML Material ProgressBar with rounded corners

I wanted to have the Material style ProgressBar component, but with some modifications to make it's height adjustable.
So far so good, I had the result I wanted.
So I just copied this code inside MyPb.qml to use it as a component:
import QtQuick 2.11
import QtQuick.Templates 2.4 as T
import QtQuick.Controls.Material 2.4
import QtQuick.Controls.Material.impl 2.4
T.ProgressBar {
id: control
property real radius: 3
contentItem: ProgressBarImpl {
implicitHeight: control.height
scale: control.mirrored ? -1 : 1
color: control.Material.accentColor
progress: control.position
indeterminate: control.visible && control.indeterminate
}
background: Rectangle {
implicitWidth: control.width
implicitHeight: control.height
radius: control.radius
color: Qt.rgba(control.Material.accentColor.r, control.Material.accentColor.g, control.Material.accentColor.b, 0.25)
}
}
Which gives this result for the sake of example:
With the code:
Rectangle {
width: 600
height: 300
color: "black"
MyPb {
anchors.centerIn: parent
id: prg
width: 100
height: 20
indeterminate: false
radius: 5
visible: true
value: 0.5
}
}
Because ProgressBarImpl doesn't really support radius, the rounded corners are "buried" under the opaque progress rectangle as can be seen on the picture (left of progress bar).
Now, the reason I'm not making my own progress bar is that I want the "indeterminate" animation as well. So I thought it would be much
simpler to reuse the Qt implementation than starting making my own
animations.
So I wonder if there would be a way to have the Material progress bar, but apply to it some kind of treatment to get rounded corners both with indeterminate = false/true.
Any help would be appreciated!
See the following post in the Qt forum:
https://forum.qt.io/topic/91649/make-a-round-progress-bar/7
The progress bar proposed there consists of the following components:
a rounded Rectangle for the "trough" of the progress bar
an Item that acts as a rectangular clip path
a rounded Rectangle inside that Item, used as the coloured bar
Adapted to your question, I get the following code as a proof-of-concept:
import QtQuick 2.9
Rectangle {
property int percentage: 40
id: root
width: 400
height: 100
radius: height / 2
color: "#333"
Item {
id: cliprect
anchors.bottom: parent.bottom
anchors.top: parent.top
anchors.left: parent.left
width: parent.width * parent.percentage / 100
clip: true
Rectangle {
width: root.width
height: root.height
radius: height / 2
anchors.bottom: parent.bottom
anchors.left: parent.left
color: "#e33"
}
}
}
It should be easy to move that into a template / make it compatible with the Material properties.
You could try setting an OpacityMask on the contentItem using the background item as a mask source.
If that doesn't work out, it will be easier just to create a progress bar. It is a very trivial and non-interactive component with a tiny usage interface after all.

Can We have a SwipeView by using PathView?

In QML Swipe View is not bidirectional.So I need a swipe view
A code sample will be very beneficial for me.
I need to keep only 3 items in my view & at a time only item should be visible & on swiping the view in either way left or right element should be on center.
This code solves half problem That is why I posted as answer
import QtQuick 2.0
Item {
property alias model: view.model
property alias delegate: view.delegate
property alias currentIndex: view.currentIndex
property real itemHeight: 60
clip: true
PathView {
id: view
anchors.fill: parent
snapMode: PathView.NoSnap
pathItemCount: height/itemHeight
preferredHighlightBegin: 0.5
preferredHighlightEnd: 0.5
dragMargin: view.width/5
path: Path {
startY: view.width/4; startX:-itemHeight/2 -50
PathLine { y: view.width/4; x: (view.pathItemCount*itemHeight + 3*itemHeight) }
}
}
}
And this is My Item :
Item{
id:widgetMain
width :480
height : 240
property int delegateHeight: widgetMain.height
property int delegateWidth : widgetMain.width
Spinner {
id: spinner
width: parent.width;
height: parent.height;
focus: true
model: ["qrc:/Tile1.qml",
"qrc:/Tile2.qml"
,"qrc:/Tile3.qml"]
itemHeight: 150
delegate: Loader {
width: delegateWidth
height: delegateHeight
source: modelData
}
}
}
Now If I swipe towards any direction, It shows only 1 tile in the view. & When my drag reaches to half way, then the tile removes & shifts to last.
Here I want to display that one tile is swiping & 2nd tile is coming from behind(Just like a Swipe view).
Now can you help me please?

Overlapping mouseareas in QtQuick 2.0

I have list of items in QML 2.0 and I want to display item's context menu (red box in the picture) only when mouse is inside black mouseArea. Context menu contains a few buttons, each of them has own museArea. In QtQuick 1.0 it works as expected, but in 2.0 not. When I move the cursor between small red boxed (context menu's items), black MouseArea::onExited is called (and context menu isn't shown). It looks like small red mouseareas covered the larger, black mousearea. If I set:
z: 10
in black mouseArea, onExited isn't called when cursor is above small red boxes, but I can't use small mouseares hovering effects. What should I do to have an access to small red boxes' mouseareas and simultanously not calling black mouseArea::onExited when cursor is above red box?
Instead of relying on the mouseArea onExited and onEntered events, you could enable hovering and check the containsMouse property. Here's a working example (QtQuick 2.0):
Column
{
spacing: 10
Repeater
{
model:4
Rectangle
{
height: 100
width: parent.width
border.color: "black"
MouseArea
{
id: mouseArea
anchors.fill: parent
hoverEnabled: true
}
Rectangle
{
visible: mouseArea.containsMouse
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.margins: 20
width: 200
border.color: "red"
Rectangle
{
anchors.centerIn: parent
color: "purple"
width: 20
height: 20
MouseArea
{
anchors.fill: parent
onClicked: print("clicked")
}
}
}
}
}
}
If a MouseArea overlaps with the area of other MouseArea items,
you can choose to propagate clicked, doubleClicked and
pressAndHold events to these other items by setting
propagateComposedEvents to true and rejecting events that should
be propagated. See the propagateComposedEvents documentation for
details.
Source

Resources