Pyside去除窗口标志

7

我正在设计一个 Pyside Qt 应用程序,我想在我的主窗口中切换 QtCore.Qt.WindowStaysOnTopHint 窗口标志。使用以下代码设置此提示可以正常工作:

self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.show()

但是我不知道如何使用Pyside移除窗口标志。

有人知道如何做吗?

3个回答

9

通常情况下,窗口标志会与现有标志一起进行OR运算:

    print(int(self.windowFlags()))
    self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
    print(int(self.windowFlags()))

然后要移除标志,使用该标志的否定进行 AND 运算:

    self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
    print(int(self.windowFlags()))

2
顺便提一下,为了更加视觉上的吸引力,可以使用二进制转换,例如 bin(int(self.windowFlags())) - Trilarion

3
你可以像这样切换窗口的显示位置:在顶部或底部。
def toggleFunc(self):
    if self.someCheckedButton.isChecked():  #show up at the top
        self.setWindowFlags(self.theMainWindow.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)

    else:  #show up at the bottom
        self.setWindowFlags(self.theMainWindow.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
    self.show()  #it's important to show up the window again after changing the window flags

1
谢谢你提供的 self.show(),我一直在寻找。这个答案被低估了。 - William Brochensque junior

0

这是用于测试窗口标志的示例代码:

enter image description here

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (QApplication, QPushButton, QWidget, QGroupBox, QGridLayout, QCheckBox, QRadioButton,
                               QTextEdit, QHBoxLayout, QVBoxLayout)


class ControllerWindow(QWidget):
    def __init__(self):
        super(ControllerWindow, self).__init__()
        self.previewWindow = PreviewWindow()
        self.createTypeGroupBox()
        self.createHintsGroupBox()
        self.quitButton = QPushButton("Quit")
        self.quitButton.clicked.connect(self.close)
        self.bottomLayout = QHBoxLayout()
        self.bottomLayout.addStretch()
        self.bottomLayout.addWidget(self.quitButton)
        self.mainLayout = QHBoxLayout()
        self.mainLayout.addWidget(self.typeGroupBox)
        self.mainLayout.addWidget(self.hintsGroupBox)
        self.mainLayout.addLayout(self.bottomLayout)
        self.setLayout(self.mainLayout)
        self.setWindowTitle("Window Flags")
        self.updatePreview()

    def updatePreview(self):
        if self.windowRadioButton.isChecked():
            flags = Qt.Window
        elif self.dialogRadioButton.isChecked():
            flags = Qt.Dialog
        elif self.sheetRadioButton.isChecked():
            flags = Qt.Sheet
        elif self.drawerRadioButton.isChecked():
            flags = Qt.Drawer
        elif self.popupRadioButton.isChecked():
            flags = Qt.Popup
        elif self.toolRadioButton.isChecked():
            flags = Qt.Tool
        elif self.toolTipRadioButton.isChecked():
            flags = Qt.ToolTip
        elif self.splashScreenRadioButton.isChecked():
            flags = Qt.SplashScreen

        if (self.msWindowsFixedSizeDialogCheckBox.isChecked()):
            flags |= Qt.MSWindowsFixedSizeDialogHint
        if (self.x11BypassWindowManagerCheckBox.isChecked()):
            flags |= Qt.X11BypassWindowManagerHint
        if (self.framelessWindowCheckBox.isChecked()):
            flags |= Qt.FramelessWindowHint
        if (self.windowNoShadowCheckBox.isChecked()):
            flags |= Qt.NoDropShadowWindowHint
        if (self.windowTitleCheckBox.isChecked()):
            flags |= Qt.WindowTitleHint
        if (self.windowSystemMenuCheckBox.isChecked()):
            flags |= Qt.WindowSystemMenuHint
        if (self.windowMinimizeButtonCheckBox.isChecked()):
            flags |= Qt.WindowMinimizeButtonHint
        if (self.windowMaximizeButtonCheckBox.isChecked()):
            flags |= Qt.WindowMaximizeButtonHint
        if (self.windowCloseButtonCheckBox.isChecked()):
            flags |= Qt.WindowCloseButtonHint
        if (self.windowContextHelpButtonCheckBox.isChecked()):
            flags |= Qt.WindowContextHelpButtonHint
        if (self.windowShadeButtonCheckBox.isChecked()):
            flags |= Qt.WindowShadeButtonHint
        if (self.windowStaysOnTopCheckBox.isChecked()):
            flags |= Qt.WindowStaysOnTopHint
        if (self.windowStaysOnBottomCheckBox.isChecked()):
            flags |= Qt.WindowStaysOnBottomHint
        if (self.customizeWindowHintCheckBox.isChecked()):
            flags |= Qt.CustomizeWindowHint

        self.previewWindow.setWindowFlags(flags)

        pos = self.previewWindow.pos()
        if (pos.x() < 0):
            pos.setX(0)
        if (pos.y() < 0):
            pos.setY(0)
        self.previewWindow.move(pos)
        self.previewWindow.show()

    def createTypeGroupBox(self):
        self.typeGroupBox = QGroupBox("Type")
        self.windowRadioButton = self.createRadioButton("Window")
        self.dialogRadioButton = self.createRadioButton("Dialog")
        self.sheetRadioButton = self.createRadioButton("Sheet")
        self.drawerRadioButton = self.createRadioButton("Drawer")
        self.popupRadioButton = self.createRadioButton("Popup")
        self.toolRadioButton = self.createRadioButton("Tool")
        self.toolTipRadioButton = self.createRadioButton("Tooltip")
        self.splashScreenRadioButton = self.createRadioButton("Splash screen")
        self.windowRadioButton.setChecked(True)
        layout = QGridLayout()
        layout.addWidget(self.windowRadioButton, 0, 0)
        layout.addWidget(self.dialogRadioButton, 1, 0)
        layout.addWidget(self.sheetRadioButton, 2, 0)
        layout.addWidget(self.drawerRadioButton, 3, 0)
        layout.addWidget(self.popupRadioButton, 0, 1)
        layout.addWidget(self.toolRadioButton, 1, 1)
        layout.addWidget(self.toolTipRadioButton, 2, 1)
        layout.addWidget(self.splashScreenRadioButton, 3, 1)
        self.typeGroupBox.setLayout(layout)

    def createHintsGroupBox(self):
        self.hintsGroupBox = QGroupBox("Hints")
        self.msWindowsFixedSizeDialogCheckBox = self.createCheckBox("MS Windows fixed size dialog")
        self.x11BypassWindowManagerCheckBox = self.createCheckBox("X11 bypass window manager")
        self.framelessWindowCheckBox = self.createCheckBox("Frameless window")
        self.windowNoShadowCheckBox = self.createCheckBox("No drop shadow")
        self.windowTitleCheckBox = self.createCheckBox("Window title")
        self.windowSystemMenuCheckBox = self.createCheckBox("Window system menu")
        self.windowMinimizeButtonCheckBox = self.createCheckBox("Window minimize button")
        self.windowMaximizeButtonCheckBox = self.createCheckBox("Window maximize button")
        self.windowCloseButtonCheckBox = self.createCheckBox("Window close button")
        self.windowContextHelpButtonCheckBox = self.createCheckBox("Window context help button")
        self.windowShadeButtonCheckBox = self.createCheckBox("Window shade button")
        self.windowStaysOnTopCheckBox = self.createCheckBox("Window stays on top")
        self.windowStaysOnBottomCheckBox = self.createCheckBox("Window stays on bottom")
        self.customizeWindowHintCheckBox = self.createCheckBox("Customize window")
        layout = QGridLayout()
        layout.addWidget(self.msWindowsFixedSizeDialogCheckBox, 0, 0)
        layout.addWidget(self.x11BypassWindowManagerCheckBox, 1, 0)
        layout.addWidget(self.framelessWindowCheckBox, 2, 0)
        layout.addWidget(self.windowNoShadowCheckBox, 3, 0)
        layout.addWidget(self.windowTitleCheckBox, 4, 0)
        layout.addWidget(self.windowSystemMenuCheckBox, 5, 0)
        layout.addWidget(self.customizeWindowHintCheckBox, 6, 0)
        layout.addWidget(self.windowMinimizeButtonCheckBox, 0, 1)
        layout.addWidget(self.windowMaximizeButtonCheckBox, 1, 1)
        layout.addWidget(self.windowCloseButtonCheckBox, 2, 1)
        layout.addWidget(self.windowContextHelpButtonCheckBox, 3, 1)
        layout.addWidget(self.windowShadeButtonCheckBox, 4, 1)
        layout.addWidget(self.windowStaysOnTopCheckBox, 5, 1)
        layout.addWidget(self.windowStaysOnBottomCheckBox, 6, 1)
        self.hintsGroupBox.setLayout(layout)

    def createCheckBox(self, text):
        checkBox = QCheckBox(text)
        checkBox.clicked.connect(self.updatePreview)
        return checkBox

    def createRadioButton(self, text):
        button = QRadioButton(text)
        button.clicked.connect(self.updatePreview)
        return button


class PreviewWindow(QWidget):
    def __init__(self):
        super(PreviewWindow, self).__init__()

        self.textEdit = QTextEdit()
        self.textEdit.setReadOnly(True)
        self.textEdit.setLineWrapMode(QTextEdit.NoWrap)
        self.closeButton = QPushButton("Close")
        self.closeButton.clicked.connect(self.close)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.textEdit)
        self.layout.addWidget(self.closeButton)
        self.setLayout(self.layout)
        self.setWindowTitle("Preview")

    def setWindowFlags(self, flags):
        super(PreviewWindow, self).setWindowFlags(flags)
        text = ""
        type = flags & Qt.WindowType_Mask
        if type == Qt.Window:
            text = "Qt.Window"
        elif type == Qt.Dialog:
            text = "Qt.Dialog"
        elif type == Qt.Sheet:
            text = "Qt.Sheet"
        elif type == Qt.Drawer:
            text = "Qt.Drawer"
        elif type == Qt.Popup:
            text = "Qt.Popup"
        elif type == Qt.Tool:
            text = "Qt.Tool"
        elif type == Qt.ToolTip:
            text = "Qt.ToolTip"
        elif type == Qt.SplashScreen:
            text = "Qt.SplashScreen"
        if flags & Qt.MSWindowsFixedSizeDialogHint:
            text += "\n| Qt.MSWindowsFixedSizeDialogHint"
        if flags & Qt.X11BypassWindowManagerHint:
            text += "\n| Qt.X11BypassWindowManagerHint"
        if flags & Qt.FramelessWindowHint:
            text += "\n| Qt.FramelessWindowHint"
        if flags & Qt.NoDropShadowWindowHint:
            text += "\n| Qt.NoDropShadowWindowHint"
        if flags & Qt.WindowTitleHint:
            text += "\n| Qt.WindowTitleHint"
        if flags & Qt.WindowSystemMenuHint:
            text += "\n| Qt.WindowSystemMenuHint"
        if flags & Qt.WindowMinimizeButtonHint:
            text += "\n| Qt.WindowMinimizeButtonHint"
        if flags & Qt.WindowMaximizeButtonHint:
            text += "\n| Qt.WindowMaximizeButtonHint"
        if flags & Qt.WindowCloseButtonHint:
            text += "\n| Qt.WindowCloseButtonHint"
        if flags & Qt.WindowContextHelpButtonHint:
            text += "\n| Qt.WindowContextHelpButtonHint"
        if flags & Qt.WindowShadeButtonHint:
            text += "\n| Qt.WindowShadeButtonHint"
        if flags & Qt.WindowStaysOnTopHint:
            text += "\n| Qt.WindowStaysOnTopHint"
        if flags & Qt.WindowStaysOnBottomHint:
            text += "\n| Qt.WindowStaysOnBottomHint"
        if flags & Qt.CustomizeWindowHint:
            text += "\n| Qt.CustomizeWindowHint"
        self.textEdit.setPlainText(text)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    controller = ControllerWindow()
    controller.show()
    sys.exit(app.exec())

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接