luf
16th June 2009, 11:21
I'm trying to make a custom widget get it's size (fixed size) set depending on the size of a stacked widget.
I pick up the resize in resizeEvent of the main widget, and emit a signal to tell the other widget to resize. The problem is that when the custom widget has large fixed size, and I try to make the window smaller, it doesn't allow to scale/resize down properly. It works if I scale down several times, but that isn't a good solution.
I've figured out that I really should set the size before the resizeevent, as when that is called, the layout is already done. I just don't figure out what event it should be done in.
I tried implementing the following, as an small example.
custom.h:
class QCustomMainPage : QWidget
{
public:
QCustomMainPage(QWidget *parent)
protected:
void resizeEvent ( QResizeEvent *re );
signals:
void sizeChange(QSize size);
};
class QCustomResizeWidget : QFrame
{
public:
QCustomResizeWidget (QWidget *parent)
public slots:
void sizeChangeSlot(QSize size);
protected:
QSize sizeHint(void) const;
QSize minimumSizeHint(void) const;
QSize currentsize;
};
custom.cpp
QCustomResizeWidget::QCustomResizeWidget(QWidget *parent):QFrame (parent){
QLabel *label1 = new QLabel("label1");
QLabel *label2 = new QLabel("label2");
QVBoxLayout *clayout = new QVBoxLayout(this);
clayout->addWidget(label1);
clayout->addWidget(label2);
}
void QCustomResizeWidgetsize::ChangeSlot(QSize size){
currentsize = QSize(size.width()/10, size.width()/10); // keep it square
this->setFixedSize(currentsize);
}
QSize QCustomResizeWidgetsize::sizeHint(void) const{
return this->currentsize;
}
QSize minimumSizeHint(void) const{
// return this->currentsize;
return QSize(1,1); // allow widget to resize down to 1,1 (to get this partially working)
}
QCustomMainPage::QCustomMainPage(QWidget *parent) : QWidget(parent){
QCustomResizeWidget *customwidget1 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QCustomResizeWidget *customwidget2 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QCustomResizeWidget *customwidget3 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QCustomResizeWidget *customwidget4 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QGridLayout *grid = new QGridLayout(this);
grid->addWidget(customwidget1, 0,0);
grid->addWidget(customwidget1, 0,1);
grid->addWidget(customwidget1, 1,0);
grid->addWidget(customwidget1, 1,1);
}
QCustomMainPage::resizeEvent(QResizeEvent *re ){
emit sizeChange(re->size());
}
I pick up the resize in resizeEvent of the main widget, and emit a signal to tell the other widget to resize. The problem is that when the custom widget has large fixed size, and I try to make the window smaller, it doesn't allow to scale/resize down properly. It works if I scale down several times, but that isn't a good solution.
I've figured out that I really should set the size before the resizeevent, as when that is called, the layout is already done. I just don't figure out what event it should be done in.
I tried implementing the following, as an small example.
custom.h:
class QCustomMainPage : QWidget
{
public:
QCustomMainPage(QWidget *parent)
protected:
void resizeEvent ( QResizeEvent *re );
signals:
void sizeChange(QSize size);
};
class QCustomResizeWidget : QFrame
{
public:
QCustomResizeWidget (QWidget *parent)
public slots:
void sizeChangeSlot(QSize size);
protected:
QSize sizeHint(void) const;
QSize minimumSizeHint(void) const;
QSize currentsize;
};
custom.cpp
QCustomResizeWidget::QCustomResizeWidget(QWidget *parent):QFrame (parent){
QLabel *label1 = new QLabel("label1");
QLabel *label2 = new QLabel("label2");
QVBoxLayout *clayout = new QVBoxLayout(this);
clayout->addWidget(label1);
clayout->addWidget(label2);
}
void QCustomResizeWidgetsize::ChangeSlot(QSize size){
currentsize = QSize(size.width()/10, size.width()/10); // keep it square
this->setFixedSize(currentsize);
}
QSize QCustomResizeWidgetsize::sizeHint(void) const{
return this->currentsize;
}
QSize minimumSizeHint(void) const{
// return this->currentsize;
return QSize(1,1); // allow widget to resize down to 1,1 (to get this partially working)
}
QCustomMainPage::QCustomMainPage(QWidget *parent) : QWidget(parent){
QCustomResizeWidget *customwidget1 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QCustomResizeWidget *customwidget2 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QCustomResizeWidget *customwidget3 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QCustomResizeWidget *customwidget4 = new QCustomResizeWidget;
connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
QGridLayout *grid = new QGridLayout(this);
grid->addWidget(customwidget1, 0,0);
grid->addWidget(customwidget1, 0,1);
grid->addWidget(customwidget1, 1,0);
grid->addWidget(customwidget1, 1,1);
}
QCustomMainPage::resizeEvent(QResizeEvent *re ){
emit sizeChange(re->size());
}