【问题标题】:How to repaint Qt window or tabview in window如何在窗口中重绘 Qt 窗口或 tabview
【发布时间】:2015-08-11 02:16:40
【问题描述】:

我想点击一个按钮,然后弹出一个对话框。 Dialog 来自 Qml,有一个标签,该标签由另一个 JavaScript 文件中的变量号组成。当变量改变时,对话框应该重新绘制,新的数字将显示在对话框上。

MyDlg.qml:

import "MyJs.js" as MyJs

Window {

    id: myDialog
    width: 300
    height: 300

    TabView {
        id:myTabView
        width: parent.width
        height: parent.height

        Tab {
            title: "tab 1"
            id: myTab1
            text: MyJs.displayText
            }
     }
}

MyJs.js:

var displayText = "0";

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    绑定在 QML 和单独的 JavaScript 文件之间不起作用,只有 inline JavaScript expressions。我找不到任何明确说明这一点的文档,但在 previous answers 中也提到过。

    如果您不想步入 C++,请使用 QML 单例 (another answer to that question)。以下是您如何使用它的示例:

    MyDialog.qml

    import QtQuick 2.0
    import QtQuick.Controls 1.0
    import QtQuick.Window 2.0
    
    import "."
    
    Window {
        id: myDialog
        width: 300
        height: 300
    
    
        TabView {
            id:myTabView
            width: parent.width
            height: parent.height
    
            Tab {
                title: MySingleton.displayText
                id: myTab1
    
                Button {
                    text: "Click to change singleton property"
                    onClicked: MySingleton.displayText = "Hello"
                }
            }
        }
    }
    

    MySingleton.qml

    pragma Singleton
    
    import QtQml 2.0
    
    QtObject {
        property string displayText: ""
    }
    

    qmldir

    singleton MySingleton MySingleton.qml
    

    更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2015-08-02
      • 1970-01-01
      相关资源
      最近更新 更多