如何使PySide窗口弹出所有其他窗口

时间:2017-03-05 22:03:49

标签: qt modal-dialog pyside

以下是如何使用'WindowStaysOnTopHint'的PyQt示例,以便弹出对话框,将其显示在所有其他窗口之上:

#copied from: http://stackoverflow.com/questions/1925015/pyqt-always-on-top
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'属性似乎是让窗口弹出所有其他窗口的关键。但是我无法弄清楚如何使用'WindowStaysOnTopHint'属性来获取弹出的“Folder Utility”窗口(在下面的脚本中),显示在所有其他窗口之上。 我尝试在self.initUI()附近插入几个替代方案,如下所示,但没有任何内容使“Folder Utility”窗口(在下面的脚本中)弹出所有其他窗口。

这是剧本:

import os
import sys 
# Based on 'http://stackoverflow.com/questions/22363123/pyside-get-directory-selected', 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()

2 个答案:

答案 0 :(得分:1)

我想现在我知道问题所在。每当你重置窗口标志时,Qt也会在内部调用setParent() - 然后隐藏窗口。因此,您必须在致电show()后明确致电setWindowFlags()

self.initUI()中添加show()(最近调用Example.__init__() 。或者从self.show()移除initIU,然后在ex.show()中添加main()行。

答案 1 :(得分:0)

我使用以下代码将PyQt4窗口设置为最顶层窗口,并将其置于Windows 10 64位环境中的最前面(在所有其他窗口之前):

self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

# The following is drawn from:  https://stackoverflow.com/questions/12118939/how-to-make-a-pyqt4-window-jump-to-the-front   
# 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()