QML的FileDialog设置标题从C++代码
问题描述:
我有以下QML文件:QML的FileDialog设置标题从C++代码
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
}
我想设置从C++代码的title
财产。我有一些代码看起来像:
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(QUrl(QStringLiteral("qrc:/qml/my_file_dialog.qml")));
QObject* object = component.create();
object->setProperty("myTitle", "Open file!");
标题具有财产myTitle
的初始值(Select file to open
)并不会改变对Open file!
我在做什么错?
UPDATE 我也试着直接从C++代码更新标题。
考虑到我有对话框对象,我更新了瓷砖这样的:
QQmlProperty::write(dialog, "title", "testing title");
而且也是这样:
dialog->setProperty("title", "testing title");
文件对话框的产权未设置。
正如@Tarod在他的回答中提到的,这似乎是一个错误。
或者我错过了什么?
答
这似乎是一个错误,因为下一个代码工作,如果我们设置
title = "xxx"
,而不是
title = myTitle
此外,您还可以查看其他属性正确更新。即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
}
}
}
我会等待一段时间才能弄清楚,如果这是一个错误,然后,如果没有其他信息将反馈给,我会接受你的回答:这是一个错误。 – mtb