1

I have the following qml file:

import QtQuick 2.2
import QtQuick.Dialogs 1.2

FileDialog 
{
 property string myTitle: "Select file to open"
 property string myfilter: "All files (*)"

 id: fileDialog
 objectName: "fileDialogObj"
 title: myTitle
 folder: shortcuts.home
 sidebarVisible : true
 nameFilters: [ myfilter ]
 onAccepted: 
 {
  close()
 }
 onRejected: 
 {
  close()
 }
 Component.onCompleted: visible = true
}

I want to set the title property from the C++ code. I have code that looks like:

QQmlEngine engine;
QQmlComponent component( &engine );
component.loadUrl( QUrl( QStringLiteral( "qrc:/qml/my_file_dialog.qml" ) ) );
QObject* object = component.create();
object->setProperty( "myTitle", "Open file!" );

The title has the initial value (Select file to open) of the property myTitle and never changes to Open file!

What am I doing wrong?

UPDATE I've also tried to update the title directly from C++ code.

Considering I have the dialog object, I update the tile like this:

QQmlProperty::write( dialog, "title", "testing title" );

And also like this:

dialog->setProperty( "title", "testing title" );

The property title of the file dialog is not set.

As @Tarod mentioned in his answer, it seems to be a bug.

Or am I missing something?

mtb
  • 1,350
  • 16
  • 32
  • What about an alias: `property alias myTitle: fileDialog.title` together with the setProperty? I assume the property binding may be broken by the property change, or there is none. An alias will enforce that binding. – martin May 24 '18 at 14:39

1 Answers1

0

It seems a bug, because the next code works if we set

title = "xxx"

instead of

title = myTitle

Also, you can check other properties are properly updated. I.e. sidebarVisible

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QDebug>    

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
    QObject *object = component.create();

    QObject *fileDialog = object->findChild<QObject*>("fileDialogObj");

    if (fileDialog)
    {
        fileDialog->setProperty("myTitle", "new title");
        fileDialog->setProperty("sidebarVisible", true);
        qDebug() << "Property value:" << QQmlProperty::read(fileDialog, "myTitle").toString();
    } else
    {
        qDebug() << "not here";
    }

    return app.exec();
}

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQml 2.2
import QtQuick.Dialogs 1.2

Item {
    FileDialog
    {
        property string myTitle: fileDialog.title
        property string myfilter: "All files (*)"

        id: fileDialog
        objectName: "fileDialogObj"
        title: "Select file to open"
        folder: shortcuts.home
        sidebarVisible : true
        nameFilters: [ myfilter ]

        onAccepted:
        {
            close()
        }
        onRejected:
        {
            close()
        }
        Component.onCompleted:
        {
            visible = true
        }
        onMyTitleChanged:
        {
            console.log("The next title will be: " + myTitle)
            title = myTitle
        }
    }
}
Tarod
  • 6,732
  • 5
  • 44
  • 50
  • I will wait a while to clarify if this is a bug, and then, if no other feedback is given, I will accept your answer: it is a bug. – mtb May 19 '17 at 07:43