2

My code uses PyQt to open up a folder select dialog. Once a folder is selected it is minimized. I'd like for the dialog to pop up in front of any other windows. I haven't been able to find a solution yet. Any suggestions?

from sys import executable, argv
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication

def gui_fname(directory=''):
    file = check_output([executable, __file__, directory])
    return file.strip()

if __name__ == "__main__":
    directory = argv[1]
    app = QApplication([directory])
    folderpath = QFileDialog.getExistingDirectory(None, "Select folder")
rNOde
  • 59
  • 5
  • Please provide more specifics about your situation. The code works exactly as you described, it opens the file dialog in front of other windows. What is your issue? – peremeykin Mar 07 '18 at 13:04
  • Sorry, the problem is that the file dialog window is automatically minimized. I have to manually click on the taskbar to bring it up. How do I get it to pop to the front of other apps on my screen instead? – rNOde Mar 07 '18 at 14:46

1 Answers1

0

I think your problem comes from the "None" in the following function. folderpath = QFileDialog.getExistingDirectory(None, "Select folder")

The dialog modality cannot be set because it has no parent. Usually, instead of None we have self.

EDIT: Of cource app is not inheriting from QWidget. Sorry about that.

use this instead. I tested it an it work:

import sys
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication, QWidget

def gui_fname(directory=''):
    file = check_output([executable, __file__, directory])
    return file.strip()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    wid = QWidget()
    folderpath = QFileDialog.getExistingDirectory(wid, "Select folder")
    sys.exit(app.exec_())
PBareil
  • 157
  • 6
  • app is not a QWidget so it can not be used, in that post it indicates that the new window will always be in front of the other window, but not on the screen. – eyllanesc Mar 07 '18 at 16:44
  • You were right about QApplication. My mistake, but I think his application must be way bigger than the code we see. The point is to use something else than None. I made my answer a little bit more precise now. – PBareil Mar 07 '18 at 20:20