0
votes

I have a Qt QML application written in the QtCreator and I want to add a button that gives users the ability to toggle the window flag 'StayOnTop'. I'm not looking for specifics on how to create a button and all that jazz, im simply looking for help on the function that does the window flag toggles. I know how todo this in Python/Pyside but not QML C++.

For example how do i translate this into my qml application?

def toggle_stay_on_top(self):
  if self.stayOpTopAct.isChecked():
    # enabled
    self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
  else:
    # disable
    self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)

I tried using this but the title bar and min/max/close buttons disappeared.

flags: Settings.stayOnTop ? flags | Qt.WindowStaysOnTopHint : flags & ~Qt.WindowStaysOnTopHint
1

1 Answers

1
votes

Use a Window in your QML file. There is a property flags that you can use.

Window {
    id: root
    flags: Qt.Tool // Just to check if the flag isn't removed
    visible: true
    width: 500
    height: 500
    Rectangle {
        anchors.fill: parent
        color: "red"
        CheckBox {
                id: stayOpTopAct
                text: "Stay on top"
                onCheckStateChanged: {
                    if(checked)
                        root.flags = root.flags | Qt.WindowStaysOnTopHint
                    else
                        root.flags = root.flags & ~Qt.WindowStaysOnTopHint
                }
        }
    }
}