简体   繁体   English

QFileDialog 总是在主窗口后面打开

[英]QFileDialog always opens behind main window

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.我试图在我的 PySide2 应用程序中打开一个文件,但文件对话框总是在主窗口下方打开,并在启动器中显示为另一个应用程序。 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.我看到其他答案,其中解决方案是将主窗口作为第一个参数传递给getOpenFileName() ,但这对我不起作用。

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:我想也许父母必须是QMainWindow ,所以我试过:

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.我正在使用 PySide2 5.12.2、Python 3.6.7,并在 Ubuntu 18.04 上运行。

Thanks to ekhumoro's comment, I learned that I can tell PySide2 not to use the native file dialog.感谢 ekhumoro 的评论,我了解到我可以告诉 PySide2 不要使用本机文件对话框。

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.希望还有另一个选项可以使本机文件对话框正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM