QRubberBand Class

QRubberBand 类提供了一个矩形或直线,可以指示选区或边界。更多

页首 #include <QRubberBand>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
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

保护函数

virtual 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

详细说明

橡皮筋通常用于显示新的边界区域(如QSplitter 或正在解锁的QDockWidget )。传统的做法是使用QPainter 和 XOR 来实现,但这种方法并不总能正常工作,因为渲染可能发生在橡皮筋下方的窗口中,但在橡皮筋被 "擦除 "之前。

只要需要在指定区域(或表示单行)周围渲染橡皮筋,就可以创建一个 QRubberBand,然后调用setGeometry(),move() 或resize() 来定位和调整大小。常见的模式是与鼠标事件结合使用。例如

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().
}

如果向 QRubberBand 的构造函数传递父对象,橡皮筋将仅在其父对象内部显示,而停留在其他子部件的顶部。如果没有传递父代,QRubberBand 将作为顶层部件运行。

调用show() 可使橡皮筋可见;当橡皮筋不是顶层时也是如此。隐藏或销毁部件会使橡皮筋消失。橡皮筋可以是RectangleLine (垂直或水平),具体取决于构建时给出的shape() 。

成员类型文档

enum QRubberBand::Shape

该枚举指定QRubberBand 应具有的形状。这是传递给样式系统的绘图提示,可由每个QStyle 解释。

常量说明
QRubberBand::Line0QRubberBand 可以表示垂直线或水平线。几何图形仍在rect() 中给出,在大多数样式中,线条将填充给出的几何图形。
QRubberBand::Rectangle1QRubberBand 可以表示矩形。某些样式会将其解释为填充矩形(通常是半透明矩形)或矩形轮廓。

成员函数文档

[explicit] QRubberBand::QRubberBand(QRubberBand::Shape s, QWidget *p = nullptr)

构造一个形状为s 的橡皮筋,其父节点为p

默认情况下,矩形橡皮筋(sRectangle )将使用遮罩,因此只能看到矩形的一小部分边框。某些样式(如本地 macOS)会改变这种情况,调用QWidget::setWindowOpacity() 来创建一个半透明的填充矩形选区。

[virtual noexcept] QRubberBand::~QRubberBand()

毁灭者

[override virtual protected] void QRubberBand::changeEvent(QEvent *e)

重实现:QWidget::changeEvent(QEvent *event).

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

重实现:QWidget::event(QEvent *event).

[virtual protected] void QRubberBand::initStyleOption(QStyleOptionRubberBand *option) const

使用此QRubberBand 中的值初始化option 。当子类需要QStyleOptionRubberBand ,但又不想自己填写所有信息时,该方法非常有用。

另请参阅 QStyleOption::initFrom()。

void QRubberBand::move(int x, int y)

将橡皮筋移至点 (x,y)。

另请参见 resize().

void QRubberBand::move(const QPoint &p)

这是一个重载函数。

将橡皮筋移动到p 点。

另请参见 resize().

[override virtual protected] void QRubberBand::moveEvent(QMoveEvent *)

重实现:QWidget::moveEvent(QMoveEvent *event).

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

重实现:QWidget::paintEvent(QPaintEvent *event).

void QRubberBand::resize(int width, int height)

调整橡皮筋的大小,使其宽度为width ,高度为height

另请参阅 move() 。

void QRubberBand::resize(const QSize &size)

这是一个重载函数。

调整橡皮筋的大小,使其新大小为size

另请参见 move().

[override virtual protected] void QRubberBand::resizeEvent(QResizeEvent *)

重实现:QWidget::resizeEvent(QResizeEvent *event).

void QRubberBand::setGeometry(const QRect &rect)

将橡皮筋的几何形状设置为rect ,并在其父窗口部件的坐标系中指定。

另请参阅 QWidget::geometry

void QRubberBand::setGeometry(int x, int y, int width, int height)

这是一个重载函数。

将橡皮筋的几何形状设置为左上角位于点 (x,y) 的矩形,其尺寸由widthheight 指定。几何图形是在父 widget 的坐标系中指定的。

QRubberBand::Shape QRubberBand::shape() const

返回橡皮筋的形状。形状只能在构建时设置。

[override virtual protected] void QRubberBand::showEvent(QShowEvent *e)

重实现:QWidget::showEvent(QShowEvent *event).

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.