Flex: How to access properties of component in dynamic creation? - apache-flex

I have a component which is created dynamically. I want to access the properties on it.
for example i create a vbox and i want to access the text font or gap of the component
var MyVBox: VBox = new VBox;
MyPanel.addChild(MyVBox);
How should it be done?

All properties and methods are accessed with "." (dot) notation.
Example:
myVBox.width = 400;
Styles are set using the setStyle() method. In your case that would be
myVBox.setStyle("fontFamily", "arial");
myVBox.setStyle("verticalGap", 20);
Check the docs at http://livedocs.adobe.com/flex/3/langref/ for the available properties and styles of each component.

The thing to remember when using ActionScript instead of MXML is that the style properties are not accessed as properties on the object but through the getStyle("propertyName") method. Font is a style for example.

Related

How to create a custom grouping of properties and apply them to different controls?

I have a window with Buttons, TextFields, Labels etc. These all share common properties like font family, color, size etc. What I would like to be able to do is to define a grouping of these properties (called, say, textStyles.MainTitle or textStyles.DescriptiveText etc.) that would include a font family, size and weight, height and color. And then in the QML file I would write something like:
myCustomProperty: textStyles.MainTitle
and this would apply those values to the control. How can I do this?
QML controls are styled by implementing their respective styles, for example for Button you have to implement a ButtonStyle.
As for the actual "grouping" you can just use a QtObject
property QtObject textStyles : QtObject {
property FontLoader mainTitle : FontLoader {...}
....
}
You can also extend styled components as dedicated types:
// StyledText.qml
Text {
font.family: someFont
font.pixelSize: fontSize
color: someColor
font.italic: true
font.letterSpacing: -1
...
}
And then use that by just StyledText {} instead of repeatedly styling regular Text elements.
Where / in what file do I place the QtObject snippet? I don't understand what // StyledText.qml is, or what a FontLoader is.
If you want it to be available in your entire application, you can put it as a property of your root object in main.qml, thanks to dynamic scoping textStyles will resolve from every other file in your project. You can put entire component styles in it as well and share in the project.
StyledText.qml is just an extra qml file you add to your project, you can being with just implementing its body in an existing qml file, then rightclick on Text and select refactoring -> move component into separate file.
A FontLoader is just a regular QML component, you can use it to load specific fonts and use as a font source for text. You don't have to use that, you can use font families from your system fonts as well. The font loader is useful for fonts you bundle with your application.

Adding new stylesheet parameters for custom Qt widgets

I would like to add stylesheet options for a custom widget I have developed. We have extended the QPushButton to be a different colour and to flash when it is depressed. This has been done by adding a new property, background color down. And we set this in code. But I would like to set this instead using a Qt stylesheet entry, something like
QFlashingButton
{
background-color-down: yellow;
flashing-interval: 5;
}
I can see one way to do this, read out the stylesheet info using the stylesheet() method, then parse it for parameters relevant to my widget and set them. But I am wondering if there is some way to access the code Qt have themselves for processing stylesheets. At first sight of their code this seems perhaps not to be publically available.
As long as the parameter you want to control in the stylesheet is a QProperty, you can set it in the stylesheet using the syntax: qproperty-<PROPERTY_NAME>: <PROPERTY_VALUE>
I don't think property names can actually have dashes in them, so assuming your QProperties on your custom widget are actually backgroundColorDown and flashingInterval, then your stylesheet would look like:
QFlashingButton
{
qproperty-backgroundColorDown: yellow;
qproperty-flashingInterval: 5;
}

Styling QML without manually marking each property to be styled

I know that QML does not support CSS styling like widgets do, and I have read up on alternative approaches to styling/theming:
https://qt-project.org/wiki/QmlStyling
http://www.slideshare.net/BurkhardStubert/practical-qml-key-navigation/34
Common for these approaches is that they require the developer to specify the parts of the QML that can be styled, either by binding to a property in a “styling QML file/singleton”, or by using a Loader to load a different QML component based on style name. What I would like is something that works like the "id" selector in CSS instead of the "class" selector, so that the individual QML files do not have to know whether they will be styled later on or not.
My current approach make all the QML files look similar to this (using approach in link 2):
Main.qml
Rectangle {
Id: background
color: g_theme.background.color
//g_theme is defined in root context and loaded dynamically
}
What I would like to do is:
Main.qml
Rectangle {
Id: background
color: “green” // default color
}
And then have a styling file that defines (or similar)
Main.qml #background.color: red
Is this possible at the moment, or something that is in the pipeline for a future Qt version, or will the preferred way of styling continue to be something similar to the approach described in the links above?
The preferred way isn't applying a style on default components, but deriving from these components to create pre-styled custom components.
What I do for my projects :
First, I create one centralized 'theme' file, as a JavaScript shared module :
// MyTheme.js
.pragma library;
var bgColor = "steelblue";
var fgColor = "darkred";
var lineSize = 2;
var roundness = 6;
Next, I create custom components that rely on it :
// MyRoundedRect.qml
import QtQuick 2.0;
import "MyTheme.js" as Theme;
Rectangle {
color: Theme.bgColor;
border {
width: Theme.lineSize;
color: Theme.fgColor;
}
radius: Theme.roundness;
}
Then, I can use my pre-styled component everywhere with a single line of code :
MyRoundedRect { }
And this method has a huge advantage : it's really object-oriented, not simple skinning.
If you want you can even add nested objects in your custom component, like text, image, shadow, etc... or even some UI logic, like color-change on mouse hover.
PS : yeah one can use QML singleton instead of JS module, but it requires extra qmldir file and is supported only from Qt 5.2, which can be limiting. And obviously, a C++ QObject inside a context property would also work (e.g. if you want to load skin properties from a file on the disk...).
It could also be helpful to look at Qt Quick Controls Styles
When using Controls Styles it is not necessary to explicitly assign each property in the target control. All properties can be defined in a separate [ControlName]Style component (e.g. ButtonStyle).
Then in target component (e.g. Button) you can just reference to style component in one line of code.
The only one downside here is that Style components are available for Qt Quick Controls only. Not for any Qt Component.

Dojo - Giving multiple className in domConstruct.create()

I am new to DOJO toolkit and still learning. I have a situation in which i want to give multiple css classes to the element created by dojo's domConstruct.create() method.
I created table element and four columns - 'td' elements with domConstruct.create() method. each column is styled differently.
I created my class -'errors-alignment' and have existing class 'error'.
If i add inline styles for more styling, it works.
domConstruct.create('td' , { className:'error',style:{....}},...)
className:'error-alignment error' //two classnames do not work either
But i can not have inline styles and can not modify existing class.
Is there anything existing in dojo or css to help me providing multiple css to an element.
You can do:
var newNode = domConstruct.create('td' , { 'class':'error' }, parentNode);
You can also use dojo/dom-class and dojo/dom-style to modify the class and style of an already existing node.
http://dojotoolkit.org/reference-guide/1.9/dojo/dom-class.html
http://dojotoolkit.org/reference-guide/1.9/dojo/dom-style.html

importing flash library symbol into flex

I am embedding a flash file in my flex file and then trying to add it to the stage. IU try addChild to a canvas element and to a container element, but it keeps giving me the error, the symbol "myBtn" is cannot be converted to a IUIcomponent.
I understand that I need to place everything inside some sort of component, but what is the proper way to do this in flex?
Adding to rawChildren isn't the way to go here. Containers ignore rawChildren when doing things such as layout, measurement, etc.
Instead, simply wrap it in a UIComponent:
[Embed(source="...")]
public var someSwf:Class;
public function addSwf():void
{
var swf:Sprite = new someSwf();
var wrapper:UIComponent = new UIComponent();
wrapper.addChild(swf);
this.addChild(wrapper);
}
Look here for info on Embedding swf files and embedding swf symbols.
I figured it out thanks to this post: http://craiggrummitt.blogspot.com/2007/11/how-to-add-children-in-flex.html
you use something called rawChildren. so I would do myComponent.rawChildren.addChild('mymc');
Here you can downlod a complement to conver a symbol into a flex component from Flash
www.adobe.com/go/flex_ck_en
You can add all your move into a symbol then convert to flex component
Select the symbol from the libary then go to:
Commands > Convert Symbol to Flex Component
then import the swc resultant in flex
finally add the elemento into flex:
public var swfImported:NameOfTheSymbol=new NameOfTheSymbol();
private function init():void{
stage.addChild(swfImported);
}

Resources