Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFileDialog always opens behind main window

Tags:

python

pyside2

I'm trying to open a file in my PySide2 application, but the file dialog always opens below the main window and appears as another application in the launcher. The application's name is "Portal".

I see other answers where the solution is to pass the main window as the first parameter to getOpenFileName(), but that doesn't work for me.

Here's a simple demonstration of the problem:

import sys
from PySide2.QtWidgets import QPushButton, QFileDialog, QApplication


class DemoButton(QPushButton):
    def __init__(self, text):
        super().__init__(text)
        self.clicked.connect(self.on_click)

    def on_click(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "Open a text file.",
            filter='Text file (*.txt)')
        print(file_name)


def main():
    app = QApplication(sys.argv)
    button = DemoButton("Hello World")
    button.show()
    app.exec_()
    sys.exit()


main()

I thought maybe the parent had to be a QMainWindow, so I tried that:

import sys

from PySide2 import QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        main_widget = QtWidgets.QWidget(self)
        self.setCentralWidget(main_widget)

        # layout initialize
        g_layout = QtWidgets.QVBoxLayout()
        layout = QtWidgets.QFormLayout()
        main_widget.setLayout(g_layout)

        # Add Widgets
        self.exec_btn = QtWidgets.QPushButton('Execute')
        self.exec_btn.clicked.connect(self.find_file)

        # global layout setting
        g_layout.addLayout(layout)
        g_layout.addWidget(self.exec_btn)

    def find_file(self):
        file_name, _ = QtWidgets.QFileDialog.getOpenFileName(
            self,
            "Open a text file.",
            filter='Text file (*.txt)')
        print(file_name)


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
    sys.exit()


main()

The file dialog behaved exactly the same.

I'm using PySide2 5.12.2, Python 3.6.7, and running on Ubuntu 18.04.

like image 429
Don Kirkby Avatar asked Dec 07 '19 06:12

Don Kirkby


Video Answer


1 Answers

Thanks to ekhumoro's comment, I learned that I can tell PySide2 not to use the native file dialog.

import sys
from PySide2.QtWidgets import QPushButton, QFileDialog, QApplication


class DemoButton(QPushButton):
    def __init__(self, text):
        super().__init__(text)
        self.clicked.connect(self.on_click)

    def on_click(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "Open a text file.",
            filter='Text file (*.txt)',
            options=QFileDialog.DontUseNativeDialog)
        print(file_name)


def main():
    app = QApplication(sys.argv)
    button = DemoButton("Hello World")
    button.show()
    app.exec_()
    sys.exit()


main()

That fixes the behaviour by bringing the file dialog to the front, but I think the native file dialog looks better. Hopefully, there's another option that can make the native file dialog work properly.

like image 89
Don Kirkby Avatar answered Oct 06 '22 20:10

Don Kirkby