QDir::SortFlag memebers available in QML - qt

How can be made available the members of QDir::SortFlag to a QML script?
For instance, how to fill up a combo from a QML script with the members of QDir::SortFlag enum or QDir::SortFlags in order to choose some sort criterium?
Is this available through some C++ custom enum in a custom class only?

Related

Update InterfaceController with different data

Hi I am new to WatchKit development. I want to know that can I update a single interface controller with multiple data for button clicks
You could have many different objects in your InterfaceController, such as labels, image views, etc.
Updating WKInterfaceLabels
You should call the setText() method on the corresponding label.
For example, you have a button and a label, and you want to print "Hello" on the label when the button is clicked. In this case, you should connect an action to the button in your interface (by control-dragging button to the code), and then add the following code in the method created:
Swift
label1.setText("Hello")
Objective-C
[label1 setText:#"Hello"];
Updating WKInterfaceImages
You should call setImage() or setImageNamed() methods on the corresponding image view.
First, the image should be located in the Asset Catalog of your WatchKit App Target, must be bundled or available as an UIImage. Then you could use these codes:
Case #1: Available as file in bundle or Asset Catalog
Swift
image1.setImageNamed("imageName")
Objective-C
[image1 setImageNamed:#"imageName"];
Case #2: Available as an UIImage
Swift
image1.setImage(image)
Objective-C
[image1 setImage:image];
If you want to have animated photos, try this link.
Conclusion
To update WKInterfaceLabels, you should call the setText() method on the corresponding label.
To update WKInterfaceImages, you should call setImage() or setImageNamed() methods on the corresponding image view.
Resources
WKInterfaceImage Class Reference
WKInterfaceLabel Class Reference

Populate QComboBox with QEnums

I've looked in multiple places and cannot seem to find anything to work for my purpose.
I have this QComboBox that constantly changes (And could change after the program is completely finished).
To make it easier I made an Enum list for switch statements
public:
enum Race{
Race1,
Race2,
ect
}
The combo is filled with the same elements.
However, I want to make it even easier. So instead of changing the Combobox and changing the enum list, is there a way where all i have to do is add a new "race" to the enum list, that it will populate the combobox, so all i would have to do is a switch statement to handle new race?
Additional info:
I'm willing to put the enum list into a qstringlist.
I have Q_ENUMS (Race) set
Using Q_ENUMS will give you a way to iterate over your enum and add their enumerands to the combobox in a loop. However it will not give you the possibility to add a related GUI text for each enumerand. The enumerand name like it is written in your source code will be available only. Maybe your code side name is not what you want to see in your GUI as a text.
Remember that you can add any kind of QVariant supported user values to a combobox while you populate it with text strings... so you do not need to maintain an enumeration if there is some other kind key you can use instead.

Formatted text in QTreeWidgetItem

I need to create QTreeWidgetItems which have support for formatted texts, such as:
MyCreatedType - INTEGER(1)
(ie: the line above should have a "normal" part : MyCreatedType and a "formatted" part (INTEGER(1) in our case).
Any idea how to accomplish this?
Thanks.
What you need is a delegate. Delegates are explained here:
Star Delegate Example http://qt-project.org/doc/qt-4.8/itemviews-stardelegate.html
QItemDelegate Reference http://qt-project.org/doc/qt-4.8/qitemdelegate.html
The general procedure I follow when creating and using custom delegates:
Create a custom type with the information you want to encapsulate.
For your case, perhaps fields for the variable type name and type value.
Store these custom types in your model, wrapping them in QVariants to satisfy the return types required by QAbstractItemModel
Create a control that matches the UI you want.
In this case it might mean a QText label for "MyCreatedType" followed by a second label in bold for "Integer(1)".
Perhaps the control has methods like "setTypeName" and "setTypeValue"
Create a delegate that paints your specific control when your custom type is found.
You will have to map fields in the custom type to fields in the custom UI control as needed.
Associate your model and delegate with the Tree View you are using.
I hope this general procedure makes sense. I would recommend completing the Star Delegate Example and then reading my procedure, as it will make more sense with some background.

What's the need for registering user-defined classes in Qt?

When working with a Qt GUI application, what is the need for registering a user-defined class which is not derived from Q_OBJECT ?? I am referring to the use of qRegisterMetaType("ClassName").
Thanks,
Vishnu
From Qt's documentation of QtMetaType :
The QMetaType class manages named
types in the meta-object system. The
class is used as a helper to marshall
types in QVariant and in queued
signals and slots connections. It
associates a type name to a type so
that it can be created and destructed
dynamically at run-time. Declare new
types with Q_DECLARE_METATYPE() to
make them available to QVariant and
other template-based functions. Call
qRegisterMetaType() to make type
available to non-template based
functions, such as the queued signal
and slot connections.
Registering your custom type with qRegisterMetaType() lets you use it in signals and slots, as well as other "non-template based functions"

Qt equivalent of .NET data binding?

Is there an equivalent of .NET's data binding in Qt?
I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner if I could bind the data to these strings rather than either querying the database again based off of a new combobox selection or some other scheme based off of building my own index of entities that would be searched with the QStrings.
The best I've come up with is to derive these entities from QString and pushing them into the widgets this way, but I've yet to actually try it. I'm not sure if it will work the way I want it to, and it seems like a nasty hack.
If there is no data binding, what do you suggest?
Thank you.
As the user nonchalant mentioned in a comment you can use the QDataWidgetMapper class.
This is quite an easy way of binding arbitrary widgets to data that is stored in a QAbstractItemModel.
The example on the linked page shows in a few lines of code, how you can link your data model to common used input widgets:
QDataWidgetMapper *mapper = new QDataWidgetMapper;
mapper->setModel(model);
mapper->addMapping(mySpinBox, 0);
mapper->addMapping(myLineEdit, 1);
mapper->addMapping(myCountryChooser, 2);
mapper->toFirst();
One way is using Qt Model/View Classes (with base at QAbstractItemModel), but they need that you widget inherits QAbstractItemView (this is widgets like QTableView etc.).
If you want map Qt model to set of widgets, which haven't nothing common with QAbstractItemView you can use QDataWidgetMapper, which maps separate widget to Qt Model/View indexes. But anyway, as said Aaron Digulla, you must write some boiler plate code...
Well, for combobox specifically, you can set a model. For QObjects in general you can use the notify signal for properties to connect or other non-property related signals. I think there is another way to do it but I can't recall.

Resources