Component with code-controlled properties (without default value) - qt

Summary
How can I create a component in QML that allows me to specify custom properties with default values, which can be overridden by command-line parameters, but which do NOT initially use the default value?
Background
I've created a QML component that can be used to parse command-line properties easily and bind to their values. Its usage looks like this:
Window {
visibility: appArgs.fullscreen ? "FullScreen" : "Windowed"
width: appArgs.width
height: width*9/16
CommandLineArguments {
id: appArgs
property real width: Screen.width
property bool fullscreen: false
Component.onCompleted: args(
['width', ['-w', '--width'], 'initial window width'],
['fullscreen', ['-f', '--fullscreen'], 'use full-screen mode']
)
}
}
$ ./myapp --help
qml: Command-line arguments supported:
-w, --width : initial window width (default:1920)
-f, --fullscreen : use full-screen mode (default:false)
It works quite well, except...
Problem
All bindings that are created to my component initially use the default value. For example, if I launch my app with -w 800 the window's width initially starts with a value of 1920, and then ~immediately resizes to 800 (when the Component.onCompleted code runs). This problem is unnoticeable 90% of the time, mildly annoying 8% of the time...and unusable in the final 2%.
Sometimes the properties that I want to control can only be set once. For example, a network port to connect to using fragile code that cannot disconnect and reconnect to a new port when it changes. Or a mapping library that loads an enormous set of resources for one visual style, and then throws errors if I attempt to change the style.
So, I need the properties to get the command-line value—if specified—the very first time they are created (and otherwise use the default value). How can I make this happen?

UPDATE: Actually, in that particular case it is actually very easy to avoid the resizing - just set visibility to false, then set the properties to the desired values, and set visibility to true:
Window {
id: main
visible: false
Component.onCompleted: {
main.width = ARG_Width // replace with
main.height = ARG_Width * 9/16 // your stuff
main.visibility = ARG_Fullscreen ? Window.FullScreen : Window.Windowed
main.visible = true
}
}
In this case it is convenient since you can simply hide the window until you set the desired property values. In case you actually need to create the component with the correct initial values, you can do something like this:
Item {
id: main
Component {
id: win
Window {
visible: true
width: ARG_Width
height: width*9/16
visibility: ARG_Fullscreen ? Window.FullScreen : Window.Windowed
}
}
Component.onCompleted: win.createObject(main)
}
In this case the application will start without any window, the desired values will be set on prototype level, so that its creation will be delayed and have the right values right from the start.
It is understandable that this happens, after all you don't read in the arguments until after your application has loaded. Thus it will load up the defaults and then switch to the supplied arguments.
If you want to avoid that, the most straightforward solution would be to read in the arguments and expose them as context properties before the main qml file is loaded, such as this (posting fullly working code since you mentioned you are not a C++ guy):
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
int w = 1920; // initial
bool f = false; // values
QStringList args = app.arguments();
if (args.size() > 1) { // we have arguments
QString a1 = args.at(1);
if (a1 == "-w") w = args.at(2).toInt(); // we have a -w, read in the value
else if (a1 == "-f") f = true; // we have a -f
}
engine.rootContext()->setContextProperty("ARG_Width", w); // expose as context
engine.rootContext()->setContextProperty("ARG_Fullscreen", f); // properties
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); // load main qml
return app.exec();
}
And then in your main.qml file:
Window {
id: main
visible: true
width: ARG_Width
height: width*9/16
visibility: ARG_Fullscreen ? Window.FullScreen : Window.Windowed
}
As the component is created it picks up the correct values right away.

If I completely change the interface to my component, such that the default value is passed to a function that returns a value, then I can achieve my goals.
So, instead of:
property real width: Screen.width
...
Component.onCompleted: args(
['width', ['-w', '--width'], 'initial window width'],
)
I must use something like:
property real width: arg(Screen.width, ['-w', '--width'], 'real', 'initial window width')
This new interface has some disadvantages:
I can no longer specify the order I want arguments to appear in the help, as the properties may invoke arg() in any order.
I can no longer require positional arguments with no flags (e.g. app filename1 filename2) for the same reason.
I have to repeat the type of the property in the descriptor.
It has other benefits, however:
The names of properties do not have to be repeated.
It is fewer lines of code (one line per property instead of 2).
It actually solves my problem stated above.
Example usage:
CommandLineParameters {
id: appArgs
property string message: arg('hi mom', '--message', 'string', 'message to print')
property real width: arg(400, ['-w', '--width'], 'real', 'initial window width')
property bool fullscreen: arg(false, ['-f', '--fullscreen'], 'bool', 'use full screen?')
property var resolution: arg('100x200', '--resolution', getResolution)
function getResolution(str) {
return str.split('x').map(function(s){ return s*1 });
}
}
The Code:
// CommandLineParameters.qml
import QtQml 2.2
QtObject {
property var _argVals
property var _help: []
function arg(value, flags, type, help) {
if (!_argVals) { // Parse the command line once only
_argVals = {};
var key;
for (var i=1,a=Qt.application.arguments;i<a.length;++i){
if (/^--?\S/.test(a[i])) _argVals[key=a[i]] = true;
else if (key) _argVals[key]=a[i], key=0;
else console.log('Unexpected command-line parameter "'+a[i]+'');
}
}
_help.push([flags.join?flags.join(", "):flags, help||'', '(default:'+value+')']);
// Replace the default value with one from command line
if (flags.forEach) flags.forEach(lookForFlag);
else lookForFlag(flags);
// Convert types to appropriate values
if (typeof type==='function') value = type(value);
else if (type=='real' || type=='int') value *= 1;
return value;
function lookForFlag(f) { if (_argVals[f] !== undefined) value=_argVals[f] }
}
Component.onCompleted: {
// Give help, if requested
if (_argVals['-h'] || _argVals['--help']) {
var maxF=Math.max.apply(Math,_help.map(function(a){return a[0].length}));
var maxH=Math.max.apply(Math,_help.map(function(a){return a[1].length}));
var lines=_help.map(function(a){
return pad(a[0],maxF)+" : "+pad(a[1],maxH)+" "+a[2];
});
console.log("Command-line arguments supported:\n"+lines.join("\n"));
Qt.quit(); // Requires connecting the slot in the main application
}
function pad(s,n){ return s+Array(n-s.length+1).join(' ') }
}
}

Related

model.rowCount() won't bind to Item's property

I have a ListView which is initialized with a custom C++ model named rootModel inside Qml.
The model inherits QAbstractListModel and defines a QVector<customType> as a private member to populate the model.
In my ApplicationWindow I have created a Dialog in which I change the model and call the setList() function to update it. This works fine.
I also want to connect the model's size to a ScrollView's int property. This property will define the children of a RowLayout.
The problem is that when I try to bind this property to the model's size, the application crashes.
FYI, all the modifications of the model follow the Qt's rules. rowCount() is Q_INVOKABLE. I have also tried using the onModelChanged handler but this didn't work (I have checked in the documentation that this signal is emitted when modelReset() is emitted, which is taking place inside setList() through endResetModel()
I believe this is a straightforward procedure (have already performed property bindings many times in my project) but doesn't work as expected.
I quote some sample code of my project.
//main.qml
ConfiguredChannels{
id: configuredList
anchors{
left: parent.left
top: devices.bottom
right: tabs.left
bottom: parent.bottom
}
}
TabArea {
id: tabs
y: toolBar.height
x: parent.width / 8
anchors {
top: toolBar.bottom
}
width: 3 * parent.width / 4
height: 3 * parent.height / 4
countPWM: configuredList.model.rowCount() //This is where I want to bind.
}
//ConfiguredChannels.qml
id: confChanView
header: confChanHeader
model: ChannelModel{
id: rootModel
list: channelList
}
//TabArea.qml
Item{
id: tabAreaRoot
property alias channelsPWM: channelsPWM
property int countPWM
ScrollView{
id: scrollPWM
anchors.fill: parent
contentItem: channelsPWM.children
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOn
RowLayout{
id: channelsPWM
spacing: 0
Layout.fillHeight: true
Layout.fillWidth: true
Component.onCompleted: {
var namesPWM = [];
for (var i=0; i<countPWM; i++){
namesPWM.push("Channel"+(i+1));
}
createChannels(countPWM, "PWM", channelsPWM, namesPWM);
}
}
}
[EDIT 1]
After looking closer I realized that with my current implementation, even if I bind properly to the model's size I would still not be able to create the desired amount of RowLayout's children on demand (after I change the model in the Dialog Configuration.qml).
That is because I have put their creation inside RowLayout's Component.onCompleted handler. The content of this handler will be executed once the Configuration.qml will be initialized inside main.qmlfor the first time. After that, any other change to the countPWM will not make a difference since the Component is already Completed! Please correct me if I am wrong at this point.
Based on that, I have followed another implementation. I have created a "wrapper" function of createChannels, named createStrips(countPWM). This way, to update properly the RowLayout's children I have to call this function.
\\Configuration.qml
\\more code
currentModel.setList(newList)
tabs.createStrips(tableModel.count) \\tableModel is used to populate the newList that will be set to the model
newList.clear()
\\more code
\\TabArea.qml
function createStrips(countPWM){
var namesPWM = [];
for (var i=0; i<countPWM; i++){
namesPWM.push("Channel"+(i+1));
}
createChannels(countPWM, "PWM", channelsPWM, namesPWM);
}
function createChannels(counter, channelType, channelParent, channelMapping){
if ( channelParent.children.length !== 0){
console.log("destroying");
for ( var j = channelParent.children.length; j > 0 ; j--){
channelParent.children[j-1].destroy();
}
}
for (var i=0;i<counter;i++){
var component = Qt.createComponent(channelType+".qml");
if( component.status !== Component.Ready )
{
if( component.status === Component.Error )
console.debug("Error:"+ component.errorString() );
return; // or maybe throw
}
var channels =component.createObject(channelParent, { "id": channelType+(i+1), "channelText.text": channelMapping[i]});
}
[EDIT 2]
Although the solution in EDIT 1 works and produces the right children for my ScrollView I don't think it is good enough and I believe the best implementation would be to bind the model's size change with the call to the createStrips(countPWM) function. Something like:
\\main.qml
ConfiguredChannels{
id: configuredList
anchors{
left: parent.left
top: devices.bottom
right: tabs.left
bottom: parent.bottom
}
onModelChanged: tabs.createStrips(model.rowCount) //or an appropriate signal handler defined on C++ side
}
And perhaps even better, make the creation of the children as a custom qml signal handler that will be emitted every time the model's size is changed. (I tried the onModelChanged as above but didn't work. Probably I am missing in which case is this signal emitted)
[SOLUTION]
I followed the instructions of the accepted answer as well as this link.
I added a Q_PROPERTY to my model's definition inside the header file named rowCount with the NOTIFY rowCountChanged as well as the signal void rowCountChanged(); . Also, inside the function setList(newList) which I use to update the model, I added at the end of its implementation the emit rowCountChanged(); . Last I connected this signal with my function createStrips(count) inside QML. Now every time the model's size is changed, my ScrollView will update automatically the strips shown as the RowLayout's children.
\\ChannelModel.h
...
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged)
...
signals:
void rowCountChanged();
\\ChannelModel.cpp
void ChannelModel::setList(ChannelList *list)
{
beginResetModel();
...
endRestModel();
emit rowCountChanged();
}
\\main.qml
Connections {
target: configuredList.model
onRowCountChanged: tabs.createStrips(configuredList.model.rowCount)
}
Only the q-property allow a binding, in your case the Q_INVOKABLE is not, so you will have to create it, for this we use the signal rowsInserted and rowsRemoved as shown below:
*.h
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged)
public:
...
signals:
void rowCountChanged();
*.cpp
//constructor
connect(this, &QAbstractListModel::rowsInserted, this, &YourModel::rowCountChanged);
connect(this, &QAbstractListModel::rowsRemoved, this, &YourModel::rowCountChanged);
*.qml
countPWM: configuredList.model.rowCount // without ()
Note:
I'm assuming that when you add an element or remove it, you're using:
beginInsertRows(QModelIndex(), rowCount(), rowCount());
//append data
endInsertRows();
Or:
beginRemoveRows(QModelIndex(), from, to)
// remove
endRemoveRows();
You can't bind to a Q_INVOKABLE since there is no association to a change-signal.
Create a Q_PROPERTY(count READ rowCount NOTIFY rowCountChanged) or something like that. Make sure that the signal rowCountChanged is emitted when rows are inserted or removed.
Of course it would be more canonical if you had a
Q_PROPERTY(count READ count NOTIFY countChanged)
int count() { return rowCount(); }
and make sure to emit countChanged.
Then you can bind to the property count.

Repater: Using C++ property as model

I have a C++ property
Q_PROPERTY(QList<qreal> XTickPos MEMBER _xTickPos);
which I want to use in a Repeater. In the same QML file, the c++ class has been given the id
id: pw
The repeater looks like this
Item {
anchors.fill: parent
visible: true
Repeater {
model: pw.XTickPos.length
Rectangle{
height: 50
width: 2
x: pw.XTickPos[index]
y:10
visible: true
color: "black"
border.width: 2
}
}
}
However, nothing is drawn on the screen. If instead I make property in the QML file:
var xTickPos = []
and set it via a Q_Invokable function in c++
Q_INVOKABLE QList<qreal> getXTickPositions();
and in QML
root.xTickPos=pw.getXTickPositions();
and use the QML property xTickPos as model in the above repeater it is working. I checked that pw.XTickPos is correctly filled via a console.log
What am I missing here?
This one is kinda tricky.
The documentation states that you can use a JS array as a model, and it does state that QList<qreal> is automatically converted to a JS array when returned to QML.
But it seems that you can't use a QList<qreal> that is automatically converted to a JS array as a model. Go figure...
Naturally, it is preferable to have a proper model with its proper notifications for top efficiency. But in case you really want to go for the list property, it appears you will have to do the conversion manually in a getter:
QVariantList model() {
QVariantList vl;
for (auto const & v : yourList) vl.append(v);
return vl;
}
Amazingly, although Qt supposedly makes that conversion automatically, it doesn't seem to be able to make a QVariantList from a QList<qreal>.
That's Qt for you...

pass data from one window to another (inside same QML file)

i got two Windows inside the same .qml file.
Window1 has a textinput1 and a button, and when I press the button, i'd like to send the string value from that textinput to the Window2 textInput2.
I'm new to Qt and QML, been reading a lot on signals, Loaders, properties and can't materialize this kind of transfer. Can anyone do a simple 10-line example of such transfer please?
Window {
id:window
TextInput {
id:text1
text: "This value is needed in the second Window!"
}
Button {
onClicked: window2.open()
}
Window {
id.window2
function open(){
visible = true
}
Text {
text: text1.text
}
}
}
If I do this it gives me ReferenceError: text1 is not defined, how can I reference the text1 from the first Window?
I would prefer to use signals in such case:
Window {
id: window1
title: "window 1"
visible: true
width: 600
height: 600
signal someSignal()
Button {
anchors.centerIn: parent
text: "Open window"
onClicked: window1.someSignal()
}
Window {
id: window2
title: "window 2"
width: 400
height: 400
// you can use this instead of Connections
//Component.onCompleted: {
// window1.someSignal.connect(show);
//}
}
Connections {
target: window1
onSomeSignal: {
window2.show();
}
}
}
I think this is more ... how do you say? ... more imperative -)
i got two Windows inside the same .qml file.
If you did then your code will work. Since it doesn't work, I will assume each window is defined in its own qml file, and you only instantiate the two windows in the same qml file.
If I do this it gives me ReferenceError: text1 is not defined, how can
I reference the text1 from the first Window?
You will have to be able to reference the window first, and it should provide an interface to reference the text.
Keep in mind that ideally ids should only be used to reference stuff in the same source. On rare occasions you could go further, and reference ids down the object tree, but only parents, and none of their out-of-line children, it will however work for in-line children that are given ids in that same source. Meaning that if window2 is created inside window then you will be able to reference window from inside window2. But if both windows are siblings in another object, the id won't resolve.
Obj1
Obj2
Obj4
Obj3
In this example object tree, Obj1 will resolve from any of the objects. However, Obj3 will not be able to resolve Obj2 if the id is given inside Obj2, but will resolve if the id for Obj2 is given inside Obj1. But there is no way to resolve Obj4 from Obj3. because the id doesn't act like a property, you can't do someId.someOtherId, that's only possible for properties. You cannot do somePropertyObject.someId neither. You can only begin with either an id or a property, and continue only with sub-properties.
When the id is not applicable, can expose objects or properties either as properties or property aliases. The first is useful when you want to expose a whole object, the second when you want to expose a particular property of an object:
Item {
property Item innerItem: inner // gives you access to the "entire" inner object
property alias innerWidth: inner.width // gives you access to a property of inner
Item {
id: inner
}
}
You can also have aliases to aliases.

Dynamically retranslate Qt Quick UI

I want to dynamically retranslate Qt Quick GUI strings.
There is intrusive trick to retranslate affected string properties, whose notifications about changes cannot be centralized.
Is it possible to make qsTr (and others) to return string-like objects, which behaves exactly like string, but also behaves like global properties connected to common "valueChanged" signal (which I want to emit, when QEvent::LanguageChange in QCoreApplication occured).
I think I can use twitching of Loader's active property, which contains entire top level GUI element to make all the user-visible strings retranslated, but this approach results in lost of the state of all the items and components, connected to the Loader and not differs from complete application restart for me.
Is it possble to create such myQsTr function?
From Qt 5.10 onwards, you can call QQmlEngine::retranslate() after you have installed a new translator with QCoreApplication::installTranslator(), to ensure that your user-interface shows up-to-date translations.
You could opt to use your own, 100% QML solution like that:
// Tr.qml
// also put `singleton Tr Tr.qml` in the qmldir file
pragma Singleton
import QtQuick 2.7
QtObject {
function t(s) {
if (lang === eng) return s
var ts = lang[s]
return ts ? ts : s
}
property var lang: eng
readonly property var eng : {
"hello" : "hello",
"goodbye" : "goodbye"
}
readonly property var ger : {
"hello" : "hallo",
"goodbye" : "auf wiedersehen"
}
readonly property var esp : {
"hello" : "hola"
}
}
// test it out
import QtQuick 2.7
import QtQuick.Controls 2.1
import "." // same old singleton bug
ApplicationWindow {
id: main
visible: true
width: 640
height: 480
color: "darkgray"
Column {
Text { text: Tr.t("hello") }
Text { text: Tr.t("goodbye") }
Button { text: "Eng"; onClicked: Tr.lang = Tr.eng }
Button { text: "Ger"; onClicked: Tr.lang = Tr.ger }
Button { text: "Esp"; onClicked: Tr.lang = Tr.esp }
}
}
The different language objects act like a map<string, string> and every time you change lang this will cause the binding expressions to reevaluate and refresh the value form the current language dictionary.
This solution will also fallback to the default language string if a translation is not found. You can easily customize the behavior and you don't rely on any external tooling. Clean, simple, self-contained and totally under your control.

Crash in QQuickItem destructor / changeListeners when closing application (Qt 5.6)

We have a fairly big QtQuick application, with a lot of modal dialogs. All of these modals share a consistent look and behaviour, and have leftButtons, rightButtons, a content and additional warning widgets. We use the following base class (PFDialog.qml):
Window {
property alias content: contentLayout.children
ColumnLayout {
id: contentLayout
}
}
and declare dialogs in the following way (main.qml):
Window {
visible: true
property var window: PFDialog {
content: Text { text: "Foobar" }
}
}
The problem is that when the application is closed, a segfault happens in the QQuickItem destructor. This segfault is hard to reproduce, but here is a surefire way of making it happen: with visual studio in debug mode, freed memory is filled with 0xDDDDDDD with triggers the segfault every time.
Full example application can be found here: https://github.com/wesen/testWindowCrash
The crash happens in QQuickItem::~QQuickItem:
for (int ii = 0; ii < d->changeListeners.count(); ++ii) {
QQuickAnchorsPrivate *anchor = d->changeListeners.at(ii).listener->anchorPrivate();
if (anchor)
anchor->clearItem(this);
}
The reason for this is that the content of our dialog (the Text item in the example above) is a QObject child of the main Window, but a visual child of the dialog window. When closing the application, the dialog window is destroyed first, and at the time the Text item is deleted, the dialog window (still registered as a changeListener) is stale.
Now my question is:
is this a QtQuick bug? Should the dialog deregister itself as a changeListener for its children when it is destroyed (I think it should)
is our property alias content: layout.children pattern correct, or is there a better way to do this? This also happens when declaring a default property alias.
For the sake of completeness, here is how we hotfix it in our application. When content changes, we reparent all the items to the layout item. A of elegance, as you will all agree.
function reparentTo(objects, newParent) {
for (var i = 0; i < objects.length; i++) {
qmlHelpers.qml_SetQObjectParent(objects[i], newParent)
}
}
onContentChanged: reparentTo(content, contentLayout)
I have had this problem lots of times, I don't think it is a bug, more like a design limitation. The more implicit behavior you get, the less control you have, leading to inappropriate orders of object destruction and access to dangling references.
There are numerous situations where this can happen "on its own" as you exceed the bounds of a trivial "by the book" qml application, but in your case it is you who's doing it.
If you want proper ownership, don't use this:
property var window: PFDialog {
content: Text { text: "Foobar" }
}
Instead use this:
property Window window: dlg // if you need to access it externally
PFDialog {
id: dlg
content: Text { text: "Foobar" }
}
Here is a good reason why:
property var item : Item {
Item {
Component.onCompleted: console.log(parent) // qml: QQuickItem(0x4ed720) - OK
}
}
// vs
property var item : Item {
property var i: Item {
Component.onCompleted: console.log(parent) // qml: null - BAD
}
}
A child is not the same as a property. Properties are still collected but they are not parented.
As for achieving the "dynamic content" thingie, I've had good results with ObjectModel:
Window {
property ObjectModel layout
ListView {
width: contentItem.childrenRect.width // expand to content size
height: contentItem.childrenRect.height
model: layout
interactive: false // don't flick
orientation: ListView.Vertical
}
}
Then:
PFDialog {
layout: ObjectModel {
Text { text: "Foobar" }
// other stuff
}
}
Lastly, for the sake of doing explicit cleanups before closing the application, on your main QML file you can implement a handler:
onClosing: {
if (!canExit) doCleanup()
close.accepted = true
}
This ensures the window will not be destroyed without doing the cleanup first.
Finally:
is our property alias content: layout.children pattern correct, or is
there a better way to do this? This also happens when declaring a
default property alias.
It wasn't last time I looked into it, but it was at least couple of years back. It would certainly be nice to have objects declared as children actually becoming children of some other object, but at the time this was not possible, and still may not be. Thus the need for the slightly more verbose solution involving the object model and the list view. If you investigate the matter and find something different, leave a comment to let me know.
I believe that you cannot declare a Window Object in a var. In my tests the SubWindow never open and sometimes broken on startup.
A Window can be declared inside an Item or inside another Window; in that case the inner Window will automatically become "transient for" the outer Window
See: http://doc.qt.io/qt-5/qml-qtquick-window-window.html
Modify your code to this:
Window {
visible: true
PFDialog {
content: Text { text: "Foobar" }
}
}

Resources