I have some form with fiels, combos etc. I would like to go over through all the widgets on the form and if for example it's textfield, clear it, something like that:
foreach(QObject *child, this->ui->children())
{
QLineEdit *txtField = qobject_cast<QLineEdit *>(child);
if (txtField)
{
txtField->clear();
}
}
The problem is the ui object doesn't have such property like children and I don't know how to get the whole collection of children widgets.
Maybe the form object has something like Clear method. It would be the best.
Thanks
Try it.
foreach(QLineEdit *widget, this->findChildren<QLineEdit*>()) {
widget->clear();
}
How many QLineEdits are we talking about? Unless there's a good reason I would just add a method to the class that has ui as a member and do it by hand, like this:
void clearform()
{
this->ui->firstlineedit->clear();
this->ui->secondlineedit->clear();
...
this->ui->nthlineedit->clear();
}
A good reason why you can't do it this way is that the QLineEdits are dynamically added and you don't have them as members. In that case you can call QObject::children on the widget that is the parent of all your QLineEdits, if they share some common name, QObject::findChildren will also work.
Related
I'd like to implement a text field in Qt that replicates the "Tags" field on stackoverflow (when submitting a question). Certain keywords would be replaced by widgets and if I backspace a widget it should be replaced with the text that was typed to create that widget. How could I accomplish something like this? Thanks.
You can start by inheriting a QWidget and implementing the KeyPress and KeyRelease events. Basically mimicking a QLineEdit widget. So you might need to look for re-usable code to avoid a lot of work, like inheriting QLineEdit and overwriting paintEvent().
Then, in your paintEvent, you use the painter to render the non-widget text and your widgets.
pseudocode:
SomeWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
foreach (object ; objectsToDraw) {
if (isText) {
painter.drawText();
} else if (isWidget) {
widget.render(&painter);
}
}
}
I want to create a custom filter for my DataTable. I would like to create a button that, when clicked on it, changes the table its data. I know about the ChoiceFilteredPropertyColumn that wicket has to offer but this is, according to my understanding of it, a dropdown filter.
I am trying to achieve something like the following picture (Pancakes is the clickable button):
Could someone point me in the right direction?
Well... the superclass of ChoiceFilteredPropertyColumn is FilteredPropertyColumn which might do the trick. Otherwise you could always implement your own Column that implements IFilteredColumn the way you like it.
Look at how the implemented `DataTable' here: http://www.packtpub.com/sites/default/files/1605OS-Chapter-5-Displaying-Data-Using-DataTable.pdf
Then you could implement your dropdown button filter like you want it and filter with the selected value the DataTable.
I solved this question by creating a custom filter (just a panel with some markup) and return it in the getFilter method of a custom FilteredPropertyColumn.
FilteredPropertyColumn:
http://wicket.apache.org/apidocs/1.4/org/apache/wicket/extensions/markup/html/repeater/data/table/filter/FilteredPropertyColumn.html
getFilter method:
http://wicket.apache.org/apidocs/1.4/org/apache/wicket/extensions/markup/html/repeater/data/table/filter/IFilteredColumn.html#getFilter%28java.lang.String,%20org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm%29
ButtonFilter class:
public class ButtonFilter extends Panel {
...
}
In custom FilteredPropertyColumn class:
#Override
public Component getFilter(String componentId, FilterForm<?> form) {
return new ButtonFilter<Y>(componentId, getFilterModel(form), filterChoices);
}
This seems like something that should be painfully simple, but I can't even find how to loop through rows in a Flex DataGrid.
Basically what I'm trying to accomplish is something like this pseudo-code:
for each(var row:Row in myDataGrid.Rows)
{
if(row.DataObject.Number == 1)
{
row.Color = Red;
}
}
I'm trying to have a Save button that upon being clicked either processes the save, or highlights the invalid rows and pops up a message telling the user why the rows are invalid. Because of some other complexities, I am not able to validate each row as it is entered. Any help is appreciated! Thanks.
Data grids are intended to be driven by their data rather than manipulated directly. One way to accomplish what you are trying to do is to add some sort of property, say "valid", to the data objects in your provider and add code to the renderer to alter its appearance based on the state of "valid". In that way you could loop through the objects in your data provider and set the "valid" property based on your validation check, which would cause the rows in the data grid to change their appearance automatically.
Hope that helps.
Try something like this:
for each(var o:Object in myDataGrid.dataProvider)
{
if(o.Number == 1) {
myDataGrid.selectedItems.push(o);
}
}
In your mxml you can set the selectionColor of the datagrid to red. See: http://blog.flexexamples.com/2008/02/19/setting-the-selection-color-and-selection-disabled-color-for-a-row-in-the-flex-datagrid-control/
Let me know if this works for you!
I am not sure you can do it on the data grid itself, but if you have an item renderer for each of the items, you can have your highlighting logic there.
basically, you define your datagrid's item renderer class:
<mx:DataGrid itemRenderer="ItemRendererClass"(...) ></mx:DataGrid>
and then you define the class "ItemRendererClass" as implementing IDataRenderer:
implements="mx.core.IDataRenderer"
This is a simplistic explanation, assuming you can figure out how to do this on yourself :)
I achieved this by overriding the set data. I have provided the sample code below.
override public function set data(value:Object):void
{
super.data=value;
if(value!=null && value.hasOwnProperty("state") && value.state == "Final State"){
setStyle("color", 0xb7babc);
}else{
setStyle("color", 0x000000);
}
this.selectable=false;
super.invalidateDisplayList();
}
I have a List of items which is based on the contents of the "category" that a user selects
When the user changes selection, I change the dataProvider of the list be be the contents of the current category.
Sometimes the list contains items, sometimes it does not
Is there a way of hiding the list when it has no items?
I know that I could do this when setting the dataProvider, but it seems like there should be an event or something else that I could be using.
You could try
visible="{myList.dataProvider.length>0}"
includeInLayout="{myList.dataProvider.length>0}"
where "myList" is the id of your List component.
My first solution to this was to override set dataProvider:
override public function set dataProvider(value:IList):void {
super.dataProvider = value;
this.setVisible(value.length > 0);
}
This did work, however Robusto's solution works also and is preferable IMO.
I have a quite large Flex application with a large set of views and I ceratain views I'd like to add shortcuts.
And i'm looking for something like:
<mx:Vbox>
<foo:Shortcut keys="ctrl+s" action="{bar();}"/>
....
</mx:VBox>
Is there any framwork or component already done that does something like this? I guess it should be too difficult to build? When building this I only want the shortcuts to be active when the view is visible. How do I detect this? What base class is best to inherit from when working with non visual components?
I don't know of any framework component that does that already, but the examples above should get you started if you try to build your own.
There's no need to inherit from any component for a non-visual component like the one you've described here (your "foo" class needs no parents.) There's nothing in the Flex framework you need to inherit from for this.
However you architect it, your foo class is going to have to take in and parse keyboard codes to listen for and accept one or more methods to call. All you have to do is figure out when to add and remove the event listeners that will call the passed-in methods.
To handle turning your keyboard events on and off based on visibility, just have your foo component bind to the "visible" property of it's parent and add/remove event listeners accordingly.
You might also consider having the listeners added when the component that foo is nested in is on the display list rather than just visible. To do this, simply added and remove your event listeners in one of the component lifecycle methods - probably commitProperties is the most appropriate.
I don't think this solution answer your question directly but anyway, to help solve your problem here is an example.
For instance, I've extended the TextArea component like so. This is the best I can do so far, it can definitely be improved upon. Like, I don't know how to make the cursor go to the end after the next shortcut is pressed.
public class TextArea extends mx.controls.TextArea
{
// the keysmap is an example dictionary holding keycodes
private var keysmap:*={
112 = "some text for F1"
,113 = "the text for F2!"
//etc, etc
}
public var handleKeyDown:Boolean =false;
public function TextArea(){
if(handleKeyDown ==true){
this.addEventListener(KeyboardEvent.KEY_DOWN,this.keydownHandler);
}
}
public function keydownHandler(e:KeyboardEvent):void{
if(e.keyCode >= 112 && e.keyCode <= 123){
e.currentTarget["text"] += String(keysmap[e.keyCode]) +" ";
}//focusManager.setFocus(this);
}
}
I can't give you a solution using MXML, however my first thought would involve a singleton static class with a Dictionary that contains a list of objects as its keys and dynamically created dictionaries as the value pairing that contain keys denoting the desired key press with a function reference as the value.
So, say you had a Sprite and you wanted to capture ctrl+s for save when focus is on that object, I would get the instance of that Singleton, and call a function such as registerKeyBinding passing in the Sprite, the keyCode you want, and your pre-defined callback:
private var registeredObjects:Dictionary = new Dictionary(true);
public function registerKeyBinding(targetObject:Object, keyCode:int, callback:Function) {
if (registeredObjects[targetObject]) {
Dictionary(registeredObjects[targetObject])[keyCode] = callback;
}
else {
registeredObjects[targetObject] = new Dictionary();
Dictionary(registeredObjects[targetObject])[keyCode] = callback;
targetObject.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
}
}
private function keyDownListener(e:KeyboardEvent):void {
if (e.ctrlKey == true) {
//calls the function if that key exists.
Dictionary(registeredObjects[e.target])[e.keyCode];
}
}
Can't say I've tested this, but it was just the first thing that popped into my head. You could then setup functions to deregister and delete keys from the dictionaries, check states of the objects in addition to the keyCodes, remove old listeners, and delete entire dictionaries when there is no longer a need for them. Hopefully this is at least a tiny bit helpful.