如何使 PySide Window 弹出所有其他 windows

How to make a PySide Window pop up above all other windows

以下是一个 PyQt 示例,说明如何使用 'WindowStaysOnTopHint' 使对话框弹出,显示在所有其他对话框之上 windows:

#copied from: 
import sys
from PyQt4 import QtGui, QtCore

class mymainwindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)

app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()

'WindowStaysOnTopHint' 属性似乎是让 window 出现在所有其他 windows 之上的关键。但我无法弄清楚如何使用 'WindowStaysOnTopHint' 属性来弹出 "Folder Utility" window(在下面的脚本中),使其显示在所有其他 windows 之上。 我尝试在 self.initUI() 附近插入几个备选方案,如下所示,但没有任何东西使 "Folder Utility" window(在下面的脚本中)弹出所有其他 windows。

这是脚本:

import os
import sys 
# Based on ' but with many changes I made
from PySide import QtCore
from PySide import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        #self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)  
        #flags = QtCore.Qt.WindowFlags()# The line above prevented the 
           #   window from appearing at all
        #flags |= QtCore.Qt.WindowStaysOnTopHint # This line and the line 
           # above had no effect.

        self.initUI()
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)

    def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        # EditText Field
        labelNuDirName = QtGui.QLabel('New Folder Name:', self)
        labelNuDirName.move(15, 10)

        self.etNuDirName = QtGui.QLineEdit('', self)
        self.etNuDirName.resize(self.etNuDirName.sizeHint())

        self.etNuDirName.move(110, 7)

        # Folder Browser
        lbBroswer = QtGui.QLabel('Directory:', self)
        lbBroswer.move(15, 40)

        self.etBrowser = QtGui.QLineEdit('', self)
        self.etBrowser.resize(210,20)#width, height  https://srinikom.github.io/pyside-docs/PySide/QtGui/QWidget.html#PySide.QtGui.PySide.QtGui.QWidget.resize
        self.etBrowser.move(90, 37)
        self.etBrowser.setEnabled(0)
        # self.etBrowser.isReadOnly = 0

        btnBrowse = QtGui.QPushButton('...', self)
        btnBrowse.setToolTip('Select directory ...')
        btnBrowse.resize(30,20)
        btnBrowse.move(305, 37)
        btnBrowse.clicked.connect(self.selectDirectory)

        # Button UI
        btn = QtGui.QPushButton('Create Folder', self)
        btn.setToolTip('This creates the folders.')
        btn.resize(btn.sizeHint())
        btn.move(5, 60)       
        btn.clicked.connect(self.generateFolders)

         ###Folder Selected Show
#        label_NuDirSelected = QtGui.QLabel('', self)
#        self.etNuDirName.resize(210,20)#width, height  https://srinikom.github.io/pyside-docs/PySide/QtGui/QWidget.html#PySide.QtGui.PySide.QtGui.QWidget.resize
#        lbBroswer.move(5, 80)

        self.resize(350, 150)
        self.center()

        self.setWindowTitle('Folder Utility')    
        self.show()

    def center(self):

        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


    def selectDirectory(self): 

        dialog = QtGui.QFileDialog
        selected_directory = dialog.getExistingDirectory(None, 
                                                                 title, 
                                                                 startingDir, 
                                                                 QtGui.QFileDialog.ShowDirsOnly)    
        # Use the selected directory...
        self.etBrowser.setText(selected_directory)
        print 'selected_directory:', selected_directory
#        txt4__label_NuDirSelected = 'Directory you selected = "' + selected_directory + '"'
#        self.label_NuDirSelected.setText(txt4__label_NuDirSelected)


    def generateFolders(self):

        existing_directory = self.etBrowser.text()
        NuDirName = self.etNuDirName.text()

        filePath = str(existing_directory) +  os.sep + str(NuDirName) 

        if not os.path.exists(filePath):
            os.makedirs(filePath)

        if os.path.exists(filePath):
            print 'Successfully Created Folders!'

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我想我现在明白问题出在哪里了。每当您重置 window 标志时,Qt 也会在内部调用 setParent() - 然后隐藏 window。因此,您必须始终在调用 setWindowFlags().

之后显式调用 show()

self.initUI()(调用 show()last 放入 Example.__init__()。或者从 initIU 中删除 self.show(),然后在 main().

中添加行 ex.show()

我使用以下代码使 PyQt4 window 成为最顶层 window 并将其置于 [=14] 中的最前面(在所有其他 windows 之前) =] 10 64位环境:

self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

# The following is drawn from:     
# the following will remove minimized status 
# and restore window with keeping maximized/normal state
self.setWindowState(self.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)

# this will activate the window
self.activateWindow()