gpt4 book ai didi

python - QFileDialog opens in new window while adding it to QHBoxLayout

转载 作者:太空宇宙 更新时间:2023-11-04 05:00:48 25 4
gpt4 key购买 nike

我的问题是,当我将 QFileDialog 添加到 QVBoxLayout 时,它会在新窗口中打开。下面是产生我的问题的代码。

from PyQt5.QtWidgets import QVBoxLayout, QFileDialog, QPushButton, QWidget


class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("My own MainWindow")

self.fileDialog = QFileDialog()

self.confirmAction = QPushButton("Press me", self)

mainLayout = QVBoxLayout()

mainLayout.addWidget(self.fileDialog)
mainLayout.addWidget(self.confirmAction)
self.setLayout(mainLayout)

最佳答案

根据docs :

Window flags are a combination of a type (e.g. Qt::Dialog) and zero ormore hints to the window system (e.g. Qt::FramelessWindowHint).

If the widget had type Qt::Widget or Qt::SubWindow and becomes awindow (Qt::Window, Qt::Dialog, etc.), it is put at position (0, 0) onthe desktop. If the widget is a window and becomes a Qt::Widget orQt::SubWindow, it is put at position (0, 0) relative to its parentwidget.

所以这些标志用于改变小部件的行为,例如将其转换为窗口、对话框、工具提示等。

docs给出以下列表:

Qt::Widget: This is the defaulttype for QWidget. Widgets of this type are child widgets if they havea parent, and independent windows if they have no parent. See alsoQt::Window and Qt::SubWindow.

Qt::Window: Indicates that thewidget is a window, usually with a window system frame and a titlebar, irrespective of whether the widget has a parent or not. Note thatit is not possible to unset this flag if the widget does not have aparent.

Qt::Dialog :Window Indicates that the widget is awindow that should be decorated as a dialog (i.e., typically nomaximize or minimize buttons in the title bar). This is the defaulttype for QDialog. If you want to use it as a modal dialog, it shouldbe launched from another window, or have a parent and used with theQWidget::windowModality property. If you make it modal, the dialogwill prevent other top-level windows in the application from gettingany input. We refer to a top-level window that has a parent as asecondary window.

Qt::Sheet: Window Indicates that thewindow is a Macintosh sheet. Since using a sheet implies windowmodality, the recommended way is to use QWidget::setWindowModality(),or QDialog::open(), instead.

Qt::Drawer: Window Indicatesthat the widget is a Macintosh drawer.

Qt::Popup : Window Indicates that the widget is a pop-up top-level window, i.e.that it is modal, but has a window system frame appropriate for pop-upmenus.

Qt::Tool: Window Indicates that the widget is atool window. A tool window is often a small window with a smaller thanusual title bar and decoration, typically used for collections of toolbuttons. If there is a parent, the tool window will always be kept ontop of it. If there isn't a parent, you may consider usingQt::WindowStaysOnTopHint as well. If the window system supports it, atool window can be decorated with a somewhat lighter frame. It canalso be combined with Qt::FramelessWindowHint.

On Mac OS X, tool windows correspond to the Floating class of windows.This means that the window lives on a level above normal windows; itimpossible to put a normal window on top of it. By default, toolwindows will disappear when the application is inactive. This can becontrolled by the Qt::WA_MacAlwaysShowToolWindow attribute.

Qt::ToolTip:Window Indicates that the widget is atooltip. This is used internally to implement tooltips.

Qt::SplashScreen: Window Indicates that the window is asplash screen. This is the default type for QSplashScreen.

Qt::Desktop:Window Indicates that this widget is thedesktop. This is the type for QDesktopWidget.

Qt::SubWindow: Indicates that this widget is a sub-window,such as a QMdiSubWindow widget.

在您的情况下,我们必须将 Qt::Dialog 的行为更改为 Qt::Widget,在下面的代码中我展示了执行此操作的代码:

class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("My own MainWindow")

self.fileDialog = QFileDialog(self)
self.fileDialog.setOption(QFileDialog.DontUseNativeDialog)
self.fileDialog.setWindowFlags(Qt.Widget)

self.confirmAction = QPushButton("Press me", self)

mainLayout = QVBoxLayout()

mainLayout.addWidget(self.fileDialog)
mainLayout.addWidget(self.confirmAction)
self.setLayout(mainLayout)

截图:

enter image description here

关于python - QFileDialog opens in new window while adding it to QHBoxLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com