QDockWidget 類

QDockWidget 類提供可以停放的小部件在 QMainWindow 或浮動在桌麵上作為頂層窗口。 更多...

頭: #include <QDockWidget>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
繼承: QWidget

公共類型

enum DockWidgetFeature { DockWidgetClosable, DockWidgetMovable, DockWidgetFloatable, DockWidgetVerticalTitleBar, NoDockWidgetFeatures }
flags DockWidgetFeatures

特性

公共函數

QDockWidget (QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
QDockWidget (const QString & title , QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
virtual ~QDockWidget ()
Qt::DockWidgetAreas allowedAreas () const
Qt::DockWidgetArea dockLocation () const
QDockWidget::DockWidgetFeatures features () const
bool isAreaAllowed (Qt::DockWidgetArea area ) const
bool isFloating () const
void setAllowedAreas (Qt::DockWidgetAreas areas )
(從 6.9 起) void setDockLocation (Qt::DockWidgetArea area )
void setFeatures (QDockWidget::DockWidgetFeatures features )
void setFloating (bool floating )
void setTitleBarWidget (QWidget * widget )
void setWidget (QWidget * widget )
QWidget * titleBarWidget () const
QAction * toggleViewAction () const
QWidget * widget () const

信號

void allowedAreasChanged (Qt::DockWidgetAreas allowedAreas )
void dockLocationChanged (Qt::DockWidgetArea area )
void featuresChanged (QDockWidget::DockWidgetFeatures features )
void topLevelChanged (bool topLevel )
void visibilityChanged (bool visible )

保護函數

virtual void initStyleOption (QStyleOptionDockWidget * option ) const

重實現保護函數

virtual void changeEvent (QEvent * event ) override
virtual void closeEvent (QCloseEvent * event ) override
virtual bool event (QEvent * event ) override
virtual void paintEvent (QPaintEvent * event ) override

詳細描述

QDockWidget provides the concept of dock widgets, also know as tool palettes or utility windows. Dock windows are secondary windows placed in the dock widget area around the central widget QMainWindow .

Diagram of dock widget within toolbars and a container for widgets

Dock windows can be moved inside their current area, moved into new areas and floated (e.g., undocked) by the end-user. The QDockWidget API allows the programmer to restrict the dock widgets ability to move, float and close, as well as the areas in which they can be placed.

外觀

A QDockWidget consists of a title bar and the content area. The title bar displays the dock widgets 窗口標題 float button and a close button. Depending on the state of the QDockWidget, the float and close buttons may be either disabled or not shown at all.

The visual appearance of the title bar and buttons is dependent on the style in use.

A QDockWidget acts as a wrapper for its child widget, set with setWidget (). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidget itself, because they change depending on whether it is docked; a docked QDockWidget has no frame and a smaller title bar.

注意: On macOS, if the QDockWidget has a native window handle (for example, if winId () is called on it or the child widget), then due to a limitation it will not be possible to drag the dock widget when undocking. Starting the drag will undock the dock widget, but a second drag will be needed to move the dock widget itself.

另請參閱 QMainWindow .

成員類型文檔編製

enum QDockWidget:: DockWidgetFeature
flags QDockWidget:: DockWidgetFeatures

常量 描述
QDockWidget::DockWidgetClosable 0x01 The dock widget can be closed.
QDockWidget::DockWidgetMovable 0x02 The dock widget can be moved between docks by the user.
QDockWidget::DockWidgetFloatable 0x04 The dock widget can be detached from the main window, and floated as an independent window.
QDockWidget::DockWidgetVerticalTitleBar 0x08 The dock widget displays a vertical title bar on its left side. This can be used to increase the amount of vertical space in a QMainWindow .
QDockWidget::NoDockWidgetFeatures 0x00 The dock widget cannot be closed, moved, or floated.

DockWidgetFeatures 類型是 typedef 對於 QFlags <DockWidgetFeature>. It stores an OR combination of DockWidgetFeature values.

特性文檔編製

allowedAreas : Qt::DockWidgetAreas

areas where the dock widget may be placed

默認為 Qt::AllDockWidgetAreas .

訪問函數:

Qt::DockWidgetAreas allowedAreas () const
void setAllowedAreas (Qt::DockWidgetAreas areas )

通知程序信號:

void allowedAreasChanged (Qt::DockWidgetAreas allowedAreas )

另請參閱 Qt::DockWidgetArea .

[since 6.9] dockLocation : Qt::DockWidgetArea

This property holds the current dock location, or Qt::NoDockLocation if this dock widget is floating or has no mainwindow parent.

該特性在 Qt 6.9 引入。

訪問函數:

Qt::DockWidgetArea dockLocation () const
void setDockLocation (Qt::DockWidgetArea area )

通知程序信號:

void dockLocationChanged (Qt::DockWidgetArea area )

features : DockWidgetFeatures

This property holds whether the dock widget is movable, closable, and floatable

By default, this property is set to a combination of DockWidgetClosable , DockWidgetMovable and DockWidgetFloatable .

訪問函數:

QDockWidget::DockWidgetFeatures features () const
void setFeatures (QDockWidget::DockWidgetFeatures features )

通知程序信號:

void featuresChanged (QDockWidget::DockWidgetFeatures features )

另請參閱 DockWidgetFeature .

floating : bool

此特性保持停放 Widget 是否浮動

A floating dock widget is presented to the user as a single, independent window "on top" of its parent QMainWindow , instead of being docked either in the QMainWindow , or in a group of tabbed dock widgets.

Floating dock widgets can be individually positioned and resized, both programmatically or by mouse interaction.

默認情況下,此特性為 true .

當此特性改變時, topLevelChanged() 信號發射。

訪問函數:

bool isFloating () const
void setFloating (bool floating )

通知程序信號:

void topLevelChanged (bool topLevel )

另請參閱 isWindow () 和 topLevelChanged ().

windowTitle : QString

此特性保持停放 Widget 標題 (題注)

默認情況下,此特性包含空字符串。

訪問函數:

QString windowTitle () const
void setWindowTitle (const QString &)

通知程序信號:

void windowTitleChanged (const QString & title )

成員函數文檔編製

[explicit] QDockWidget:: QDockWidget ( QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())

構造 QDockWidget 采用父級 parent 和窗口標誌 flags . The dock widget will be placed in the left dock widget area.

[explicit] QDockWidget:: QDockWidget (const QString & title , QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())

構造 QDockWidget 采用父級 parent 和窗口標誌 flags . The dock widget will be placed in the left dock widget area.

窗口標題被設為 title . This title is used when the QDockWidget is docked and undocked. It is also used in the context menu provided by QMainWindow .

另請參閱 setWindowTitle ().

[virtual noexcept] QDockWidget:: ~QDockWidget ()

Destroys the dock widget.

[signal] void QDockWidget:: allowedAreasChanged ( Qt::DockWidgetAreas allowedAreas )

此信號發射,當 allowedAreas 特性改變。 allowedAreas parameter gives the new value of the property.

注意: 通知程序信號對於特性 allowedAreas .

[override virtual protected] void QDockWidget:: changeEvent ( QEvent * event )

重實現: QWidget::changeEvent (QEvent *event).

[override virtual protected] void QDockWidget:: closeEvent ( QCloseEvent * event )

重實現: QWidget::closeEvent (QCloseEvent *event).

[signal] void QDockWidget:: dockLocationChanged ( Qt::DockWidgetArea area )

This signal is emitted when the dock widget is moved to another dock area , or is moved to a different location in its current dock area. This happens when the dock widget is moved programmatically or is dragged to a new location by the user.

注意: 通知程序信號對於特性 dockLocation .

另請參閱 dockLocation () 和 setDockLocation ().

[override virtual protected] bool QDockWidget:: event ( QEvent * event )

重實現: QWidget::event (QEvent *event).

[signal] void QDockWidget:: featuresChanged ( QDockWidget::DockWidgetFeatures features )

此信號發射,當 features 特性改變。 features parameter gives the new value of the property.

注意: 通知程序信號對於特性 features .

[virtual protected] void QDockWidget:: initStyleOption ( QStyleOptionDockWidget * option ) const

初始化 option 采用值來自此 QDockWidget 。此方法對子類是有用的,當需要 QStyleOptionDockWidget ,但不希望自己填充所有信息。

另請參閱 QStyleOption::initFrom ().

bool QDockWidget:: isAreaAllowed ( Qt::DockWidgetArea area ) const

返迴 true if this dock widget can be placed in the given area ;否則返迴 false .

[override virtual protected] void QDockWidget:: paintEvent ( QPaintEvent * event )

重實現: QWidget::paintEvent (QPaintEvent *event).

[since 6.9] void QDockWidget:: setDockLocation ( Qt::DockWidgetArea area )

Assigns this dock widget to area . If docked at another dock location, it will move to area . If floating or part of floating tabs, the next call of setFloating (false) will dock it at area .

注意: setDockLocation(Qt::NoDockLocation) is equivalent to setFloating (true)。

注意: setter 函數對於特性 dockLocation .

該函數在 Qt 6.9 引入。

另請參閱 dockLocation () 和 dockLocationChanged ().

void QDockWidget:: setTitleBarWidget ( QWidget * widget )

Sets an arbitrary widget as the dock widget's title bar. If widget is nullptr , any custom title bar widget previously set on the dock widget is removed, but not deleted, and the default title bar will be used instead.

If a title bar widget is set, QDockWidget will not use native window decorations when it is floated.

Here are some tips for implementing custom title bars:

  • Mouse events that are not explicitly handled by the title bar widget must be ignored by calling QMouseEvent::ignore (). These events then propagate to the QDockWidget parent, which handles them in the usual manner, moving when the title bar is dragged, docking and undocking when it is double-clicked, etc.
  • DockWidgetVerticalTitleBar is set on QDockWidget , the title bar widget is repositioned accordingly. In resizeEvent (), the title bar should check what orientation it should assume:
    QDockWidget *dockWidget = qobject_cast<QDockWidget*>(parentWidget());
    if (dockWidget->features() & QDockWidget::DockWidgetVerticalTitleBar) {
        // I need to be vertical
    } else {
        // I need to be horizontal
    }
    							
  • The title bar widget must have a valid QWidget::sizeHint () 和 QWidget::minimumSizeHint (). These functions should take into account the current orientation of the title bar.
  • It is not possible to remove a title bar from a dock widget. However, a similar effect can be achieved by setting a default constructed QWidget as the title bar widget.

使用 qobject_cast () as shown above, the title bar widget has full access to its parent QDockWidget . Hence it can perform such operations as docking and hiding in response to user actions.

另請參閱 titleBarWidget () 和 DockWidgetVerticalTitleBar .

void QDockWidget:: setWidget ( QWidget * widget )

Sets the widget for the dock widget to widget .

If the dock widget is visible when widget 有添加,必須 show () 它明確。

Note that you must add the layout of the widget before you call this function; if not, the widget will not be visible.

另請參閱 widget ().

QWidget *QDockWidget:: titleBarWidget () const

Returns the custom title bar widget set on the QDockWidget ,或 nullptr if no custom title bar has been set.

另請參閱 setTitleBarWidget ().

QAction *QDockWidget:: toggleViewAction () const

Returns a checkable action that can be added to menus and toolbars so that the user can show or close this dock widget.

The action's text is set to the dock widget's window title.

The QAction object is owned by the QDockWidget . It will be automatically deleted when the QDockWidget 被銷毀。

注意: The action can not be used to programmatically show or hide the dock widget. Use the visible property for that.

另請參閱 QAction::text and QWidget::windowTitle .

[signal] void QDockWidget:: topLevelChanged ( bool topLevel )

此信號發射,當 floating 特性改變。 topLevel parameter is true if the dock widget is now floating; otherwise it is false.

注意: 通知程序信號對於特性 floating .

另請參閱 isWindow ().

[signal] void QDockWidget:: visibilityChanged ( bool visible )

This signal is emitted when the dock widget becomes visible (or invisible). This happens when the widget is hidden or shown, as well as when it is docked in a tabbed dock area and its tab becomes selected or unselected.

注意: The signal can differ from QWidget::isVisible (). This can be the case, if a dock widget is minimized or tabified and associated to a non-selected or inactive tab.

QWidget *QDockWidget:: widget () const

Returns the widget for the dock widget. This function returns zero if the widget has not been set.

另請參閱 setWidget ().