Yes, it's possible. Here is the implementation:
Header:
class Resizable_rubber_band : public QWidget {
public:
Resizable_rubber_band(QWidget* parent = 0);
private:
QRubberBand* rubberband;
void resizeEvent(QResizeEvent *);
};
Source:
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());
}
Usage: (ui->label
is the label used for displaying the image to be cropped)
Resizable_rubber_band* band = new Resizable_rubber_band(ui->label);
band->move(100, 100);
band->resize(50, 50);
band->setMinimumSize(30, 30);