<p>根据<a href="http://doc.qt.io/qt-4.8/qwidget.html#windowFlags-prop" rel="nofollow noreferrer">docs</a>:</p>
<blockquote>
<p>Window flags are a combination of a type (e.g. Qt::Dialog) and zero or
more hints to the window system (e.g. Qt::FramelessWindowHint).</p>
<p>If the widget had type Qt::Widget or Qt::SubWindow and becomes a
window (Qt::Window, Qt::Dialog, etc.), it is put at position (0, 0) on
the desktop. If the widget is a window and becomes a Qt::Widget or
Qt::SubWindow, it is put at position (0, 0) relative to its parent
widget.</p>
</blockquote>
<p>因此,这些标志用于改变小部件的行为,例如将其转换为窗口、对话框、工具提示等。在</p>
<p>在<a href="http://doc.qt.io/qt-4.8/qt.html#WindowType-enum" rel="nofollow noreferrer">docs</a>中给出了以下列表:</p>
<blockquote>
<p><strong>Qt::Widget</strong>: This is the default
type for QWidget. Widgets of this type are child widgets if they have
a parent, and independent windows if they have no parent. See also
Qt::Window and Qt::SubWindow. </p>
<p><strong>Qt::Window</strong>: Indicates that the
widget is a window, usually with a window system frame and a title
bar, irrespective of whether the widget has a parent or not. Note that
it is not possible to unset this flag if the widget does not have a
parent. </p>
<p><strong>Qt::Dialog</strong> :Window Indicates that the widget is a
window that should be decorated as a dialog (i.e., typically no
maximize or minimize buttons in the title bar). This is the default
type for QDialog. If you want to use it as a modal dialog, it should
be launched from another window, or have a parent and used with the
QWidget::windowModality property. If you make it modal, the dialog
will prevent other top-level windows in the application from getting
any input. We refer to a top-level window that has a parent as a
secondary window.</p>
<p><strong>Qt::Sheet</strong>: Window Indicates that the
window is a Macintosh sheet. Since using a sheet implies window
modality, the recommended way is to use QWidget::setWindowModality(),
or QDialog::open(), instead. </p>
<p><strong>Qt::Drawer</strong>: Window Indicates
that the widget is a Macintosh drawer. </p>
<p><strong>Qt::Popup</strong> : 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-up
menus.</p>
<p><strong>Qt::Tool</strong>: Window Indicates that the widget is a
tool window. A tool window is often a small window with a smaller than
usual title bar and decoration, typically used for collections of tool
buttons. If there is a parent, the tool window will always be kept on
top of it. If there isn't a parent, you may consider using
Qt::WindowStaysOnTopHint as well. If the window system supports it, a
tool window can be decorated with a somewhat lighter frame. It can
also be combined with Qt::FramelessWindowHint. </p>
<p>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; it
impossible to put a normal window on top of it. By default, tool
windows will disappear when the application is inactive. This can be
controlled by the Qt::WA_MacAlwaysShowToolWindow attribute.</p>
<p><strong>Qt::ToolTip</strong>:Window Indicates that the widget is a
tooltip. This is used internally to implement tooltips.</p>
<p><strong>Qt::SplashScreen</strong>: Window Indicates that the window is a
splash screen. This is the default type for QSplashScreen.</p>
<p><strong>Qt::Desktop</strong>:Window Indicates that this widget is the
desktop. This is the type for QDesktopWidget.</p>
<p><strong>Qt::SubWindow</strong>: Indicates that this widget is a sub-window,
such as a QMdiSubWindow widget.</p>
</blockquote>
<p>在您的例子中,我们必须将<code>Qt::Dialog</code>的行为更改为<code>Qt::Widget</code>,在下面的代码中,我展示了执行此操作的代码:</p>
<pre><code>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)
</code></pre>
<p>截图:</p>
<p><a href="https://i.stack.imgur.com/f6pl9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f6pl9.png" alt="enter image description here"/></a></p>