2017-05-05 15 views
1

C++에서 제목을 설정 I 다음 QML 파일이 있습니다QML의 FileDialog 코드

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 = myTitle

대신

title = "xxx"

을 설정하면 다음 코드가 작동하기 때문에

답변

0

그것은, 버그를 보인다. 나는. 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(); 
} 

I는 다른 피드백이 제공되지 않는 경우이 다음 버그이며, 만약 명확히 잠시 기다린다

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 
     } 
    } 
} 
+0

main.qml , 당신의 대답을 받아 들일 것입니다 : 그것은 버그입니다. – mtb