add data to a qml XmlDataModel - qt

gi, i have a question about qml and the XmlDataModel.
i have a xml file where i have something like this:
<model>
<item title="one" />
<item title="two" />
</model>
and i have a listView which has this data:
ListView {
id: listView
dataModel: XmlDataModel {
source: "data/model1.xml"
}
}
now i dant to add some data to this model when i e.g. press a button. how to make this?

XmlListModel is a read-only model by design. However, you can use it to conveniently fetch data and fill with that data a ListModel which is mutable model. You can find an example here.

Related

WPF Datagrid binding and column display, not able to display the Data in the DataGrid after binding the DataGridTextColumn Header

Initially I was generating a dataGrid with AutoGeneratedColumns = True getting values from a dataTable. However the number of columns for each dataset is fixed and I would like to name them accordingly and therefore named them in the code but if the Tab was not selscted when I runed the code then I was getting an error that the Index is out of range.
I decided to do AutoGeneratedColumns = False and manually define my columns. However in the sequel my data dataGrid was empty and my data not displayed. The Data Table name is Underlyings and it is generated at runtime, before that its not even there.
Below my XAML code:
<DataGrid ItemsSource ="{Binding Path=Underlying}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="462,145,0,203" Name="dataGrid3" Width="872" >
<DataGrid.Columns>
<DataGridTextColumn Header="In" Binding="{Binding FirstColumnName}" />
<DataGridTextColumn Header="InL" Binding="{Binding SecondColumnName}" />
<DataGridTextColumn Header="Total " Binding="{Binding ThirdColumnName}" />
</DataGrid.Columns>
</DataGrid>
And in the code thwe following
dataGrid3.ItemsSource = Underlying.DefaultView
However I think that XAML binding would not work for me because my datatable is generated at runtime so there is nothing to bind to at design time.
Any Ideas how to do so?
I think you should not set the ItemsSource property but rather use the DataContext property of the DataGrid and use templating in the XAML as in the question here: WPF: Binding to DataGrid

Flex - Passing data into tabbed views

I have a project that has 4 views where I'm using the tabBar with viewStack/NavigatorContent. I have a couple HTTPServices set up gathering the XML and converting to Bindable ArrayCollections. When and how is the best way to pass that data to such things as charts, dataGrids, etc. that aren't seen until the user clicks a tab? Right now I have each item set up with creationComplete functions that pass it then. Although it seems to be working, is this the best way, or is there something better and maybe even quicker?
Thanks,
Mark
The best way is using data binding. Say you have a main container which contains ViewStack witch components representing tabs content. So you should have [Bindable] properties for data in a main container like the following:
[Bindable]
private var chartData:ArrayCollection;
[Bindable]
private var dataGridData:ArrayCollection;
etc.
So for the component, containing chart, you should populate chart data with data binding:
<ViewStack>
…
<MyChartsTab chartData="{chartData}" />
…
</ViewStack>
And of course you should introduce the same chartData field (make sure it is public) in your MyChartsTab component. Your charts there can be populated with data binding too.
So after getting data you just fill your fields in main component and data binding performs the rest job without any care of initialization from your side.
When creating your views, make sure you allow a public variable (like 'dataProvider') where you can bind the data it needs. Like this:
<mx:ViewStack>
<s:NavigatorContent>
<SomeComponent dataProvider="{someData}" />
</s:NavigatorContent>
<s:NavigatorContent>
<SomeComponent2 dataProvider="{someData}" />
</s:NavigatorContent>
</mx:ViewStack>
And within the custom component you'd have:
private var _data:Object; // use strong typing if possible
public function set dataProvider(value:Object):void // use strong typing if possible
{
this._data = value;
}
public function get dataProvider():Object
{
return this._data;
}

How to create customized component?

I want to create a data grid that has 4 columns: description, quantity, price, and line total. The line total will simply be the product of quantity & price. I want the last line of the grid to be the total of all line totals.
How can I create this type of grid? I thought of using item renderers, but I can't figure out how to have the last row be for the line items total.
If I must create a custom component, I'd appreciate book recommendations on custom component creation. While I have a general understanding of how to create custom components, I don't have a firm a grip on it as I'd like. Thanks.
While you should definitely check out the links provided by www.Flextras.com, there are a couple of ways to provide the functionliaty you're after:
The simplest is a labelFunction:
<DataGrid dataProvider="{dp}">
<columns>
<DataGridColumn labelFunction="sumTotals" />
</columns>
</DataGrid>
private function sumTotals(item:Object,column:DataGridColumn):String {
return Number(item.quantity * item.price).toString();
}
Alternatively, you could create your own itemRenderer, as follows:
<!-- MyItemRenderer.mxml -->
<mx:Label>
<mx:Script>
override public function set data(value:Object):void {
super.data = value;
this.label = Number(item.quantity * item.price).toString();
}
</mx:Script>
</mx:Label>
<!-- Your component -->
<DataGrid dataProvider="{dp}">
<columns>
<DataGridColumn itemRenderer="MyItemRenderer"/>
</columns>
</DataGrid>
A Flex DataGrid cannot include items that are not in the dataProvider, so adding a row that includes the total of all line items in the DataGrid is not practical, without extending the DataGrid.
Take a look at this documentation for info about creating custom components. Also take a look at this screencast series. A direct link to episode 1.

Flex: referencing all data in TileList

I have 2 TileList component in my Flex application.
1 tilelist is filled with data much like following xml sample:
<person name="Test">
<likes>Flex</likes>
<likes>PHP</likes>
</person>
<person name="test2">
<likes>HTML</likes>
<likes>CSS</likes>
</person>
the data shown in that tilelist is the name.
my second tilelist:
<items>
<preference>Flex</preference>
<preference>Flash</preference>
<preference>HTML</preference>
<preference>CSS</preference>
<preference>PHP</preference>
<preference>CMS</preference>
<preference>ASP</preference>
<preference>C</preference>
</items>
data shown is the preference.
The user can click the first tilelist and then the items that person "likes" should be selected in the second tilelist (in other words they lit up).
click event on my first tilelist
private function highlightPreferences(e:ListEvent):void{
trace(e.currentTarget);
//and now I'm stuck
}
Is there any way to achieve this?
Just write a function that returns the selectedIndices for a particular person. Then, bind the second TileList's selectedIndices like this: selectedIndices="{findLikes(firstList.selectedItem)}" The binding will fire if firstList.selectedItem changes.
Oh, and please don't use a repeater. Lists can do everything repeater can better.

In Flex, is there something like a 'this' reference for an MXML component?

I could code what I want to achieve like this:
<mx:Button id="someButton" click="doRememberButton(someButton)" ... />
but would find it very helpful (I am putting together a rather large UI) if I could write:
<mx:Button click="doRememberButton(this)" ... />
Now, the obvious problem is that 'this' does not point to the Button, but to the main component defined by the file the code is in (e.g. VBox), yet it would be a great help if I had some reference to the 'current' MXML component..
Would anybody have a solution for this? Thanks!
Tom
Inline event handlers is really just wrapped code, so you may use the event object to get details of the dispatcher and other event information. Like so:
<mx:Button click="trace(event.target)" />
In your case, you'd have to change the signature of your event handler, e.g.:
private function doRememberButton(event:Event):void
{
...
}
And in the MXML code:
<mx:Button click="doRememberButton(event)" />
The target property of the event class is the original dispatcher of the event. There is also a currentTarget property which is the current target in the event chain. This relates to event bubbling. There is more information on this in Adobe LiveDocs
private function doRememberButton(ev: Event) {
//this gives your button
ev.currentTarget;
}
here is a solution more precisely the way u needed
<mx:Button id="someButton" click="doRememberButton(event.currentTarget as Button)" />
at the function:
private function doRememberButton(thisBtn:Button):void
{
...
}
that's it! :)

Resources