QML Tableview display value from selected row - qt

I have a tableview and I want to open a dialog box on onPressAndHold on a row and display the value of the cell of the row "orderNumber".
But i get the Error message: ReferenceError: row is not defined
TableView {
id: tableviewopenorders
height: 180
clip: false
visible: true
onPressAndHold: oocanceldialog.open()
TableViewColumn {
id: orderNumberColumn
role: "orderNumber"
title: "Order Number"
}
model: openordersModel
}
ListModel {
id: openordersModel
ListElement {
orderNumber: "1223455"
}
ListElement {
orderNumber: "111111"
}
}
Dialog {
id: oocanceldialog
title: "Cancel confirmation"
standardButtons: Dialog.Ok | Dialog.Cancel
x: (parent.width - width) / 2
y: (parent.height - height) / 2
Label {
text: openordersModel.get(row).orderNumber
}
onAccepted: console.log("Ok clicked")
onRejected: oocanceldialog.close()
}

row exists in the context of onPressAndHold, so it does not exist outside of it, to get the row we must use the currentRow attribute of the TableView:
currentRow : int
The current row index of the view. The default value is -1 to indicate that no row is selected.
In your case:
Label {
text: openordersModel.get(tableviewopenorders.currentRow).orderNumber
}

Related

How to create different component base on ListModel in QML

There are 9 parameters that I need to use TextField1 to input value.
So I use
ListModel lstPara {
ListElement{
text:"A";value:"123"
}...(9 Elements)
}
Grid{
id: grid
anchors.fill: parent
columns: 3
spacing: 5
Repeater {
id: rpPara
model: lstPara
delegate: TextField1 {
}
}
}
But now there is a parameter that i need to use in another QML type to set the value, all others are used in TextField1.
I tried to define ListModel like this
ListModel lstPara{
ListElement {
text: "A";
type: 1";
value: "123"
}
ListElement {
text: "B";
type: 2";
value: "321"
}
...(9 Elements)
}
Grid{
id: grid
anchors.fill: parent
columns: 3
spacing: 5
Repeater {
id: rpPara
model: lstPara
(some code : like this)
delegate: {
return type === 1 ?
TextField1 {
}
:
another QML type {
}
}
}
}
The code above can not run.
And I don`t want to write 8 TextField1 and 1 another QML type.
So, is there a way to use ListModel?
You can't directly use an Item declaration in a conditional expression like that, but you can do it with a Component. Here's an example of how to do it using a Loader as your delegate, and choosing which Component to load based on the model:
ListModel {
id: lstPara
ListElement {
text: "A"
type: 1
value: "123"
}
ListElement {
text: "B"
type: 2
value: "321"
}
}
Grid {
id:grid
anchors.fill: parent
columns: 3
spacing: 5
Repeater {
id: rpPara
model: lstPara
delegate: Loader {
sourceComponent: type === 1 ? someText : otherText
onLoaded: {
item.text = text
item.value = value
}
}
}
Component {
id: someText
Text {
property string value
color: "blue"
}
}
Component {
id: otherText
Text {
property string value
color: "red"
}
}
}

QML property binding for repeater item

How do property bindings work for repeater items? I am trying to use a property binding on an element generated by a repeater, but the binding doesn't work:
Rectangle
{
id: root
property int currentLocation: 0
Button {
text: "Change location"
onClicked: root.currentLocation = (root.currentLocation + 1) % 2
}
Repeater {
id: locationlRepeater
model: locationModel
Item {
visible: root.currentLocation == model.location // Why doesn't this update on button click?
Text {
id: locationText
text: "Location: " + model.location
}
}
}
ListModel {
id: locationModel
ListElement {
location: 0
// etc.
}
ListElement {
location: 1
// etc.
}
}
Updated: using Qt 5.6.2

How to set value to second page textfield from Main page textfield

I have textfields on main.qml and second.qml page. When I'm setting value to main.page textfield I want to set same value to second.page textfield.I use alias propery but not get expected output.
ApplicationWindow {
id: windowObject
visible: true
width: 640
height: 480
StackView {
id: stack
initialItem: view
Component {
id: view
MouseArea {
Text {
text: stack.depth
anchors.centerIn: parent
}
onClicked: stack.push(view)
}
}
}
TextField{
id: setvalue
text:"50" // set value from main page
}
Button{
id: clickme
text : "ClickMe"
x: 100
y:200
onClicked: {
console.debug("New Page")
stack.pop(StackView.Immediate)
stack.push (Qt.resolvedUrl("Secondpage.qml"))
} }}
Secondpage.qml:
Item {
id: name
property alias value : getvalue.text
TextField{
id: getvalue
text : "" // value from main page TextField
}
}
Actually I didn't find where you exactly using your second page.
Let's suppose it should be on the main screen as well. Then your code should looks like some thing like that:
Window {
id: windowObject
visible: true
width: 640
height: 480
TextField{
id: setvalue
text:"50" // set value from main page
}
Secondpage {
id: _secondPageItem
anchors.top: setvalue.bottom
value: setvalue.text
}
}
Secondpage.qml
Item { // Secondpage.qml
id: name
property alias value : getvalue.text
width: getvalue.width // by default item has geometry (0, 0)
height: getvalue.heigh
TextField{
id: getvalue
text : "" // value from main page TextField
}
}

How to get currentIndex of an delegateItem in a TreeView

Hellow,
When using delegate on a Column, how could I know the index of the Cell or Row selected if the user clicks on the delegateItem ?
Here is an example. The second Column is a MouseArea and I want to expand the currentIndex when the user clicks on the MouseArea:
TreeView {
id: view
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
delegate : MouseArea {
id:mous
onClicked {
//get indexMouseArea
view.expand(indexMouseArea)
}
}
}
model: fileSystemModel
onExpanded {
console.log("expanded :" + index)
}
}
Solution thanks to #mcchu:
Hellow,
When using delegate on a Column, how could I know the index of the Cell or Row selected if the user clicks on the delegateItem ?
Here is an example. The second Column is a MouseArea and I want to expand the currentIndex when the user clicks on the MouseArea:
TreeView {
id: view
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
delegate : MouseArea {
id:mous
onClicked {
view.expand(styleData.index);
}
}
}
model: fileSystemModel
onExpanded {
console.log("expanded :" + index)
}
}

TableView not the correct size with QML

I am trying to create a TableView with QML where I have a checkbox, an image and a text field. The table column definitions are as follows:
// TableViewCheckBoxColumn.qml
TableViewColumn {
title: ""
role: "check"
delegate: CheckBox {
anchors.fill: parent
checked: styleData.value
}
}
//TableViewImageColumn.qml
TableViewColumn {
title: ""
role: "thumbnail"
delegate: Image {
anchors.fill: parent
source: styleData.value
width: 30
height: 30
}
}
Now the data model and the table itself is defined as a QML component as follows:
Item {
ListModel {
id: sourceModel
ListElement {
check: false
thumbnail: "file:///Users/xargon/alignment.png"
length: "10:22"
}
}
// Table view
TableView {
anchors.centerIn: parent
alternatingRowColors: false
TableViewCheckBoxColumn {
id: checkedColumn
}
TableViewImageColumn {
id: thumbColumn
}
TableViewColumn {
id: lengthColumn
role: "length"
title: "Length"
}
model: sourceModel
}
}
Now, this is embedded in a ColumnLayout and a StackView as:
ColumnLayout {
anchors.fill: parent
MyTable {
id: reviewScreen
Layout.fillWidth: true
}
StackView {
id: options
Layout.fillHeight: true
Layout.fillWidth: true
initialItem: reviewScreen
}
}
Now I was expecting the table to fill the entire width of the parent control and I also was expecting the image to be drawn as a 30 x 30 image but what I see is the attached screenshot where the horizontal scrollbar is there to move between controls and the table is small and the image is very distorted as well.
yes, only you need declare heigh and weight of every TableViewColumn, for example:
TableViewColumn { id: lengthColumn; role: "length"; title: "Length"; height: parent.height/8; width:parent.width*0.25}

Resources