Qt5 QML: swap two Items in ListView - qt

i'm using model view in qml.
ListView{
id: targetParameter
width: parent.width
height: parent.height
anchors.top: parent.top
anchors.left: parent.left
anchors.leftMargin: 20
spacing: 10
orientation: ListView.Horizontal
interactive: false
model: proxyModelCharacterization
delegate: ParameterChangeTarget {
paramWidht: {
if(name === "NAME"){
targetParameter.width * 0.11
}else{
targetParameter.width * 0.42
}
}
paramHeight: targetParameter.height * 0.95
}
}
with this list view, I have three elements (for example Rectangle).
rect1, rect2, rect3.
i want to swap rect2 and rect3, but i can't change the order in which they are instantiated on my controller.
how can i swap two element on list view?
every kind of help or suggestion are greatly appreciated.

You can use the .move() method to move an element in a ListModel.
In the following example, the elements are added in the ListModel once, in the Component.onCompleted signal. You can then move them up or down (in the model and hence, the view) by clicking on the corresponding buttons. The buttons use the move() method of ListModel.
Example:
ListModel {
id: listModel
}
ListView {
id: listView
anchors.fill: parent
spacing: 10
model: listModel
delegate: Rectangle {
height: btnMoveUp.height
width: listView.width
color: "red"
Button {
id: btnMoveUp
anchors.left: parent.left
text: "Move Up"
enabled: index !== 0
onClicked: listModel.move(index, index-1,1)
}
Button {
id: btnMoveDown
anchors.left: btnMoveUp.right
text: "Move Down"
enabled: index !== listModel.count - 1
onClicked: listModel.move(index, index+1,1)
}
Text {
anchors {
left: btnMoveDown.right
right: parent.right
verticalCenter: parent.verticalCenter
}
text: model.name
}
}
}
Component.onCompleted: {
listModel.append({name: "Item 1"});
listModel.append({name: "Item 2"});
listModel.append({name: "Item 3"});
listModel.append({name: "Item 4"});
listModel.append({name: "Item 5"});
}

Related

QML Dynamically Update ListModel

I'm confused at some point in QML as I'm new to it. The simplified QML codes define a window that includes one ListView which includes my delegate Items. One button loads and unloads this ListView Element. When I load and unload the ListView Element, all text that I wrote inside the TextInput is reset from listModel as I expected. So I need to dynamically update listModel thus I will not lose the text that I wrote in TextInput. I added a Keys.onPressed in TextInput to achieve that. But it works with some logical error. When I type, let's say "aaaa", then I unload the ListView and load again via Button, what I get is "aaa" (the last letter is not passed to listModel). This is understandable but how can I update the list model roles dynamically in this example?
main.qml
ApplicationWindow {
id: applicationWindow
width: 300
height: 200
visible: true
title: qsTr("01_Change_Model_Data")
ListModel {
id: listModel1
ListElement {labelText: "Text Field 1:"; textInput_text : "This is text 1"}
ListElement {labelText: "Text Field 2:"; textInput_text : "This is text 2"}
}
Button {
id: loadUnloadBtn
height: 24
width: 50
text: "Load"
anchors {
right: parent.right
rightMargin: 20
top: parent.top
topMargin: 10
}
onClicked: {
if(listAreaLoader.source == "") {
listAreaLoader.source = "ListArea.qml"
}else {
listAreaLoader.source = ""
}
}
}
Loader {
id: listAreaLoader
anchors {
top: parent.top
topMargin: 10
bottom: parent.bottom
bottomMargin: 10
left: parent.left
leftMargin: 10
right: parent.right
rightMargin: 80
}
source: ""
}
}
ListArea.qml:
Item {
id: listViewDelegate
ListView {
id: listView1
anchors.fill: parent
model: listModel1
delegate: listElementDelegate
spacing: 6
}
Component {
id: listElementDelegate
Rectangle {
color: "#00000000"
height: 24
width: 50
Label {
id: label1
text: labelText
}
Rectangle {
color: "grey"
radius: 4
width: 100
height: 20
anchors {
verticalCenter: label1.verticalCenter
left: label1.right
leftMargin: 10
}
TextInput {
id: textInput1
anchors.fill: parent
leftPadding: 5
rightPadding: 5
clip: true
verticalAlignment: Text.AlignVCenter
text: textInput_text
Keys.onPressed: {
listView1.currentIndex = index
listModel1.setProperty(index, "textInput_text", textInput1.text)
}
}
}
}
}
}

Long TabBar - adding a size and position indicator to reflect the presence of off-screen tabs

I have a tab bar with a stacklayout like the following:
Rectangle {
id: rect
height: 190
anchors.right: parent.right
anchors.left: parent.left
color: "transparent"
anchors.top: uniqueHandleText.bottom
anchors.topMargin: 100
TabBar {
id: frame
anchors.right: parent.right
anchors.left: parent.left
background: Rectangle {
color: "#737373"
}
x: -hbar.position * width
Repeater {
model: wizard.categories
TabButton {
id: tabData
property bool selected: false
text: modelData.name
width: 200
font.pixelSize: 18
contentItem: Text {
text: tabData.text
font: tabData.font
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
wrapMode: Text.WordWrap
color: "#FFFFFF"
}
background: Rectangle {
implicitWidth: frame.width
implicitHeight: 180
opacity: enabled ? 1 : 0.3
color: tabData.checked ? "#BD9CBE": "#737373"
}
}
}
}
ScrollBar {
id: hbar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Horizontal
size: rect.width / frame.width
anchors.left: parent.left
anchors.right: parent.right
anchors.top: frame.bottom
}
Text {
font.pixelSize: 18
text: "Next"
anchors.right: parent.right
visible: frame.x != frame.width ? true: false
}
StackLayout {
id: stack1
anchors.left: parent.left
anchors.right: parent.right
anchors.top: frame.bottom
currentIndex: frame.currentIndex
Repeater {
model: wizard.categories
Item {
id: homeTab
TabBar {
id: homeTabTab
anchors.right: parent.right
anchors.left: parent.left
anchors.top: parent.top
height: 180
background: Rectangle {
color: "#958096"
}
Repeater {
model: modelData.sub_categories
TabButton {
property bool selected: false
id: currentTab
text: modelData.name
width: 200
font.pixelSize: 18
background: Rectangle {
implicitWidth: frame.width
implicitHeight: 180
opacity: enabled ? 1 : 0.3
color: currentTab.checked ? "#958096": "#8D758E"
}
contentItem: Text {
text: currentTab.text
font: currentTab.font
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
wrapMode: Text.WordWrap
color: "#FFFFFF"
MouseArea {
anchors.fill: parent
onClicked: {
if(currentTab.checked){
currentTab.checked = false
} else {
currentTab.checked = true
}
}
onDoubleClicked: {
currentTab.selected = true
var found = false;
var someText = frame.itemAt(stack1.currentIndex).text;
print(someText)
for(var i = 0; i<wizard.selectedSkills.count; i++){
if(wizard.selectedSkills.get(i).name === someText){
wizard.selectedSkills.get(i).sub_categories.append({"name":currentTab.text});
wizard.skills.push({"name": someText})
found = true;
}
}
if(!found){
print(currentTab.text)
wizard.selectedSkills.append({"name":someText, "sub_categories":[{"name":currentTab.text}]})
}
print(window.selectedSkills)
}
}
}
}
}
}
}
}
}
}
I've tried many different things to add a scrollbar or to figure out how to use the flickable functionality that TabBar has. However, the documentation doesn't specify how it works, it just does. Therefore, they are not accessible (or even rewritteable, to use those properties). I want to add a small indicator like an arrow to specify that there is more elements for ease of navigation on desktop on top of the TabBar functionality.
It doesn't seem like the necessary properties are exposed in order to make this happen the easy way.
However, since this is QML, it means the whole object tree is gaping wide open to introspection, allowing us to establish that the item that does the flicking is the contentItem of a ListView inside the Container the ToolBar inherits. The view happens to be the second visible child, although this is technically "private implementation" that one should not rely on. So it is better to take some extra care to establish whether or not you have the correct object.
ApplicationWindow {
id: main
width: 640
height: 480
visible: true
TabBar {
id: toolbar
width: parent.width
height: 50
Repeater {
model: 10
TabButton {
text: "test " + index
width: 100
}
}
}
Rectangle {
height: 5
width: main.width * (view ? view.visibleArea.widthRatio : toolbar.width / toolbar.contentWidth)
color: "red"
anchors.top: toolbar.bottom
x: view ? (main.width - width) * (view.contentX / (view.contentWidth - view.width)) : 0
}
property ListView view: {
var l = toolbar.visibleChildren.length
while (--l) if ("cacheBuffer" in toolbar.visibleChildren[l]) return toolbar.visibleChildren[l]
return null
}
}
And there you have it. We iterate the tabview children until we find one that has a property cacheBuffer which is fairly unique to ListView, and once we have that, we can access the needed properties. As you see, for the indicator width we can do even without the list view, as the toolbar exposes a contentWidth property, but for the indicator position there is no workaround.
And it works:

How to get index of particular item in ListModel?

I've got a little problem with GridView component. I have to change some property of some objects during runtime. To do this, I can use the setProperty() function. However, this function needs index to work. And here the problem appears becouse, all items are created during runtime.
How can I get index of specific item?
Let me explain a little bit more. The item I'm reffering to is just a simple rectangle with MouseArea. Here's its code :
Rectangle {
property var colorR
id: namE
width: 100
height: 100
color: colorR
MouseArea {
id: mouseArea1
anchors.fill: parent
onClicked:
{
console.log(parent.nameIndex)
}
}
}
And here is my GridView code :
GridView {
id: gridView1
anchors.rightMargin: 5
anchors.leftMargin: 5
anchors.bottomMargin: 5
anchors.topMargin: 10
anchors.fill: parent
flickableDirection: Flickable.VerticalFlick
snapMode: GridView.NoSnap
highlightRangeMode: GridView.NoHighlightRange
highlightMoveDuration: 148
flow: GridView.FlowLeftToRight
layoutDirection: Qt.RightToLeft
model: ListModel {id: listModelMy}
delegate: Column {ColorBlock{}}
cellHeight: 100
cellWidth: 100
}
And here is my code to dynamicly create items in GridView
Rectangle {
id: addButton
width: 65
height: 55
color: "#b04b4b"
border.color: "#252323"
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
anchors.right: parent.right
anchors.rightMargin: 8
Image {
id: image1
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.bottomMargin: 10
anchors.topMargin: 10
anchors.fill: parent
source: "svgIcons/add.svg"
MouseArea {
id: mouseArea1
anchors.fill: parent
onClicked:
{
var test = listModelMy.append(ListElement);
}
}
}
}
I don't really know how to get this index. Or I'm just blind and I'm missing something obvious.
There is an index attached property available for use inside the delegate. For every delegate instance it will correspond to the associated list model element index.
GridView {
anchors.fill: parent
model: ListModel { id: mod }
delegate: Rectangle {
width: 50
height: 50
color: "red"
Text {
text: index
anchors.centerIn: parent
}
}
}

How to get model in onCurrentItemChanged with QtQuick ListView

I use the listview like this:
ListView {
id: list
clip: true
spacing: 2
anchors.fill: parent
model: datas
delegate: listItem
onCurrentItemChanged: {
//I want get the part of the model which belong to currentItem
}
}
Component {
id: listItem
Rectangle {
height: 30
anchors.left: parent.left
anchors.right: parent.right
color: "green"
Label {
anchors.fill: parent
text: uuid
color: "white"
}
MouseArea{
anchors.fill: parent;
onClicked: {
console.log("study clicked")
console.log(uuid)
studyList.currentIndex=index
//component.selected(uuid)
}
}
}
}
I want get the part of the model which belong to currentItem in onCurrentItemChanged.
For example:
the model is
ListModel{
ListElement{uuid:aaaa}
ListElement{uuid:bbbb}
ListElement{uuid:cccc}
}
So there should be 3 item.
If I click the second one, I can get the ListElement which uuid is bbbb.
Is there any way?
You can do:
onCurrentIndexChanged: {
//I want get the part of the model which belong to currentItem
console.log(datas.get(currentIndex))
}
then the code will be
Item{
width: 800
height: 480
ListModel{
id: datas
ListElement{uuid:"aaaa"}
ListElement{uuid:"bbbb"}
ListElement{uuid:"cccc"}
}
ListView {
id: list
clip: true
spacing: 2
anchors.fill: parent
model: datas
delegate: listItem
onCurrentIndexChanged: {
//I want get the part of the model which belong to currentItem
console.log(datas.get(currentIndex).uuid)
}
}
Component {
id: listItem
Rectangle {
height: 30
anchors.left: parent.left
anchors.right: parent.right
color: "green"
Label {
anchors.fill: parent
text: uuid
color: "white"
}
MouseArea{
anchors.fill: parent;
onClicked: {
list.currentIndex=index
}
}
}
}
}
or you can try this approach, when button clicked emit a signal with parameters, the current element and the index or only the element .
example:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Item{
width: 800
height: 480
ListModel{
id: datas
ListElement{uuid:"aaaa"}
ListElement{uuid:"bbbb"}
ListElement{uuid:"cccc"}
}
ListView {
id: list
clip: true
spacing: 2
anchors.fill: parent
model: datas
delegate: listItem
signal sgnElementClicked(var element, var index)
// onCurrentIndexChanged: {
// //I want get the part of the model which belong to currentItem
// console.log(currentItem)
// }
onSgnElementClicked: console.log(element.uuid, index)
}
Component {
id: listItem
Rectangle {
height: 30
anchors.left: parent.left
anchors.right: parent.right
color: "green"
Label {
anchors.fill: parent
text: uuid
color: "white"
}
MouseArea{
anchors.fill: parent;
onClicked: {
// console.log("study clicked")
//console.log(uuid)
list.sgnElementClicked(datas.get(index), index)
/// studyList.currentIndex=index
//component.selected(uuid)
}
}
}
}
}

Qt, QML ListView and Desktop App

My question is kind of a two part conditional question. I have a desktop application I'm writing in C++/Qt. In the app I have a couple lists that I want to decorate and add list items with icons and rich text.
I first attempted to do this with the QWidget world but the more I looked into it, the more I thought QML might be a better option. But now I'm wondering about that as well since it seems that QML Is more geared toward touch screen devices. Not to mention that my progress with QML has been frusating. Give them QML below, I cannot figure out how to: (1) get an item to highlight when I click it and (2) add a scroll bar:
import QtQuick 1.0
Item
{
width: 300
height: 200
ListModel
{
id: myModel2
ListElement { text: "List Item 1" }
ListElement { text: "List Item 2" }
ListElement { text: "List Item 3" }
ListElement { text: "List Item 4" }
ListElement { text: "List Item 5" }
ListElement { text: "List Item 6" }
}
Component
{
id: beerDelegate
Rectangle
{
id: beerDelegateRectangle
height: beerDelegateText.height * 1.5
width: parent.width
Text
{
id: beerDelegateText
text: "<b>" + modelData + "</b> <i>(" + modelData + ")</i>"
}
MouseArea
{
anchors.fill: parent
onClicked:
{
console.log("clicked: " + modelData + " at index: " + index);
beerList.currentIndex = index;
}
}
}
}
ListView
{
id: beerList
anchors.fill: parent
model: myModel2
delegate: beerDelegate
highlightFollowsCurrentItem: true
highlight: Rectangle
{
width: parent.width
color: "red"
}
focus: true
}
}
How can I accomplish what I'm looking for given this QML? Or is using QML in a QWidget desktop app just a bad idea all around?
For the first question (highlight):
Your list actually draws the highlight, however, your item delegate overpaints this with a white rectangle! Just replace the rectangle with an item and it works:
Component
{
id: beerDelegate
Item
{
...
}
}
For the second question (scroll bars):
As far as I know, QML doesn't provide scroll bars out of the box. There is however the Qt Desktop Components project (git repository) which gives you access to most of the widgets in the QML world. Among them, there is a ScrollArea.
It is no longer necessary to implement the Scrollbars yourself. There is the ScrollView-Item since Qt 5.1. Simply surround a Flickable-Item (e.g. the ListView-Item you use, is also "Flickable") with the ScrollView-Item and you'll be fine:
ScrollView {
ListView {
id: beerList
anchors.fill: parent
model: myModel2
delegate: beerDelegate
highlightFollowsCurrentItem: true
highlight: Rectangle
{
width: parent.width
color: "red"
}
focus: true
}
}
For the second question. i.e Scroll-bar on ListView:
I have created code for scroll bar on ListView. It also can work on the GridView
ScrollBar.qml
import Qt 4.7
Item {
property variant target
width: 8
anchors.top: target.top
anchors.bottom: target.bottom
anchors.margins: 1
anchors.rightMargin: 2
anchors.bottomMargin: 2
anchors.right: target.right
visible: (track.height == slider.height) ? false : true
Image {
id: scrollPath
width: 2
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
source: "qrc:/resources/buttons/slider2.png"
}
Item {
anchors.fill: parent
Timer {
property int scrollAmount
id: timer
repeat: true
interval: 20
onTriggered: {
target.contentY = Math.max(0, Math.min(target.contentY + scrollAmount,
target.contentHeight - target.height));
}
}
Item {
id: track
anchors.top: parent.top
anchors.topMargin: 1
anchors.bottom: parent.bottom
width: parent.width
MouseArea {
anchors.fill: parent
onPressed: {
timer.scrollAmount = target.height * (mouseY < slider.y ? -1 : 1)
timer.running = true;
}
onReleased: {
timer.running = false;
}
}
Image {
id:slider
anchors.horizontalCenter: parent.horizontalCenter
source: "qrc:/resources/buttons/slider.png"
width: parent.width
height: Math.min(target.height / target.contentHeight * track.height, track.height) < 20 ? 20 : Math.min(target.height / target.contentHeight * track.height, track.height)
y: target.visibleArea.yPosition * track.height
MouseArea {
anchors.fill: parent
drag.target: parent
drag.axis: Drag.YAxis
drag.minimumY: 0
drag.maximumY: track.height - height
onPositionChanged: {
if (pressedButtons == Qt.LeftButton) {
target.contentY = slider.y * target.contentHeight / track.height;
}
}
}
}
}
}
}
And I used scroll bar item with ListView in MyListView.qml as:
MyListView.qml
ListView {
id: list
clip: true
anchors.margins: 5
anchors.fill: parent
model: 10
delegate: trackRowDelegate
interactive: contentHeight > height
}
ScrollBar {
id: verticalScrollBar
target: list
clip: true
}
This ScrollBar item can be used with GridView as
GridView {
id: grid
clip: true
anchors.margins: 5
anchors.fill: parent
cellWidth:100
cellHeight: 100
model: items
interactive: contentHeight > height
snapMode: GridView.SnapToRow
delegate: myDelegate
}
ScrollBar {
id: verticalScrollBar
target: grid
clip: true
visible: grid.interactive
}

Resources