Does anybody knows, how to prevent scrolling down QML ListView when I prepend some elements to its head? I want to update ListView in a Twitter-like manner, when it always keeps its position and only explicit flick is possible.
Actually the insert function did the job. You can insert it at the top or any desired position like this modelName.insert(0,{object});. A working example is here.
import QtQuick 1.0
Rectangle {
id: rectangle1
width: 320
height: 480
ListModel {
id: cModel
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
ListView {
id: list_view1
width: rectangle1.width
height: rectangle1.height - 40
anchors.horizontalCenter: parent.horizontalCenter
delegate: Text {
text: name + ": " + number
}
model: cModel
}
Rectangle {
id: rectangle2
width: 320
height: 40
color: "#ffffff"
anchors.top: list_view1.bottom
Text {
id: text1
text: qsTr("Click to add!")
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 16
MouseArea {
id: mouse_area1
anchors.fill: parent
onClicked: addNewItemTop();
}
}
}
function addNewItemTop()
{ var i = Math.random();
cModel.insert(0,{"name" : "New Number", "number":i.toString()});
}
}
Related
I'm trying to make a list of buttons, where the user can select one of the options. I can't seem to display the text from the listmodel, but I can't figure out why. It seems like even the button is not correctly placed. Does anyone have ideas as to how to fix this? Thanks!
import QtQuick 2.15
import QtQuick.Window 2.2
import QtQuick.Controls 2.15
Window {
id: window
visible: true
height: 400
width: 400
Rectangle {
id: rect
width: 400; height: 400
ListView {
id: listview
anchors.fill: parent
model: timeList
delegate: Item {
anchors.top: parent.top
height: 30
Button {
anchors.top: parent.top
anchors.left: parent.left
id: textButton
text: text
width: parent.width
height: 30
flat: true
onClicked: {
console.log(value)
}
}
Rectangle {
height: (index === timeList.count - 1 ? 0 : 1)
color: '#E1E6E9'
width: 400
anchors {
left: textButton.left
top: textButton.bottom
}
}
}
ListModel {
id: timeList
ListElement { text: "15 minutes"; value: 15 }
ListElement { text: "1 hour"; value: 60 }
ListElement { text: "3 hours"; value: 180 }
ListElement { text: "6 hours"; value: 360 }
ListElement { text: "12 hours"; value: 720 }
}
}
}
}
There are several issues here:
don't name ListElement properties using keywords, text: text looks confusing.
don't wrap everything with Rectangle/Item with no need, use Layout instead if needed
never use anchors for delegates
pay attention that all containers have a size, the delegate's Item has no with for example (or probably 0)
actually, that should work:
import QtQuick
import QtQuick.Window
import QtQuick.Controls
Window {
id: window
visible: true
height: 400
width: 400
ListView {
anchors.fill: parent
model: timeList
delegate: Button {
id: textButton
text: name
width: parent.width
height: 30
flat: true
onClicked: {
console.log(value)
}
}
ListModel {
id: timeList
ListElement { name: "15 minutes"; value: 15 }
ListElement { name: "1 hour"; value: 60 }
ListElement { name: "3 hours"; value: 180 }
ListElement { name: "6 hours"; value: 360 }
ListElement { name: "12 hours"; value: 720 }
}
}
}
In my screen, I can see around 5 list element out of 11.I'm Using Up/Down Arrow Keys to move the Highlight Rectangle(Border Rectangle).
Highlight rectangle will be displayed above list element only for first 5 list elements.
Once it reaches the 6th element the rectangle will be displayed behind the list element even if the Z index is 1 and it never comes above list element again.
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.0
Window {
visible: true
width: Screen.width
height: Screen.height
id: rootWindow
ListModel {
id: contact
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Bill Smith"
number: "555 3264"
}
}
ListView{
clip: true
z: -1
id: lst
width: Screen.width
height: Screen.height
anchors.centerIn: rootWindow
x: rootWindow.width *.05
spacing: 20
y: subrect.height + subrect.y+ 100
model: contact
delegate:
RowLayout{
id: idx
anchors.topMargin: 5
spacing: -5
Rectangle{
width: 30
height:100
color: "#3399ff"
radius: 5
}
Rectangle{
id: segment
width: lst.width
height: 100
color: "white";
layer.enabled: true
layer.effect: DropShadow{
transparentBorder: true
horizontalOffset: 3
verticalOffset: 3
color: "#e1dede"
}
RowLayout {
Rectangle{
id: inner
width: 300
height: 50
color: "transparent"
Text {
x: 50
y: inner.height /3
text: "Name : " + name
font.pixelSize: 30
anchors.verticalCenter: inner
}
Column{
y: inner.height /3
Text {
color: "grey"
x: lst.width * .8
text: "Contact "
font.pixelSize: 20
}
Text {
color: "grey"
font.pixelSize: 20
x: lst.width * .8
text: number
}
}
}
}
}
}
focus: true
highlight: Rectangle { color: "transparent"; z:1;radius: 8; border.width: 3; border.color: "black" }
}
}
Thank You
The stacking order of the Listview happens as below.
So to get your highlighter on the top change z: 2 that should work. Here is the complete description of the Listview stacking order doc.
And remove the z-order of your listview's instance.
I have ListView with some text input fields inside it:
Window {
visible: true
ListModel {
id: textModel
ListElement {
text: "Bill Smith"
}
ListElement {
text: "John Brown"
}
ListElement {
text: "Sam Wise"
}
}
ListView {
width: 180; height: 200
focus: true
model: textModel
delegate: RowLayout{
id: layout
Label {
text: model.text
}
TextField {
text: model.text
}
}
}
}
And I want to set input focus to the first TextField in the list. How can I do this? If I add focus: true in ListView it does not help.
You have to activate the focus of the TextField using the ListView.isCurrentItem property:
ListView {
id: view
width: 180; height: 200
focus: true
model: textModel
delegate: RowLayout{
id: layout
Label {
text: model.text
}
TextField {
focus: layout.ListView.isCurrentItem
text: model.text
}
}
// Component.onCompleted: view.currentIndex = 0
}
I have a vertical Listview and when i click an item i will display horizontal Listview. The question is that when i click 2 nd item i should display 2nd item on horizontal Listview. Here it is my implementation:
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
StackView {
id: rootStackView
anchors.fill: parent
initialItem: verListView
}
VListview {
id: verListView
onListItemClicked: {
rootStackView.push(horListView)
horListView.init(index)
}
}
HListView {
id: horListView
visible: false
}
}
VListview.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
Page {
id: root
signal listItemClicked(var index)
ListView {
id: listView
anchors.fill: parent
Layout.fillWidth: true
model: ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
delegate: Component {
id: contactDelegate
Item {
MouseArea {
anchors.fill: parent
onClicked: {
console.log("aaa")
root.listItemClicked(index)
}
}
width: listView.width; height: 40
Column {
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
}
}
}
}
HListview.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
Page {
function init(index) {
console.log(index)
horizontalListView.currentIndex = index;
horizontalListView.positionViewAtIndex(index, ListView.Beginning)
}
ListView {
id: horizontalListView
anchors.fill: parent
orientation: ListView.Horizontal
model: ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
snapMode: ListView.SnapToItem
delegate: Component {
id: contactDelegate
Item {
width: horizontalListView.width; height: 40
Column {
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
}
}
}
}
HorizontalListView
I am clicking 2nd and 3 rd items but it display always first item. Anybody faced the issue? Thanks in advance
The width of horizontalListView is wrongly set.
HListview.qml
ListView {
id: horizontalListView
// anchors.fill: parent
height: 40 <== fix the height
width: root.width <== width of the hview must be same size as that of root window.
orientation: ListView.Horizontal
...
}
EDIT
Explanation: If positioning the view at index would cause empty space to be displayed at the beginning or end of the view, the view will be positioned at the boundary.
I am working on qml, found found an error during moving mouse wheel scrolling i.e.
"TypeError: Cannot read property 'name' of undefined".
It only appear when either mouse wheel scrolling or close the application while during execution and beginning of the application didn't show this error.
One more thing, I am getting accurate data/result whatever I want but due to this error the performance of application get affected.
Please let me know how can I get rid of this issue?
also find below example to see error.
My code is..
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
TableView {
anchors.fill: parent
visible: true
backgroundVisible: false
alternatingRowColors: false
sortIndicatorVisible: true
clip: true
highlightOnFocus: false
width: parent.width
height: parent.height
TableViewColumn{ role: "code" ; title: "cde" ; width: 200;
delegate: Component {
id: codeDelegate
Item{
Text {
color: "lightgray"
elide: styleData.elideMode
text: styleData.value //modelSettingData.get(styleData.row).name
font.family: "Arial"
font.pixelSize: 18
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
Text {
id: metaData
color: "red"
width: parent.width
// height: 20
anchors.bottom: parent.bottom
anchors.bottomMargin: -parent.height/1.5
font.weight: Font.ExtraLight
//elide: Text.ElideMiddle
text: "Time: " + modelSettingData.get(styleData.row).name //Error comes when this part added but I need to use it with the same form
}
}
}
}
}
TableViewColumn{ role: "name" ; title: "nme" ; width: 200
delegate: Component {
id: nameDelegate
Text {
color: "yellow"
elide: styleData.elideMode
text: styleData.value
font.family: "Arial"
font.pixelSize: 18
}
}
}
model: ListModel {
id: modelSettingData
ListElement {
name: "aaaaaaaaaa"
code: "3042666666666"
}
ListElement {
name: "bbbbbb"
code: "32235"
}
ListElement {
name: "ccccccc"
code: "32638"
}
ListElement {
name: "ddddddddddd"
code: "00000000000"
}
ListElement {
name: "eeeeeeeeeeee"
code: "111111111111"
}
ListElement {
name: "ffffffffffff"
code: "222222222222"
}
ListElement {
name: "ggggggggggggg"
code: "3333333333333"
}
ListElement {
name: "hhhhhhhhhhhhh"
code: "4444444444444"
}
ListElement {
name: "iiiiiiiiiiiii"
code: "5555555555555"
}
}
rowDelegate: Rectangle {
property bool selected : styleData.selected
width: parent.width-2
height: 100
color: styleData.selected? "black" : "black"
Rectangle {
width: parent.width
height: 1
anchors.bottom: parent.bottom
visible: parent.selected
color: "yellow"
}
}
}
}