The QRubberBand class provides a rectangle or line that can indicate a selection or a boundary. 更多...
| 頭: | #include <QRubberBand> |
| qmake: | QT += widgets |
| 繼承: | QWidget |
| enum | Shape { Line, Rectangle } |
| QRubberBand (QRubberBand::Shape s , QWidget * p = nullptr) | |
| virtual | ~QRubberBand () |
| void | move (int x , int y ) |
| void | move (const QPoint & p ) |
| void | resize (int width , int height ) |
| void | resize (const QSize & size ) |
| void | setGeometry (const QRect & rect ) |
| void | setGeometry (int x , int y , int width , int height ) |
| QRubberBand::Shape | shape () const |
| const QMetaObject | staticMetaObject |
| void | initStyleOption (QStyleOptionRubberBand * option ) const |
| virtual void | changeEvent (QEvent * e ) override |
| virtual bool | event (QEvent * e ) override |
| virtual void | moveEvent ( QMoveEvent * ) override |
| virtual void | paintEvent ( QPaintEvent * ) override |
| virtual void | resizeEvent ( QResizeEvent * ) override |
| virtual void | showEvent (QShowEvent * e ) override |
The QRubberBand class provides a rectangle or line that can indicate a selection or a boundary.
橡皮筋經常用於展示新的邊界區域 (如在 QSplitter 或 QDockWidget that is undocking). Historically this has been implemented using a QPainter and XOR, but this approach doesn't always work properly since rendering can happen in the window below the rubber band, but before the rubber band has been "erased".
You can create a QRubberBand whenever you need to render a rubber band around a given area (or to represent a single line), then call setGeometry (), move () 或 resize () to position and size it. A common pattern is to do this in conjunction with mouse events. For example:
void Widget::mousePressEvent(QMouseEvent *event) { origin = event->pos(); if (!rubberBand) rubberBand = new QRubberBand(QRubberBand::Rectangle, this); rubberBand->setGeometry(QRect(origin, QSize())); rubberBand->show(); } void Widget::mouseMoveEvent(QMouseEvent *event) { rubberBand->setGeometry(QRect(origin, event->pos()).normalized()); } void Widget::mouseReleaseEvent(QMouseEvent *event) { rubberBand->hide(); // determine selection, for example using QRect::intersects() // and QRect::contains(). }
If you pass a parent to QRubberBand 's constructor, the rubber band will display only inside its parent, but stays on top of other child widgets. If no parent is passed, QRubberBand will act as a top-level widget.
調用 show () to make the rubber band visible; also when the rubber band is not a top-level. Hiding or destroying the widget will make the rubber band disappear. The rubber band can be a Rectangle 或 Line (vertical or horizontal), depending on the shape () it was given when constructed.
This enum specifies what shape a QRubberBand should have. This is a drawing hint that is passed down to the style system, and can be interpreted by each QStyle .
| 常量 | 值 | 描述 |
|---|---|---|
QRubberBand::Line
|
0
|
A QRubberBand can represent a vertical or horizontal line. Geometry is still given in rect () and the line will fill the given geometry on most styles. |
QRubberBand::Rectangle
|
1
|
A QRubberBand can represent a rectangle. Some styles will interpret this as a filled (often semi-transparent) rectangle, or a rectangular outline. |
Constructs a rubber band of shape s ,采用父級 p .
By default a rectangular rubber band (
s
is
Rectangle
) will use a mask, so that a small border of the rectangle is all that is visible. Some styles (e.g., native
macOS
) will change this and call
QWidget::setWindowOpacity
() to make a semi-transparent filled selection rectangle.
[虛擬]
QRubberBand::
~QRubberBand
()
析構函數。
[override virtual protected]
void
QRubberBand::
changeEvent
(
QEvent
*
e
)
重實現自 QWidget::changeEvent ().
[override virtual protected]
bool
QRubberBand::
event
(
QEvent
*
e
)
重實現自 QWidget::event ().
[protected]
void
QRubberBand::
initStyleOption
(
QStyleOptionRubberBand
*
option
) const
初始化 option 采用值來自此 QRubberBand 。此方法對子類是有用的,當需要 QStyleOptionRubberBand ,但不希望自己填充所有信息。
另請參閱 QStyleOption::initFrom ().
將橡皮筋移到點 ( x , y ).
另請參閱 resize ().
這是重載函數。
將橡皮筋移到點 p .
另請參閱 resize ().
[override virtual protected]
void
QRubberBand::
moveEvent
(
QMoveEvent *
)
重實現自 QWidget::moveEvent ().
[override virtual protected]
void
QRubberBand::
paintEvent
(
QPaintEvent *
)
重實現自 QWidget::paintEvent ().
Resizes the rubberband so that its width is width , and its height is height .
另請參閱 move ().
這是重載函數。
Resizes the rubberband so that its new size is size .
另請參閱 move ().
[override virtual protected]
void
QRubberBand::
resizeEvent
(
QResizeEvent *
)
重實現自 QWidget::resizeEvent ().
Sets the geometry of the rubber band to rect , specified in the coordinate system of its parent widget.
另請參閱 QWidget::geometry .
這是重載函數。
Sets the geometry of the rubberband to the rectangle whose top-left corner lies at the point ( x , y ), and with dimensions specified by width and height . The geometry is specified in the parent widget's coordinate system.
Returns the shape of this rubber band. The shape can only be set upon construction.
[override virtual protected]
void
QRubberBand::
showEvent
(
QShowEvent
*
e
)
重實現自 QWidget::showEvent ().