如何在应用程序窗口中跟踪项目的首次启动可见性

时间:2018-02-21 23:15:43

标签: qt qml qtquick2

我正在使用Qt 5.9.3。我有一个QML Item,如此:

Item {
    id: root
    width : 400
    height: 700
}

如何在应用程序窗口的屏幕上第一次看到项目时进行跟踪? onVisibilityChanged

如何对MyQuickItem进行跟踪,如下所示:

// C++ definition
class MyQuickItem : public QQuickitem {
}

// QML side
MyQuickItem {
  id: some_id
}

1 个答案:

答案 0 :(得分:1)

我想完整的代码假设如下:

ApplicationWindow{
    visible: true
    onVisibilityChanged: {
        console.log("ApplicationWindow is ", visibility)
    }
    Component.onCompleted: {
        console.log("ApplicationWindow is onCompleted")
    }
    Item {
        id: root
        width : 400
        height: 700
        onVisibleChanged: {
            console.log("Item is ", visible)
        }
        Component.onCompleted: {
            console.log("Item is onCompleted")
        }
    }
}

显然onVisibilityChanged ApplicationWindow会在visibility属性发生更改时触发,因此当您最小化并重新显示窗口时会触发Component.onCompleted。当你提到第一次可见时,我认为你正在寻找tshirts = [{'brand':'Hanes', 'color':'black'}, {'brand':'Hurley', 'color':'red'}] resDict = {} hasKey = False brand = input("Search a tshirt brand: ") for tshirt in tshirts: if tshirt["brand"] == brand: hasKey = True resDict = tshirt break if hasKey: if resDict['color']==input("Search color: "): print("Available") else: print("Not Available") else: print("Not available")