QML ListView won't respond on mouse click - qt

Hi all I have tried several things but I can't make my ListView to response to mouse click.
Here is my code of ListView:
ListView {
id: listview1
x: 0
y: 82
// width: 574
// height: 967
width: window.width
height: window.height
visible: true
keyNavigationWraps: false
boundsBehavior: Flickable.DragAndOvershootBounds
opacity: 1
maximumFlickVelocity: 2500
anchors.leftMargin: 0
highlightMoveSpeed: 489
contentWidth: 0
preferredHighlightEnd: 2
spacing: 5
highlightRangeMode: ListView.NoHighlightRange
snapMode: ListView.SnapToItem
anchors.bottomMargin: 0
anchors.rightMargin: 0
anchors.topMargin: 82
anchors.fill: parent
model: myModel
delegate:Component {
//id: contactDelegate
Item {
property variant myData: model
width: 574; height: 90
Column {
x: 12
y: 0
width: 562
height: 90
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 12
anchors.topMargin: 0
anchors.fill: parent
spacing: 2
Text { text: '<b>ID: </b> ' + id_korisnika ; verticalAlignment: Text.AlignTop; wrapMode: Text.NoWrap; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Ime: </b> ' + ime_korisnika ; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Prezime: </b> ' + prezime_korisnika; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { height: 16; text: '<b>Broj telefona: </b> ' + broj_korisnika ; verticalAlignment: Text.AlignVCenter; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
Text { text: '<b>Adresa: </b> ' + adresa_korisnika ; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
}
}
}
}
//delegate: contactDelegate
highlight: Rectangle {color:"black"; radius: 5; opacity: 0.7
}
focus: true
}
I have tried to put my code in all areeas but I can't make it work. any suggestion is nice.

See KeyInterction sample from QtCreator. There is your answer :)
You can see sample delegate as follow:
ListViewDelegate:
Item {
id: container
width: ListView.view.width; height: 60; anchors.leftMargin: 10; anchors.rightMargin: 10
Rectangle {
id: content
anchors.centerIn: parent; width: container.width - 40; height: container.height - 10
color: "transparent"
antialiasing: true
radius: 10
Rectangle { anchors.fill: parent; anchors.margins: 3; color: "#91AA9D"; antialiasing: true; radius: 8 }
}
Text {
id: label
anchors.centerIn: content
text: "List element " + (index + 1)
color: "#193441"
font.pixelSize: 14
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
container.ListView.view.currentIndex = index
container.forceActiveFocus()
}
}
}
The key is MouseArea section.
Good luck - S.M.Mousavi

i dont see onClicked handler code in your MouseArea (mouse_area1). How are you trying to capture/respond the mouse click.
Try following code and see what happens.
MouseArea {
id: mouse_area1
z: 1
hoverEnabled: false
anchors.fill: parent
onClicked:{
console.log("test");
}
}

Related

I need to set ScrollView on my rectangle, how do I do this?

The whole problem is
I tried to implement it, but always faced some issues,
Like, ScrollView Covers the whole content or I get the issue where my Column-item
Cannot anchor to an item that isn't a parent or sibling.
The screen of my the whole tab is here
Tab where I need to place scrollview
Rectangle{
id:root
color: "white"
anchors.fill: parent
Rectangle{
id:label
color: "#F6F6F6"
width: root.width
height: root.height * 0.20
z: parent.z + 4
Text{
width: label.width
height: label.height
text: getImageName()
font.family: "Abel"
font.pointSize: 24
color: "#4A3F35"
lineHeight: 24
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
Column {
anchors.top: label.bottom
anchors.horizontalCenter: root.horizontalCenter
anchors.topMargin: label.height * 0.5
spacing: label.height * 0.3
Repeater {
id:secondTabRepeater
model: getAmountOfMountedVolumes()
Rectangle{
id:driveInfoRectangle
width: root.width * 0.87
height: root.height * 0.20
color:"white"
radius: 5
border.color: getUsbDevice(index)
border.width: 2
//border.color: "#4A3F35"
Text {
id: memorySizeText
text: getUsbSpace(index) + " Gb"
anchors.fill: parent
font.family: "Abel"
font.pointSize: 22
color: "#4A3F35"
lineHeight: 22
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
Text {
id: drivePathText
text: getUsbDevice(index)
anchors.fill: parent
anchors.bottomMargin: 5
font.family: "Abel"
font.pointSize: 14
color: "#4A3F35"
lineHeight: 12
verticalAlignment: Text.AlignBottom
horizontalAlignment: Text.AlignHCenter
}
}
}
}
}
Try This : I add ScrollBar inside one Flickable to your code and now it works.
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
id:root
color: "white"
anchors.fill: parent
Rectangle{
id:label
color: "#F6F6F6"
width: root.width
height: root.height * 0.20
z: parent.z + 4
Text{
width: label.width
height: label.height
text:"Example"
font.family: "Abel"
font.pointSize: 24
color: "#4A3F35"
lineHeight: 24
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
Flickable{
width: parent.width
height: parent.height
contentHeight: mColumnId.implicitHeight
contentWidth: mColumnId.implicitWidth
Column {
id:mColumnId
anchors.top: label.bottom
anchors.horizontalCenter: root.horizontalCenter
anchors.topMargin: label.height * 0.5
spacing: label.height * 0.3
Repeater {
id:secondTabRepeater
model: 10
Rectangle{
id:driveInfoRectangle
width: root.width * 0.87
height: root.height * 0.20
color:"white"
radius: 5
border.color: getUsbDevice(index)
border.width: 2
//border.color: "#4A3F35"
Text {
id: memorySizeText
text: getUsbSpace(index) + " Gb"
anchors.fill: parent
font.family: "Abel"
font.pointSize: 22
color: "#4A3F35"
lineHeight: 22
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
Text {
id: drivePathText
text: getUsbDevice(index)
anchors.fill: parent
anchors.bottomMargin: 5
font.family: "Abel"
font.pointSize: 14
color: "#4A3F35"
lineHeight: 12
verticalAlignment: Text.AlignBottom
horizontalAlignment: Text.AlignHCenter
}
}
}
}
ScrollBar.vertical: ScrollBar{}
ScrollBar.horizontal: ScrollBar{}
}
}
}

Scroll a text by mouse Click and hold like a touch screen

I need to scroll by mouse click ,the code bellow works very well when scroll bars clicked or on wheel,BUT I need by mouse click and hold in anywhere in text.
ScrollView {
id: scrollView
x: 15
y: 38
width: 647
height: 168
visible: true
spacing: 2
focusPolicy: Qt.ClickFocus
clip: true
Text {
id: text1
x: -17
y: -27
opacity: 1
visible: true
color: "#ffffff"
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
font.pixelSize: 16
verticalAlignment: Text.AlignVCenter
wrapMode: Text.WrapAnywhere
font.preferShaping: false
font.kerning: false
font.styleName: "Bold"
style: Text.Normal
font.weight: Font.Medium
font.bold: true
clip: true
anchors.topMargin: 0
anchors.bottomMargin: -8
anchors.leftMargin: 0
anchors.rightMargin: 0
font.family: "Courier New"
}
}
You can use a Flickable instead of ScrollView.
A really basic sample would be like this.
Flickable {
width: 200; height: 100
contentWidth: text.width; contentHeight: text.height
// These create default scroll bars for both orientations.
// You could modify the scroll bars if you want.
ScrollBar.vertical: ScrollBar{}
ScrollBar.horizontal: ScrollBar{}
Text{ id: text; width: 200; height: 300; text: "Some long text here" }
}

ListView cannot load delegate from another qml file

i want to use ListViewDelegate.qml as a delegate for my list view items on my Kos.qml. but it giving me this error. i also try using loader but it seems didn't work, i try to remove the curly braces and it load the Kos.qml page but it didn't render the list view
file:/ngomahyukv2/Kos.qml:55: ListViewDelegate is not a type
File name case mismatch
#Kos.qml
i use another file PageBackground.qml as a background
import QtQuick 2.4
import QtQuick.Controls 2.3
PageBackground {
id: kos
width: 640
height: 480
Text {
id: element
x: 6
y: 20
width: 24
height: 32
color: "#ffffff"
text: qsTr("<")
font.strikeout: false
styleColor: "#ffffff"
font.underline: false
font.italic: false
font.bold: true
font.pixelSize: 25
MouseArea {
id: mouseArea
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent
onClicked: kosloader.source = ""
}
}
ListView {
id: listView
x: 0
y: 69
width: 640
height: 410
model: ListModel{
// need to be in for loop and data from database
ListElement{
imagePath : "static/Bisnis-kos-kosan.png"
kosName : "Kos Name"
kosAlamat : "Jalan Lorem"
kosJumlahKamar: "5"
kosGender : "Laki-laki"
kosHarga: "7000000"
kosProfile: "KosSpec.qml"
ownerContact: "instgram.com"
}
}
delegate: ListViewDelegate { }
}
Loader{
id: kosspec
visible: false
source: ""
}
}
#ListViewDelegate.qml
import QtQuick 2.0
import QtQuick.Controls 2.13
Item {
id: listviewdelegate
Image {
id: idthumbnail
x: 8
y: 8
width: 227
height: 158
source: imagePath
fillMode: Image.PreserveAspectCrop
}
Text {
id: idnamakos
x: 252
y: 8
text: kosName
font.bold: true
font.family: "Verdana"
wrapMode: Text.WordWrap
font.pixelSize: 22
}
Text {
id: idalamat
x: 251
y: 40
width: 301
height: 17
text: qsTr("Alamat : " + kosAlamat)
wrapMode: Text.WordWrap
font.pixelSize: 12
font.family: "Verdana"
}
Text {
id: idjumlahkamar
x: 252
y: 63
width: 240
height: 14
text: qsTr("Jumlah Kamar : " + kosJumlahKamar)
wrapMode: Text.WordWrap
font.pixelSize: 12
font.family: "Verdana"
}
Text {
id: idgender
x: 251
y: 83
width: 265
height: 14
text: qsTr("Gender : " + kosGender)
wrapMode: Text.WordWrap
font.pixelSize: 12
font.family: "Verdana"
}
Text {
id: idharga
x: 251
y: 103
width: 373
height: 14
text: qsTr("Harga : " + kosHarga)
wrapMode: Text.WordWrap
font.pixelSize: 12
elide: Text.ElideNone
font.family: "Verdana"
}
Button {
id: buttonCek
x: 510
y: 131
width: 96
height: 35
visible: true
font.family: "Verdana"
font.pixelSize: 16
background: Rectangle{
color: "#ef3644"
anchors.fill: parent
}
contentItem: Text {
id: cek
text: "CEK"
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
font.bold: true
font.pointSize: 10
horizontalAlignment: Text.AlignHCenter
color: "#ffffff"
}
MouseArea {
id: mouseAreaCek
anchors.fill: parent
onClicked: {
kosspec.visible = true
kosspec.source = kosProfile
}
}
}
Button {
id: buttonHubungi
x: 400
y: 131
width: 96
height: 35
font.family: "Verdana"
visible: true
contentItem: Text {
id: name1
color: "#ef3644"
text: "HUBUNGI"
anchors.fill: parent
font.bold: true
font.pointSize: 10
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
background: Rectangle {
color: "#00f1f0f0"
border.width: 4
border.color: "#ef3644"
anchors.fill: parent
}
font.pixelSize: 16
MouseArea {
id: mouseAreaHubungi
anchors.fill: parent
onClicked: {
Qt.openUrlExternally (ownerContact);
}
}
}
ToolSeparator {
id: toolSeparator
x: 5
y: 172
width: 600
height: 15
orientation: Qt.Horizontal
}
}
As the error message says, it seems to have to do with the casing of your file. Custom types need to begin with a capital letter, but also check that you have got the V and D in the correct case.
Also, is the file in the same directory? Do you have files named the same but with different casing in parallel? Directories?
I'm pretty confident that the issue is in the file name, or the name of a file that interferes with the file name. However, if you want to create a type module and refer to the file that way, you can also poke at using qmldir files: https://doc.qt.io/qt-5/qtqml-modules-qmldir.html .
But try to find something odd in your files first.

QML ScrollBar combined with ListView

I'm new to QML and QT so don't blame me if this question is going to sound stupid for most of you but I've search all over the internet without any luck in founding an answer.
What I'm trying to do:
I'm having a ScrollView which has inside of it a ScrollBar and a ListView.
I want that at the moment when I'm scrolling the ListView elements to also move the bar from ScrollBar. In other words, I want to use the ScrollBar as an overall view of your current position, you are not supposed to touch that, its only purpose is for viewing.
My Code:
ScrollView{
implicitHeight: 100
implicitWidth: 50
anchors.fill: parent
ScrollBar.horizontal: ScrollBar{
id: hbar
active: true
policy: ScrollBar.AlwaysOn
anchors {
left: parent.left
top: parent.top
right: parent.right
}
background: Rectangle {
implicitWidth: 100
implicitHeight: 50
opacity: enabled ? 1 : 0.3
color: hbar.down ? "#red" : "black"
}
contentItem: Rectangle {
implicitWidth: 6
implicitHeight: 100
radius: width / 2
color: hbar.pressed ? "#81e889" : "#c2f4c6"
}
}
ListView {
id: listViewParent
height: listViewID.height/10*6
contentHeight: height*2
contentWidth: width*2
clip: false
interactive: false
keyNavigationWraps: true
anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left
anchors.leftMargin: 0
enabled: true
scale: 1
transformOrigin: Item.Center
anchors.verticalCenter: parent.verticalCenter
boundsBehavior: Flickable.DragAndOvershootBounds
flickableDirection: Flickable.HorizontalFlick
highlightMoveDuration: 0
cacheBuffer: 300
snapMode: ListView.SnapToItem
layoutDirection: Qt.LeftToRight
orientation: ListView.Vertical
model: 1
delegate:
ListView {
width: parent.width;
height: parent.height;
spacing: listViewID.width/8/9
model: MovieListModel {}
orientation: ListView.Horizontal
id: listid
delegate:
Rectangle {
property int recDynamicHeight: listViewID.height/10*6
property int recOriginalHeight: listViewID.height/10*6
property int recDynamiclWidth: listViewID.width/7
property int recOriginalWidth: listViewID.width/7
id: rectPer
width: recDynamiclWidth
height: recDynamicHeight
Image {
id: image1
anchors.fill: parent;
source: model.imgUrl
}
Text {
property bool isVisible: false
color: "#ffffff"
anchors.fill: parent
visible: textid.isVisible
id: textid
text: model.title
font.bold: true
horizontalAlignment: Text.AlignLeft
font.pixelSize: listViewID.width/8/9
topPadding: listViewID.width/8/9
leftPadding: listViewID.width/8/9
}
Text {
anchors.topMargin: listViewID.width/8/9
color: "#ffffff"
anchors.fill: parent
visible: textid.isVisible
id: yearId
text: model.year
horizontalAlignment: Text.AlignLeft
font.pixelSize: listViewID.width/8/9
topPadding: listViewID.width/8/9*2
leftPadding: listViewID.width/8/9
}
MouseArea {
anchors.fill: parent
onPressed: {
rectPer.recDynamicHeight = rectPer.recOriginalHeight;
rectPer.recDynamicHeight += rectPer.recOriginalHeight/10;
rectPer.recDynamiclWidth += rectPer.recOriginalWidth/10;
console.log(textid.isVisible);
textid.isVisible = true;
textid.visible = textid.isVisible;
console.log(sideButtonID.x);
console.log(sideButtonID.y);
console.log(model.year + " clicked");
}
onClicked: {
console.log("INDEX: " + model.id)
load_page(PageType.movie_detailed_view, model.title, model.description, model.imgUrl, model.type, model.year)
}
onReleased: {
rectPer.recDynamicHeight = rectPer.recOriginalHeight;
rectPer.recDynamiclWidth = rectPer.recOriginalWidth;
textid.isVisible = false;
textid.visible = textid.isVisible;
}
}
}
}
}
}
Layout:
You could try use a Flickable instead of a ScrollView and spawn a ListView there(delegates: Rectangles). Then, spawn the Scrollbar inside the ListView

How to animate a message that appears on bottom?

I'm showing a message on the bottom:
Msg.qml
import QtQuick 2.4
Item {
property alias text: mf.text
anchors.fill: parent
antialiasing: false
opacity: 0.9
z: 100
MsgForm {
id: mf
width: parent.width
y: parent.height - height - 5
}
}
MsgForm.ui.qml
import QtQuick 2.4
Item {
property alias text: msg.text
width: 200
id: message
height: msg.height+10
Rectangle {
id: rectangle
color: "#fb9191"
anchors.fill: parent
border.color: "#fd6666"
border.width: 2
Text {
id: msg
anchors.top: parent.top
anchors.topMargin: 2
textFormat: Text.PlainText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.left: parent.left
anchors.leftMargin: 4
wrapMode: Text.WordWrap
clip: false
font.bold: true
font.pointSize: 12
font.family: "Tahoma"
}
}
}
How can I animate the form to appear from the bottom smoothly?
After the animation, if the window resizes, the message must stay always on the bottom.
You can play with anchors.bottomMargin property to raise the message item from the bottom.
import QtQuick 2.4
Item {
property alias text: mf.text
anchors.fill: parent
antialiasing: false
opacity: 0.9
z: 100
MsgForm {
id: mf
property bool showing: false
width: parent.width
anchors{
bottom: parent.bottom
bottomMargin: mf.showing ? 0 : -mf.height
Behavior on bottomMargin{
NumberAnimation{ }
}
}
}
}
Thanks everyone. In the end I've solved the issue by following advices received in the qtcentre forum.
The desired effect can be achieved easily by defining a local numerical property that is use to bind to either an anchors.XXXXmargin or the y property expression.
Following this approach a possible solution is the following:
MsgForm {
property bool showing: false
property int position: showing ? height : 0
width: parent.width
y: parent.height - position
Behavior on position {
NumberAnimation {duration: 500}
}
}
You can make an animation on the opacity change:
Msg.qml
import QtQuick 2.4
Item {
property alias text: mf.text
anchors.fill: parent
antialiasing: false
opacity: 0.9
z: 100
MouseArea{
anchors.fill: parent
onClicked: mf.opacity = !mf.opacity
}
MsgForm {
id: mf
//y: parent.height - height - 5
opacity:0
Behavior on opacity {
NumberAnimation{
duration:600
}
}
width: parent.width
anchors.bottom: parent.bottom
}
}
or any other NumberAnimation. I recommand you to create States, within it do some propertyChanges, and on some actions, example button clicked change states.
example in your MsgForm.ui.qml add:
MouseArea{
anchors.fill: parent
onClicked: mf.state= "show"
}
and in the action, example:
in my mouseArea I change the state of mf
MouseArea{
anchors.fill: parent
onClicked: mf.state= "show"
}
If you want an Animation on the y try this:
MsgForm.ui.qml
import QtQuick 2.4
Item {
id: message
property alias text: msg.text
width: parent.width
height: msg.height+10
Rectangle {
id: rectangle
color: "#fb9191"
anchors.fill: parent
border.color: "#fd6666"
border.width: 2
Text {
id: msg
anchors.top: parent.top
anchors.topMargin: 2
textFormat: Text.PlainText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.left: parent.left
anchors.leftMargin: 4
wrapMode: Text.WordWrap
clip: false
font.bold: true
font.pointSize: 12
font.family: "Tahoma"
}
}
Behavior on y {
NumberAnimation{
duration:300
}
}
states: [
State {
name: "show"
PropertyChanges {
target: message
y: parent.height - height
}
},
State {
name: "hide"
PropertyChanges {
target: message
y: parent.height + height + 5
}
}
]
}
Msg.qml
import QtQuick 2.4
Rectangle {
property alias text: mf.text
width:800
height: 480
antialiasing: false
opacity: 0.9
z: 100
MouseArea{
anchors.fill: parent
onClicked: mf.state= "show"
}
MsgForm {
id: mf
//y: parent.height - height - 5
y: parent.height +height + 5
width: parent.width
}
}

Resources