| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 5 | |
Here is what I want: widget with "<-" and "->" buttons to increase and decrease its size, and integrate it in layout in so way that when I press "<-" or "->" only size of this widget will be changed. In attachment there is small example to demonstrate problem: there are mywidget and two buttons on window, two buttons belong to QVBoxLayout QVBoxLayout and mywidget belong to QHBoxLayout, when I change size of "mywidget" via setMaximumSize resize updateGeometry size of "mywidget" is changed, but sometimes also changed size of QVBoxLayout with buttons, I try to fix it by seting window sizepolicy to Minimum and buttons size policy to Fixed, but when I increase size of "mywidget" and then decrease, window size is not decreased. Any suggestions how to fix this?
Attachment:
Attachment:
qt-fixed-size.zip
Description: Zip archive
Message 2 in thread
Looks like I can not sent zip files to list,
here is code:
$cat mywidget.hpp
#ifndef __MY_WIDGET_HPP_
#define __MY_WIDGET_HPP_
#include <QWidget>
class QToolButton;
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
QSize sizeHint() const;
QSize minimumSizeHint() const;
protected:
void resizeEvent(QResizeEvent *event);
private:
QToolButton *inc_, *dec_;
static const int sizes_[];
const int *curSize_;
int preferedWidth() const;
void setupButtons();
private slots:
void increase();
void decrease();
};
#endif//!__MY_WIDGET_HPP_
$cat mywidget.cpp
#include <QToolButton>
#include <QResizeEvent>
#include "mywidget.hpp"
const int MyWidget::sizes_[] = { 20, 40, 80, 0 };
MyWidget::MyWidget(QWidget *parent): QWidget(parent)
{
curSize_ = &sizes_[0];
inc_ = new QToolButton(this);
inc_->setArrowType(Qt::RightArrow);
inc_->setFocusPolicy(Qt::NoFocus);
dec_ = new QToolButton(this);
dec_->setArrowType(Qt::LeftArrow);
dec_->setFocusPolicy(Qt::NoFocus);
resize(preferedWidth(), 100);
setupButtons();
connect(inc_, SIGNAL(released()), this, SLOT(increase()));
connect(dec_, SIGNAL(released()), this, SLOT(decrease()));
}
void MyWidget::setupButtons()
{
static const int RESIZE_BTN_W = 15;
static const int RESIZE_BTN_H = 15;
dec_->setGeometry(width() - 2 * RESIZE_BTN_W, 0,
RESIZE_BTN_W, RESIZE_BTN_H);
inc_->setGeometry(width() - RESIZE_BTN_W, 0,
RESIZE_BTN_W, RESIZE_BTN_H);
}
QSize MyWidget::sizeHint() const
{
return QSize(preferedWidth(), height());
}
QSize MyWidget::minimumSizeHint() const
{
return this->sizeHint();
}
int MyWidget::preferedWidth() const
{
return 5 * *curSize_;
}
void MyWidget::increase()
{
if (*(curSize_ + 1)) {
++curSize_;
setMaximumSize(preferedWidth(), height());
resize(preferedWidth(), height());
updateGeometry();
}
}
void MyWidget::decrease()
{
if (curSize_ != &sizes_[0]) {
--curSize_;
setMaximumSize(preferedWidth(), height());
resize(preferedWidth(), height());
updateGeometry();
}
}
void MyWidget::resizeEvent(QResizeEvent *event)
{
setupButtons();
event->accept();
}
$cat mainwin.cpp
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include "mainwin.hpp"
#include "mywidget.hpp"
MainWin::MainWin()
{
QHBoxLayout *mlayout = new QHBoxLayout;
setLayout(mlayout);
mywidget_ = new MyWidget;
mlayout->addWidget(mywidget_);
QVBoxLayout *btnsLayout = new QVBoxLayout;
mlayout->addLayout(btnsLayout);
QPushButton *start = new QPushButton;
start->setText("Start");
btnsLayout->addWidget(start);
QPushButton *stop = new QPushButton;
stop->setText("Stop");
btnsLayout->addWidget(stop);
#if 1
start->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
stop->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
#endif
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
}
On 1/28/07, Tomasz Kvarsin <kvarsin@xxxxxxxxx> wrote:
> Here is what I want:
> widget with "<-" and "->" buttons to increase and decrease its size,
> and integrate it in layout in so way that when I press "<-" or "->"
> only size of this widget will be changed.
>
> In attachment there is small example to demonstrate problem:
> there are mywidget and two buttons on window,
> two buttons belong to QVBoxLayout
> QVBoxLayout and mywidget belong to QHBoxLayout,
>
> when I change size of "mywidget" via
> setMaximumSize
> resize
> updateGeometry
>
> size of "mywidget" is changed,
> but sometimes also changed size of QVBoxLayout with buttons,
> I try to fix it by seting window sizepolicy to Minimum
> and buttons size policy to Fixed,
> but when I increase size of "mywidget" and then decrease,
> window size is not decreased.
>
> Any suggestions how to fix this?
>
>
>
--
[ signature omitted ]