I would like to use the scroll on my mouse to scroll on a horizontal scrollview. How to do i achieve this here is the code for my sample scroll view.
ScrollView {
id:scrollview
ListView {
anchors.fill: parent
model: 30
orientation: ListView.Horizontal
delegate: Component{
Item{
height: scrollview.height
width: scrollview.height
Rectangle{
anchors.fill: parent
color:"lightgrey"
Text{
anchors.centerIn: parent
text: index
}
}
}
}
}
}
I would like to achieve a scroll like in the vertical view but instead just in the horizontal orientation
I got it to work with a MouseArea on top of the ScrollView that listens for mouse wheel events but passes through other events.
ScrollView {
id:scrollview
property int scrollSpeed: 30
ListView {
anchors.fill: parent
model: 30
orientation: ListView.Horizontal
delegate: Component {
Item {
height: scrollview.height
width: scrollview.height
Rectangle {
anchors.fill: parent
color:"lightgrey"
Text {
anchors.centerIn: parent
text: index
}
}
}
}
}
}
MouseArea {
anchors.fill: scrollview
onWheel: {
if (wheel.angleDelta.y > 0) {
scrollview.flickableItem.contentX -= scrollview.scrollSpeed;
if (scrollview.flickableItem.contentX < 0) {
scrollview.flickableItem.contentX = 0;
}
} else {
scrollview.flickableItem.contentX += scrollview.scrollSpeed;
if (scrollview.flickableItem.contentX + scrollview.flickableItem.width > scrollview.flickableItem.contentWidth) {
scrollview.flickableItem.contentX = scrollview.flickableItem.contentWidth - scrollview.flickableItem.width;
}
}
}
onClicked: mouse.accepted = false;
onPressed: mouse.accepted = false;
onReleased: mouse.accepted = false;
onDoubleClicked: mouse.accepted = false;
onPositionChanged: mouse.accepted = false;
onPressAndHold: mouse.accepted = false;
}
Related
I have an app that consists of multiple plages. And user will be able to switch between these pages by button clicks. When I put a Window {} in Kontrolpage.qml, it gives me EGLFS: OpenGL windows cannot be mixed with others error. So I put an Item in Kontrolpage.qml. In that way when I press the button, item gets opened. However, it looks small. Eventhough I set the size constraints in Kontrolpage.qml to 640-480 which is same as the size of the main.qml. So main qml still visible in the background and on top of it newly opened qml is visible but it's not fullscreen. How can I fix that thing and what I am doing wrong?
Window {
id:main
width:640
height:480
Image
{
objectName: "background"
width: parent.width
height: parent.height
source:"Imgs/Images/bckgnd.png"
LeftButtons
{
id:buttonKontrol
x: -20
y:110
buttonName: "kontrol"
Loader { id: kontrolPageLoader}
MultiPointTouchArea
{
id:touchArea_kontrol
anchors.fill:parent
onPressed:
{
kontrolPageLoader.source ="KontrolPage.qml"
}
}
}
}
And here is the Kontrolpage.qml:
Item{
visible:true
width:640
height:480
Image {
objectName:"background"
width:parent.width
height:parent.height
source:"Imgs/Images/bckgnd"
}
}
Edit: When I put Loader outside of the LeftButtons and into window {} as :
Window {
id:main
width:640
height:480
Loader { id: kontrolPageLoader}
Image
{
objectName: "background"
width: parent.width
height: parent.height
source:"Imgs/Images/bckgnd.png"
LeftButtons
{
id:buttonKontrol
x: -20
y:110
buttonName: "kontrol"
MultiPointTouchArea
{
id:touchArea_kontrol
anchors.fill:parent
onPressed:
{
kontrolPageLoader.source ="KontrolPage.qml"
}
}
}
}
It does not open the new page. And keeping the loader inside the LeftButtons, when I use anchors.fill:parent, it resize it according to parent LeftButtons. However, I want to have the size of my Window in the new Page. The main problem in here looks like, why I cannot put Loader out of Leftbuttons and in Window?
Complete code for main.qml:
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
id:main
visible: true
width: 640
height: 480
//Constant Definitions
readonly property string textColorBlue: "#8ccae5"
readonly property string textColorRed: "#dc0000"
Item {
id: backgroundItem
width:main.width
height:main.height
Loader
{
id:pageLoader
anchors.fill:parent
}
Image
{
id:backgroundImage
objectName: "background"
width:parent.width
height:parent.height
source: "Imgs/Images/NMS_NEW_4_TR_03.png"
LeftButtons
{
id: buttonKontrol
x: -20
y: 110
buttonName:"Kontrol"
MultiPointTouchArea
{
id:touchArea_kontrol
anchors.fill: parent
onPressed:
{
buttonKontrol.buttonImageColor="Imgs/Images/button_left_red.png"
buttonKontrol.buttonTextColor=textColorRed
pageLoader.source="KontrolPage.qml"
}
onReleased:
{
buttonKontrol.buttonImageColor="Imgs/Images/button_left_blue.png"
buttonKontrol.buttonTextColor=textColorBlue
}
}
}
LeftButtons
{
id:buttonKullaniciKitabi
x:-20
y:310
buttonName:"Kullanıcı Kitabı"
MultiPointTouchArea
{
id:touchArea_kullaniciKitabi
anchors.fill: parent
onPressed:
{
buttonKullaniciKitabi.buttonImageColor="Imgs/Images/button_left_red.png"
buttonKullaniciKitabi.buttonTextColor=textColorRed
}
onReleased:
{
buttonKullaniciKitabi.buttonImageColor="Imgs/Images/button_left_blue.png"
buttonKullaniciKitabi.buttonTextColor=textColorBlue
}
}
}
LeftButtons
{
id: buttonAyarlar
x:-20
y:510
buttonName: "Ayarlar"
MultiPointTouchArea
{
id:touchArea_ayarlar
anchors.fill: parent
onPressed:
{
buttonAyarlar.buttonImageColor="Imgs/Images/button_left_red.png"
buttonAyarlar.buttonTextColor=textColorRed
}
onReleased:
{
buttonAyarlar.buttonImageColor="Imgs/Images/button_left_blue.png"
buttonAyarlar.buttonTextColor=textColorBlue
}
}
}
RightButtons
{
id:buttonCCTV
x:1050
y:110
buttonName: "CCTV"
MultiPointTouchArea
{
id:touchArea_CCTV
anchors.fill: parent
onPressed:
{
buttonCCTV.buttonImageColor="Imgs/Images/button_right_red.png"
buttonCCTV.buttonTextColor=textColorRed
}
onReleased:
{
buttonCCTV.buttonImageColor="Imgs/Images/button_right_blue.png"
buttonCCTV.buttonTextColor=textColorBlue
}
}
}
RightButtons
{
id:buttonCTIS
x:1050
y:310
buttonName: "CTIS"
MultiPointTouchArea
{
id:touchArea_CTIS
anchors.fill: parent
onPressed:
{
buttonCTIS.buttonImageColor="Imgs/Images/button_right_red.png"
buttonCTIS.buttonTextColor=textColorRed
}
onReleased:
{
buttonCTIS.buttonImageColor="Imgs/Images/button_right_blue.png"
buttonCTIS.buttonTextColor=textColorBlue
}
}
}
RightButtons
{
id:buttonSisHavani
x:1050
y:510
buttonName: "Sis Havanı"
MultiPointTouchArea
{
id:touchArea_sisHavani
anchors.fill: parent
onPressed:
{
buttonSisHavani.buttonImageColor="Imgs/Images/button_right_red.png"
buttonSisHavani.buttonTextColor=textColorRed
}
onReleased:
{
buttonSisHavani.buttonImageColor="Imgs/Images/button_right_blue.png"
buttonSisHavani.buttonTextColor=textColorBlue
}
}
}
}
}
}
I have a QML project where I am able to drag & drop rectangles that are in a ListView.
I want to disable the drag&drop feature for the first Item (rectangle) of the ListView.
Here is an example:
Rectangle {
visible: true
width: 1000; height: 1000
ListView {
id: root
width: parent.width; height: parent.height
model: DelegateModel {
id: visualModel
model: myModel
model: ListModel {
id: colorModel
ListElement { someData }
...
}
delegate: MouseArea {
property int visualIndex: DelegateModel.itemsIndex
id: delegateRoot
cursorShape: Qt.PointingHandCursor
width: root.width; height: 100
drag.target: icon
drag.axis: Drag.YAxis
drag.minimumY: 0
property bool current: false
Rectangle {
blablaData
//Something like : if firstItem, disable drag&drop
}
onClicked: {
delegateRoot.current = !delegateRoot.current;
if(current) {
delegateRoot.height = 300
}
else {
delegateRoot.height = 100
}
}
Rectangle {
id: container
anchors.top: icon.bottom
width: root.width-5
height: delegateRoot.height - icon.height
clip: true
border.color: "#81BEF7"
Behavior on implicitHeight {
PropertyAnimation { duration: 100 }
}
Text {
anchors.fill: parent
anchors.margins: 10
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
text: size
}
}
DropArea {
anchors { fill: parent; margins: 15 }
onEntered: {
visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex)
}
}
}
}
}
}
Do you have any idea of how to do it ?
Thanks a lot !
EDIT: Added some features to my example
In the delegate root item, try:
enabled: index ? true : false
I found an easy way to do it, and you can use it for the item you want (not only the first one).
I need to change drag.target and drag.axis in delegateRoot by using and setting a boolean like isDraggable to true or false on each item and then use it like this:
drag.target: isDraggable ? content : undefined
drag.axis: isDraggable ? Drag.YAxis : Drag.None
I have a TextEdit inside a ScrollView. How can I implement logic so that the ScrollView moves when the user presses the up or down arrow key and it moves the text beyond the ScrollView bounds?
//qml
ScrollView {
id: palGenTextScrollView
property int scrollBarWidth: 15
anchors.fill: parent
MouseArea {
id: mouseArea
anchors.fill: parent
onWheel: {
if (wheel.modifiers & Qt.ControlModifier){
if (wheel.angleDelta.y > 0)
{
mainTextEdit.font.pixelSize++
}
else
{
mainTextEdit.font.pixelSize--
}
}
else{
wheel.accepted=false
}
}
}
TextEdit {
id: mainTextEdit
text: fileio.palFileText
font.family: "Courier"
wrapMode: TextEdit.Wrap
selectByMouse: true
//when going out of upward bounds: palGenTextScrollView.flickableItem.contentY--
//when going out of downward bounds: palGenTextScrollView.flickableItem.contentY++
}
}
You can use TextArea which does exactly that for you. If you want to bake your own, take a look at the flickable example in TextEdit's docs in the detailed description.
Note that the TextEdit does not implement scrolling, following the cursor, or other behaviors specific to a look-and-feel. For example, to add flickable scrolling that follows the cursor:
Flickable {
id: flick
width: 300; height: 200;
contentWidth: edit.paintedWidth
contentHeight: edit.paintedHeight
clip: true
function ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}
TextEdit {
id: edit
width: flick.width
height: flick.height
focus: true
wrapMode: TextEdit.Wrap
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
}
}
I am animating the grid item to upwards on swiping up and removes the corresponding item on the animation stopped function.
But the problem is when trying to remove the 0th index item. when removing 0th index item with animation, the whole screen moves up instead of the corresponding item animation.
Is there any solution for this ?
Item {
id: fake_drag
x:0
y:0
width: 288*scaleFactor
height: 215*scaleFactor
property bool isReleased: false
NumberAnimation {
id: animationY
target: fake_drag
property: "y"
from: mouseYValue;
to: -200
duration: 500
onStopped: {
console.log("removed index : ", indexValue)
favouriteapp.remove(indexValue)
}
}
MouseArea {
id: mouseArea
property int difference: 0
width: 288*scaleFactor
height: 215*scaleFactor
anchors.centerIn: parent
onPressed : {
itemInitialYPosition = mouseY
indexValue = index
}
drag.target: fake_drag_image
drag.axis: Drag.YAxis
onReleased: {
onceloaded = false;
}
onMouseYChanged: {
mouseYValue = mouseY
difference = itemInitialYPosition - mouseY
if(!onceloaded) {
if(itemInitialYPosition >= mouseY){
if(difference >= 25){
console.log("swipe..")
animationY.start()
onceloaded = true;
}
}
}
}
Image {
id: fake_drag_image
width: 64; height: 64
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
source: "images/Feature_Galary.png"
states: State {
when: mouseArea.drag.active
ParentChange { target: fake_drag_image; parent: itemid }
AnchorChanges { target: fake_drag_image; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined }
}
}
}
This is the code used in grid item delegate.
I want to implement the following scenario in QML.
Here is a sample/simplified delegate for ListView element:
Component {
Item {
id: container
MouseArea {
anchors.fill: parent
hoverEnabled: true
onClicked: {
container.ListView.view.currentIndex = index
container.forceActiveFocus();
}
onEntered: {
actionList.state = "SHOW";
myItem.state = "HOVER"
}
onExited: {
actionList.state = "HIDE";
myItem.state = "NORMAL"
}
Rectangle {
id: myItem
color: "gray"
anchors.fill: parent
Row {
id: actionList
spacing: 5; anchors.fill: parent
Image {
id: helpAction
source: "" //Some image address
width: 16; height: 16; fillMode: Image.PreserveAspectFit
states: [
State {
name: "NORMAL"
PropertyChanges { target: helpAction; opacity: 0.7 }
},
State {
name: "HOVER"
PropertyChanges { target: helpAction; opacity: 1.0 }
}
]
MouseArea {
hoverEnabled: true
anchors.fill: parent
onEntered: {
parent.state = "HOVER";
}
onExited: {
parent.state = "NORMAL";
}
}
states: [
State {
name: "SHOW"
PropertyChanges { target: actionList; visible: false }
},
State {
name: "HIDE"
PropertyChanges { target: actionList; visible: true }
}
]
}
//Other action buttons...
states: [
// `NORMAL` and `HOVER` states definition here...
]
}
}
}
}
But I have a problem with MouseArea.
Inner MouseArea (actionButton) does not work properly for entered event. When mouse enters on action button, outer MouseArea fires exited event.
Is there any mistake in my code? More generally, how can I implement such a scenario in QML?
I was faced by this same problem, and came across the answer in the QtQuick 5.0 documentation for MouseArea. The answer to this is actually quite simple.
If you want to include child mouse hover events in your parent MouseArea, make you child MouseArea a child of the parent MouseArea:
MouseArea {
id: parent
MouseArea {
id: child
}
}
Since I have a custom Widget type that would be used as the parent view, I ended up with the default property being the children of the MouseArea:
Item {
default property alias children: mouseArea.data
MouseArea {
id: mouseArea
}
}
Iv'e tried a few things but it does not seem possible to hover over two MouseArea simultaneously. The preventStealing and propagateComposedEvents seem to only work when you have a click event. But from the inner MouseArea you can trigger the entered() signal of the other one. Something like this:
import QtQuick 2.1
Rectangle {
width: 500
height: 500
Rectangle {
width:300
height: 300
color: "red"
MouseArea {
id: big
anchors.fill: parent
hoverEnabled:true
onEntered: {
console.log("ENTERED BIG mousearea");
}
onExited: {
console.log("EXITED BIG mousearea");
}
}
Rectangle {
anchors.centerIn: parent
height: 100
width: 100
color: "green"
MouseArea {
anchors.fill: parent
hoverEnabled:true
onEntered: {
console.log("ENTERED small mousearea");
big.entered();
}
onExited: {
console.log("EXITED small mousearea");
big.exited();
}
}
}
}
}
The issue is that the exited() signal from the containing MouseArea will be called before calling the entered() back again. So you might need to "delay" the change of state in exited() just to make sure you really want to hide your action buttons. Another solution would be to save the current mouse position and hide the buttons ONLY if exited() is called with the mouse on one of its border.
make states for each state of the elements in the View then you can use things like if statements or case statements to change these properties In Other words, Try not to set your elements up to work on MouseArea but on properties And set the Elements properties to work on the set properties I hope that this helps if not here is example:
EDIT I added the color to be transparent. if there is no mouse what so ever. If I was using a Image I would use opacity then add a bunch of Behaviors also But this is a working
example
import QtQuick 2.0
Rectangle {
width: 360
height: 360
property string state1:"OutMouse"
property string state2: "OutMouse"
property string state3: "OutMouse"
property string state4: "OutMouse"
Rectangle{
id:blueRec
width: parent.width
height: parent.height / 6
color: state1 === "InMouse" ? "blue" : "green"
MouseArea{
anchors.fill: blueRec
hoverEnabled: true
onEntered: state1 = "InMouse"
onExited: {
if (state1 === state2 || state3 || state4){
state1 = "InMouse"
}
if(state1 !== state2 || state3 || state4)
{
state1 = "OutMouse"
}
}
}
Text {
text: state1=== "InMouse"? qsTr("foo") :"bar"
anchors.centerIn: blueRec
}
Row{
width: parent.width
height: parent.height / 4
spacing: 2
anchors{
left: parent.left
verticalCenter: blueRec.verticalCenter
leftMargin: blueRec.width / 12
}
Rectangle{
id: rec1
height: parent.height;
width: height
color: {
if ( state3 === "InMouse")
return "gray"
if (state1 === "OutMouse")
return "transparent"
else
return "white"}
MouseArea{
id: rec1M
anchors.fill: parent
hoverEnabled: true
onEntered:{
state1 = "InMouse"
state2 = "InMouse"
}
onExited: state2 = "OutMouse"
}
}
Rectangle{
id: rec2
height: parent.height ;
width: height
color: {
if (state3 === "InMouse")
return "gray"
if (state1 === "OutMouse")
return "transparent"
else
return "white"
}
MouseArea{
id: rec2M
anchors.fill: parent
hoverEnabled: true
onEntered:{
state1 = "InMouse"
state3 = "InMouse"
}
onExited: state3 = "OutMouse"
}
}
Rectangle{
id: rec3
height: parent.height;
width: height
color:{
if (state4 === "InMouse")
return "gray"
if (state1 === "OutMouse")
return "transparent"
else
return "white"
}
MouseArea{
id: rec3M
anchors.fill: parent
hoverEnabled: true
onEntered:{
state4 = "InMouse"
state1 = "InMouse"
}
onExited: state4 = "OutMouse"
}
}
}
}
}
Try this:
add a signal to the inner area that's emitted on mouse enter.
Connect the signal to the outer area.
The signal causes the outer area to enter the hovered state.
Mouse exit on both will still cancel hover state. As you move the mouse off the controls it should work correctly without any extra code