How change field of reused component in Qml? - qt

I have the following code. I need to set different "model" field for different TableViews. How can i do this?
I need override "model" field for every TableView. It is possible?
// main.qml
ApplicationWindow {
id: window
TabView {
Tab {
title: "Tab 1"
MyTable {}
}
Tab {
title: "Tab 2"
MyTable {}
}
}
}
.
// MyTable.qml
MyTableView {
TableView {
TableViewColumn {
role: "number"
title: "Number"
}
model: MyModel
}
}
The next code is correct but too long.
// main.qml
ApplicationWindow {
id: window
TabView {
Tab {
title: "Tab 1"
TableView {
TableViewColumn {
role: "number"
title: "Number"
}
model: MyModel_1
}
}
Tab {
title: "Tab 2"
TableView {
TableViewColumn {
role: "number"
title: "Number"
}
model: MyModel_2
}
}
}
}

You can set a property alias for the model. First set an id for the TableView then hook up its model.
// MyTable.qml
MyTableView {
property alias model: tableView.model
TableView {
id: tableView
TableViewColumn {
role: "number"
title: "Number"
}
// model: MyModel // redundant in most cases
// unless you want to have a DEFAULT model
}
}
And use model in each MyTable item like so:
// main.qml
ApplicationWindow {
id: window
TabView {
Tab {
title: "Tab 1"
MyTable { model: MyFirstModel }
}
Tab {
title: "Tab 2"
MyTable { model: MySecondModel }
}
}
}

Related

Get value or property from Stackview item when it popped in Qml

Is there any way to get value or property from Stackview item when it popped in Qml?
I want to get the edited name in 'EditProfile.qml' when it popped to 'Profile.qml' in the below project.
main.qml
StackView {
id: stackView
Component.onCompleted: push('Profile.qml', {name : 'David'})
}
Profile.qml
Page {
property string name: ''
Column {
Text {
text: 'name' + name
}
Button {
text: 'Edit'
onClicked: stackView.push('EditProfile.qml', {name : name})
}
}
}
EditProfile.qml
Page {
property alias name: txtName.text
Column {
TextEdit {
id: txtName
text: name
}
Button {
text: 'Back'
onClicked: stackView.pop()
}
}
}
After reading QT manual carefully, I found the answer. The push function returns the item which is pushed. so:
Profile.qml
Page {
id: root
property string name: ''
Column {
Text {
text: 'name' + name
}
Button {
text: 'Edit'
onClicked: {
var item = stackView.push('EditProfile.qml', {name : name})
item.exit.connect(change);
function change(text) {
item.exit.disconnect(change);
root.name = text;
}
}
}
}
}
EditProfile.qml
Page {
signal exit(var text)
property alias name: txtName.text
Column {
TextEdit {
id: txtName
text: name
}
Button {
text: 'Back'
onClicked: {
exit(txtName.text)
stackView.pop()
}
}
}
}

Get specific item in model QML

I can recover an element in my model:
property var model: [
{ title: i18n.tr("Standard"), descr: i18n.tr("Style by default"), style:"default" },
{ title: i18n.tr("None"), descr: i18n.tr("Icons without style", style:"none" }
]
ListItem.ItemSelector {
id: styleIconList
model: settingsColumn.model
delegate: OptionSelectorDelegate {
property var item: model.modelData ? model.modelData : model
text: item.title
subText: item.descr
}
onSelectedIndexChanged: {
launchermodular.settings.iconStyle = model.get(selectedIndex).style //here is problem
}
}
Can you tell me how?
Thank you

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)
}
}

How to access Data of a Column in a TreeView with Index

I want to access Data of a Column in a TreeView while knowing Index but I have trouble to do it.
Here is an example. I want to change the value of isCollapsed when a row is expanded:
TreeView {
id: view
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
delegate : MouseArea {
id:mous
property bool isCollapsed : false
}
}
model: fileSystemModel
onExpanded {
index.data("filePermissions").isCollapsed = true //This is not working, I don't find the right syntaxe
}
}

Access properties from different qml tabs within same tabbedpane

I have my main.qml that contains a tabbedpane, in which, it has two tabs: tab1 and tab2.
I would like to be able to change the text from one tab to another.
If I do the same with navigationpane it works but not with tabs apparently. Is there any way I could share information between them? I've tried with signals in c++ but it doesn't work either(I guess it doesnt know the instance ?).
Any suggestion is appreciated.
main.qml:
TabbedPane {
Tab {
Tab1 {
}
}
Tab {
Tab2 {
}
}
attachedObjects: [
Tab1 {
id: tab1
},
Tab2 {
id: tab2
}
]
}
Tab1.qml:
Page {
property alias labeltab1: labeltab1
Container {
Label {
id: labeltab1
text: "label tab1"
}
Button {
id: buttontab1
text: "tab1"
onClicked: {
tab2.labeltab2.text = "This is coming from tab1"
}
}
}
}
Tab2.qml:
Page {
property alias labeltab2: labeltab2
Container {
Label {
id: labeltab2
text: "Label tab2"
}
Button {
id: buttontab2
text: "tab2"
onClicked: {
tab1.labeltab1.text = "This is coming from tab2"
}
}
}
}
I guess it's actually simpler with tabs and I found my own solution.
I noticed that momentics cannot detect that "thepane" is linkable and will not suggest its name when typing it from one of the tab. Also, a property with colon will automatically bind the value afterit, as: text: thepane.mystring
When clicking on buttons, it changes the value of mystring thus changing both labels texts.
main.qml
TabbedPane {
id: thepane
property string mystring
Tab {
Tab1 {
}
}
Tab {
Tab2 {
}
}
}
Tab1.qml
Page {
Container {
Label {
id: labeltab1
text: thepane.mystring
}
Button {
id: buttontab1
text: "tab1"
onClicked: {
thepane.mystring = "This is coming form tab1"
}
}
}
}
Tab2.qml
Page {
Container {
Label {
id: labeltab2
text: thepane.mystring
}
Button {
id: buttontab2
text: "tab2"
onClicked: {
thepane.mystring = "This is coming from tab2"
}
}
}
}
Thank you for your idea.
I changed the code. Now it is better for me, maybe for other one people :)
main.qml
TabbedPane {
id: main_Pane
property string action_1
property string action_2
Tab {
Tab1 {}
}
Tab {
Tab2 {}
}
}
Tab1.qml
Page {
Container {
Label {
text: main_Pane.action_1
}
Button {
text: "Button 1"
onClicked: {
main_Pane.action_2 = "This is action form Tab1"
}
}
}
}
Tab2.qml
Page {
Container {
Label {
text: main_Pane.action_2
}
Button {
text: "Button 2"
onClicked: {
main_Pane.action_1 = "This is action from Tab2"
}
}
}
}

Resources