Here is the the thing, I have a header file with a list of defines, defining string constants. I'm doing my interface with QML. I was wondering if there is any way to import those defines so that I can reference from QML.
Related
As far as I know in QT QML is not possible to instantiate a C++ class in QML Component, unless it is a QQuickItem.
I would not like to put all ViewModels on ViewEngine context because it is a very bad pratice create all classes in memory without using.
My question is: How can I instantiate a C++ ViewModel ou Services/API from a single QML component without usnig ViewEngine Context.
Do ViewModels have to be QQuickItem type?
C++ objects do not need to be QQuickItems, they need to be QObjects. You just need to register your class with the QQmlEngine, like this:
qmlRegisterType<MyObject>("my.component.library", 1, 0, "MyObject");
Then in your qml files, you can instantiate that class like this:
import my.component.library 1.0
MyObject {
...
}
I have a python code that sending a dictionary to my QML. I want to know how could I have keys of dictionary and length of dictionary in QML?
You can't actually send a Python Dict to QML. This is because QML does not have a Dict object type. It is most likely translated into a JSObject which can be manipulated like a normal Javascript Object. Refer to http://www.w3schools.com/js/js_objects.asp for information on working with JS objects.
When *.ui files are saved, they generate an *.h file which defines and implements a translation method "retranslateui()" which uses a QApplication::translate call where the context for the translation equals the UI class name.
I need to be able to override that and put in my own custom context name.
The reason if you must know is that we have a non-QT legacy translation tables, with 14 languages. I built a tool that translates those strings into a *.TS file. The problem is, there IS NO CONTEXT, it's just a flat list of strings, so my tool forces one context for my entire application. The automatic generation of translate method calls where the context = the UI class name fails my ui translation.
I found that the UIC command line utility, creating this *.h file from the *.ui file has a parameter to define a different translation method. I can use this to route to my own translation method that'll add on the single context. My problem is that I can't find out how to change the UIC call from qmake to include this parameter with a custom value.
You can modify QMAKE_UIC property in your .pro file
QMAKE_UIC += -params to add
I've got a custom QML object called Target.qml. I would like to create dynamic instances of this object using Qt.createQmlObject().
It works when using builtin qml objects like Image:
var newTarget = Qt.createQmlObject('import Qt 4.7; Image {source: "widgets/SlideSwitchImages/knob.png"; }', parent);
But fails when using any custom object types like:
var newTarget = Qt.createQmlObject('import Qt 4.7; Target {}', parent);
If however I use my custom Target type statically in QML everything works. Is this a known limitation, any workarounds?
If you just need an arbitrary number of Target instances it would be better to use Component.
Component {
id: targetFactory
Target {}
}
var newTarget = targetFactory.createObject(parent, properties)
However if you want to stick to the Qt.createQmlObject call I guess you have the Target component in a different directory and you're just missing out some import statement. The string parameter must be the content of a QML file that works on its own in the same directory as the one calling it.
E.g.
var newTarget = Qt.createQmlObject('import QtQuick 1.0; import "../Targets"; Target {}', parent);
BTW: The Qt 4.7 imports are deprecated because they don't allow additional versions of QtQuick.
From the docs:
There are two ways to create objects dynamically from JavaScript. You can either call Qt.createComponent() to dynamically create a Component object, or use Qt.createQmlObject() to create an item from a string of QML. Creating a component is better if you have an existing component defined in a .qml file, and you want to dynamically create instances of that component. Otherwise, creating an item from a string of QML is useful when the item QML itself is generated at runtime.
I understand this to mean that createQmlObject will only work if you've defined the item type at runtime and that the application is therefore aware of the existence of it.
createComponent seems to perform the same function but for item types pre-defined in .qml files, as in your case.
In my flex app there are a few custom components. I want to create instance of these components at runtime and assign them properties by reading in a config file.
I know how to read xml and instantiate components, however my question is about being able
to get the type of the component from the xml attribute and then creating an instance of that type.
My xml looks like this:
You can instantiate an arbitrary named type through ActionScript's "reflection API":
var clazz:Class = Class(getDefinitionByName("class.from.your.xml.file.Name"));
var component:Object = new clazz();
http://livedocs.adobe.com/flex/3/langref/flash/utils/package.html#getDefinitionByName()
If you get an error about the type not existing, this is because it isn't linked from elsewhere in your application and the compiler only adds classes that are referenced. You can work around this using the following compiler arg:
includes class [...]
Links one or more classes to the resulting application SWF file, whether or not those classes are required at compile time.
http://livedocs.adobe.com/flex/3/html/compilers_14.html#157203