QML filedialog设置标题来自C 代码
QML FileDialog set title from C++ code
我有以下QML文件:
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!
我在做什么错?
更新我还尝试直接从C 代码更新标题。
考虑到我有对话框对象,我会这样更新瓷砖:
QQmlProperty::write( dialog, "title", "testing title" );
也这样:
dialog->setProperty( "title", "testing title" );
未设置文件对话框的属性标题。
正如@tarod在他的回答中提到的那样,这似乎是一个错误。
还是我错过了什么?
这似乎是一个错误,因为如果我们设置
,下一个代码有效 title = "xxx"
而不是
title = myTitle
另外,您可以检查其他属性已正确更新。IE。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
}
}
}
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 为什么抓取窗口标题的代码会导致应用崩溃?
- QML filedialog设置标题来自C 代码
- 如何在标题文件中获取成员功能的正确代码覆盖范围
- 在代码中的同一位置最有效地填充静态标题列表和动态值列表
- 当我把所有的代码放在标题时,我要考虑什么?