2

The following is a PyQt example of how to use 'WindowStaysOnTopHint' so that the dialog pops up, showing itself above all other windows:

#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_()

The 'WindowStaysOnTopHint' attribute seems to be the key to getting a window to pop up above all other windows. But I cannot figure out how to use the 'WindowStaysOnTopHint' attribute to get the "Folder Utility" window (in the script below) to pop up, showing itself above all other windows. I tried inserting several alternatives near the self.initUI(), as shown below, but nothing made the "Folder Utility" window (in the script below) pop up above all other windows.

Here's the script:

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()
Marc B. Hankin
  • 771
  • 3
  • 15
  • 31
  • 1
    The static functions use a native dialog, which you cannot control. Use the `QFileDialog` constructor to [create a non-native dialog](https://doc.qt.io/qt-4.8/qfiledialog.html#details). – ekhumoro Mar 06 '17 at 00:31
  • Thank you for the referral to that very useful URL. I infer from your response that the Window in my long code example problem is a "static function" window, and that 'WindowStaysOnTopHint' does not to such a window. Do you know where I could find out how to make a "static function" window pop up above all other windows? And, where can I find info that talks about "static function" windows? And thank you again for your generous help. – Marc B. Hankin Mar 07 '17 at 16:47
  • As I said: there's no way to control a native (i.e. non-Qt) dialog. – ekhumoro Mar 07 '17 at 17:07
  • I think my question was badly written; Am I right in concluding that the Window produced by the Class Example I posted is not a Qt dialog? That's the window I want to pop up above all other windows, and (correct me if I am wrong) that window is a native Qt window. I'm sorry for being so ignorant. – Marc B. Hankin Mar 07 '17 at 18:31
  • Yes, the `Example` class is a Qt window. But it **does** show above all windows, which is why I thought you must be referring to the file-dialog itself. The only thing different to the first code example, is that it does not call `ex.show()`. – ekhumoro Mar 07 '17 at 21:32

2 Answers2

2

I think I now see where the problem is. Whenever you reset the window flags, Qt also internally calls setParent() - which then hides the window. So you must always explicitly call show() after calling setWindowFlags().

Put self.initUI() (which calls show()) last in Example.__init__(). Or remove self.show() from initIU, and add the line ex.show() in main().

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

I used the following code to make a PyQt4 window the topmost window and to bring it to the front (in front of all other windows) in a Windows 10 64 bit environment:

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()
Marc B. Hankin
  • 771
  • 3
  • 15
  • 31