widgets resizeevent functions examples c++ qt qtwidgets

c++ - resizeevent - qwidget functions



¿Implementando manejadores de tamaño en QRubberband? ¿Es relevante QSizeGrip? (2)

Me gustaría que el usuario de la instancia de QRubberband . He visto esta pregunta aquí, pero no hay una solución.

El caso de uso es que el usuario puede arrastrar una marquesina de selección sobre una foto y luego hacer ajustes finos arrastrando los márgenes de QRubberband para cambiar la geometría, o reubicar la geometría existente arrastrando la selección. He implementado el reposicionamiento pero me pregunto sobre los controladores de cambio de tamaño para cambiar la geometría del QRubberband.

¿Es posible usar QSizeGrip aquí? Salvo eso, ¿existe una forma estándar de verificar eventos tipo mouse dentro del margen dentro de Qt o de alguna otra manera para implementar esto? Esto es para una aplicación de investigación que no necesita ni garantiza mucho esfuerzo en esta función, pero sería bueno tenerla.


Si es posible. Aquí está la implementación:

Encabezamiento:

class Resizable_rubber_band : public QWidget { public: Resizable_rubber_band(QWidget* parent = 0); private: QRubberBand* rubberband; void resizeEvent(QResizeEvent *); };

Fuente:

Resizable_rubber_band::Resizable_rubber_band(QWidget *parent) : QWidget(parent) { //tell QSizeGrip to resize this widget instead of top-level window setWindowFlags(Qt::SubWindow); QHBoxLayout* layout = new QHBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); QSizeGrip* grip1 = new QSizeGrip(this); QSizeGrip* grip2 = new QSizeGrip(this); layout->addWidget(grip1, 0, Qt::AlignLeft | Qt::AlignTop); layout->addWidget(grip2, 0, Qt::AlignRight | Qt::AlignBottom); rubberband = new QRubberBand(QRubberBand::Rectangle, this); rubberband->move(0, 0); rubberband->show(); show(); } void Resizable_rubber_band::resizeEvent(QResizeEvent *) { rubberband->resize(size()); }

Uso: ( ui->label es la etiqueta utilizada para mostrar la imagen que se va a recortar)

Resizable_rubber_band* band = new Resizable_rubber_band(ui->label); band->move(100, 100); band->resize(50, 50); band->setMinimumSize(30, 30);


En caso de que alguien más vaya a Google con la intención de usar esto con PyQt, aquí está el código Python equivalente al ejemplo de C ++ de @ pavel-strakhov y puedo confirmar que funciona para mí en * buntu Linux 14.04 LTS con el sistema proporcionado Python 3.4 y PyQt 5.2.1.

(Con la advertencia de que mi tema de widgets elegido no sabe cómo girar el QSizeGrip en la esquina superior izquierda, por lo que ambas esquinas tienen agarres de aspecto idéntico).

Fuente:

class ResizableRubberBand(QWidget): """Wrapper to make QRubberBand mouse-resizable using QSizeGrip Source: http://.com/a/19067132/435253 """ def __init__(self, parent=None): super(ResizableRubberBand, self).__init__(parent) self.setWindowFlags(Qt.SubWindow) self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.grip1 = QSizeGrip(self) self.grip2 = QSizeGrip(self) self.layout.addWidget(self.grip1, 0, Qt.AlignLeft | Qt.AlignTop) self.layout.addWidget(self.grip2, 0, Qt.AlignRight | Qt.AlignBottom) self.rubberband = QRubberBand(QRubberBand.Rectangle, self) self.rubberband.move(0, 0) self.rubberband.show() self.show() def resizeEvent(self, event): self.rubberband.resize(self.size())

Uso:

self.band = ResizableRubberBand(ui.label) self.band.move(100, 100) self.band.resize(50, 50) self.band.setMinimumSize(30, 30)