QRubberBand Class
La clase QRubberBand proporciona un rectángulo o una línea que puede indicar una selección o un límite. Más información...
| Encabezado: | #include <QRubberBand> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
| qmake: | QT += widgets |
| Heredado de: | QWidget |
Tipos públicos
| enum | Shape { Line, Rectangle } |
Funciones públicas
| 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 |
Funciones protegidas
| virtual void | initStyleOption(QStyleOptionRubberBand *option) const |
Funciones protegidas reimplementadas
| 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 |
Descripción detallada
A menudo se utiliza una banda elástica para mostrar una nueva zona delimitada (como en un « QSplitter » o un « QDockWidget » que se está desacoplando). Históricamente, esto se ha implementado utilizando un « QPainter » y una operación XOR, pero este enfoque no siempre funciona correctamente, ya que el renderizado puede producirse en la ventana situada debajo de la banda elástica, pero antes de que esta se haya «borrado».
Puedes crear un QRubberBand siempre que necesites renderizar una banda elástica alrededor de un área determinada (o para representar una sola línea) y, a continuación, llamar a setGeometry(), move() o resize() para posicionarla y ajustar su tamaño. Un patrón habitual es hacerlo en combinación con eventos del ratón. Por ejemplo:
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().
}Si pasas un elemento padre al constructor de QRubberBand, la banda elástica solo se mostrará dentro de su elemento padre, pero permanecerá por encima de otros widgets hijos. Si no se pasa ningún elemento padre, QRubberBand actuará como un widget de nivel superior.
Llama a ` show()` para hacer visible la banda elástica; esto también es necesario cuando la banda elástica no es de nivel superior. Ocultar o destruir el widget hará que la banda elástica desaparezca. La banda elástica puede ser un ` Rectangle ` o un ` Line ` (vertical u horizontal), dependiendo de la ` shape()` que se le haya pasado al crearla.
Documentación sobre los tipos de miembros
enum QRubberBand::Shape
Esta enumeración especifica la forma que debe tener un ` QRubberBand `. Se trata de una indicación de dibujo que se transmite al sistema de estilos y que puede ser interpretada por cada ` QStyle`.
| Constante | Valor | Descripción |
|---|---|---|
QRubberBand::Line | 0 | Un « QRubberBand » puede representar una línea vertical u horizontal. La geometría se sigue indicando en ` rect()` y, en la mayoría de los estilos, la línea rellenará la geometría especificada. |
QRubberBand::Rectangle | 1 | Un ` QRubberBand ` puede representar un rectángulo. Algunos estilos interpretarán esto como un rectángulo relleno (a menudo semitransparente) o como un contorno rectangular. |
Documentación de las funciones miembro
[explicit] QRubberBand::QRubberBand(QRubberBand::Shape s, QWidget *p = nullptr)
Crea una banda elástica con la forma s, cuyo elemento padre es p.
Por defecto, una banda elástica rectangular (s es Rectangle) utilizará una máscara, de modo que solo sea visible un pequeño borde del rectángulo. Algunos estilos (por ejemplo, el nativo de macOS) modificarán esto y llamarán a QWidget::setWindowOpacity() para crear un rectángulo de selección relleno y semitransparente.
[virtual noexcept] QRubberBand::~QRubberBand()
Destructor.
[override virtual protected] void QRubberBand::changeEvent(QEvent *e)
Reimplementa: ` QWidget::changeEvent(QEvent *event)`.
[override virtual protected] bool QRubberBand::event(QEvent *e)
Reimplementa: ` QWidget::event(QEvent *event)`.
[virtual protected] void QRubberBand::initStyleOption(QStyleOptionRubberBand *option) const
Inicializa ` option ` con los valores de esta página: QRubberBand. Este método resulta útil para las subclases cuando necesitan un ` QStyleOptionRubberBand`, pero no quieren rellenar toda la información ellas mismas.
Véase también QStyleOption::initFrom().
void QRubberBand::move(int x, int y)
Mueve la goma elástica hasta el punto (x, y).
Véase también resize().
void QRubberBand::move(const QPoint &p)
Mueve la goma elástica para que apunte a p.
Esta es una función sobrecargada.
Véase también resize().
[override virtual protected] void QRubberBand::moveEvent(QMoveEvent *)
Reimplementa: ` QWidget::moveEvent(QMoveEvent *event)`.
[override virtual protected] void QRubberBand::paintEvent(QPaintEvent *)
Reimplementa: QWidget::paintEvent(QPaintEvent *event).
void QRubberBand::resize(int width, int height)
Cambia el tamaño de la banda elástica para que su anchura sea width y su altura sea height.
Véase también move().
void QRubberBand::resize(const QSize &size)
Cambia el tamaño de la goma elástica para que su nuevo tamaño sea size.
Esta es una función sobrecargada.
Véase también move().
[override virtual protected] void QRubberBand::resizeEvent(QResizeEvent *)
Reimplementa: QWidget::resizeEvent(QResizeEvent *event).
void QRubberBand::setGeometry(const QRect &rect)
Establece la geometría de la banda elástica en rect, especificada en el sistema de coordenadas de su widget padre.
Véase también QWidget::geometry.
void QRubberBand::setGeometry(int x, int y, int width, int height)
Establece la geometría de la banda elástica como el rectángulo cuya esquina superior izquierda se encuentra en el punto (x, y), y cuyas dimensiones vienen especificadas por width y height. La geometría se especifica en el sistema de coordenadas del widget padre.
Se trata de una función sobrecargada.
QRubberBand::Shape QRubberBand::shape() const
Devuelve la forma de esta banda elástica. La forma solo se puede establecer en el momento de su creación.
[override virtual protected] void QRubberBand::showEvent(QShowEvent *e)
Reimplementa: ` QWidget::showEvent(QShowEvent *event)`.
© 2026 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.