PDA

View Full Version : Resize widget force layout resizing



^NyAw^
10th February 2009, 12:48
Hi,

I have a Dialog that contain one Widget and the tipical OK_Cancel buttons. The widgets are in a vertical layout.

I want the dialog to be resized automatically when I resize the Widget . The Widget have to display an image and I want to resize it to a proportional size forcing the dialog to resize.

Is it posible or have I to change the dialog size manually?

The widget is a special one similar to OpenGL widget.

Thanks,

Cruz
10th February 2009, 12:55
I think it micht happen when you set the minimumSize of your widget. They layout will try to accomodate it and probably resize the dialog. Try and see and let me know. :)

talk2amulya
10th February 2009, 12:59
implement the resizeEvent function and do all relevant changes there, it shouldn't be difficult

^NyAw^
10th February 2009, 16:03
Hi,

Setting the minimum size of the widget don't work.

Reimplementing the sizeEvent it's not easy as you said, I think. How can I know all the widgets sizes including layout sizes?
Is there another way to do it? Or if I have to reimplement resizeEvent, how can I get all sizes?

Thanks,

jpn
10th February 2009, 16:40
See QxtLetterBoxWidget (http://doc.libqxt.org/latest/classQxtLetterBoxWidget.html).

^NyAw^
10th February 2009, 17:07
Hi,

Thanks JPN. I think that this is what I need.

Good job on this extension library. ;)

^NyAw^
10th February 2009, 17:13
Hi,

Not exactly waht I need. As I read from docs, "QxtLetterBoxWidget (http://doc.libqxt.org/latest/classQxtLetterBoxWidget.html) preserves the aspect ratio of its content widget". What I need is to resize the widget and also force the dialog to adjust the size to show the widgets on its new size.

Really can QxtLetterBoxWidget helps me to this job?

Thanks,

jpn
10th February 2009, 17:19
What I need is to resize the widget and also force the dialog to adjust the size to show the widgets on its new size.
You can make the dialog non-resizable:

dialog->layout()->setSizeConstraint(QLayout::SetFixedSize);
and it will follow the size hint of the content. You just need to implement sizeHint() for your content widget and call updateGeometry() whenever the size hint changes.


Really can QxtLetterBoxWidget helps me to this job?
Well, why do you care about the dialog size when the aspect ratio of the content is correct? :)

^NyAw^
10th February 2009, 17:30
Hi,



and it will follow the size hint of the content. You just need to implement sizeHint() for your content widget and call updateGeometry() whenever the size hint changes.


The widget not will resize itself. On myDialog constructor I need to change the size of myWidget:


myDialog::myDialog(...)
{
...
int iWidth = obj->getWidth();
int iHeight = obj->getHeight();
float dAspRat = (float)iWidth/(float)iHeight;

int iWidthWidget, iHeightWidget;
iWidthWidget = 640;
iHeightWidget = (640/dAspRat );

m_pqWidget->resize(iWidthWidget,iHeightWidget);
...

//Here I want to force "this"(dialog) to fit the widgets size.
}





Well, why do you care about the dialog size when the aspect ratio of the content is correct? :)


Because I want to force the Widget to be 640 width and Y width(that depends on the aspect ratio).

Thanks,

jpn
10th February 2009, 18:27
The widget not will resize itself.
I said you have to implement sizeHint(), not to call resize(). The latter has no effect together with QLayout::SetFixedSize. The layout will follow the size hint, strictly.

^NyAw^
10th February 2009, 18:44
Hi,



myLayout::myLayout(...)
{
...
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
...
}

QSize myWidget::sizeHint()
{
//Here I have to calculate the size -> 640xY
}
The "sizeHint" method of myWidget is never called.

I don't really understand "sizeHint" behaviour, maybe because I'm not english and don't understand it's function.

See the attached image. I have this dialog and I use the QWidget(that is difficult) to see(it is on top of the other widgets). I use this QWidget as the parent of my widget(because I have not created the QtDesigner plugin).

Thanks,

talk2amulya
10th February 2009, 18:55
u forgot to attach the "attached image" :)

jpn
10th February 2009, 21:02
Ok, here's an example dummy widget that changes its size hint periodically:


class Widget : public QWidget
{
public:
Widget(QWidget* parent = 0) : QWidget(parent), sh(320, 240) {
setStyleSheet("background: red"); // just to make it easier to see
startTimer(2000);
}
QSize sizeHint() const { return sh; } // notice "const"

protected:
void timerEvent(QTimerEvent* /*event*/) {
sh += QSize(40, 30);
if (sh.width() > 640)
sh = QSize(320, 240);
updateGeometry(); // inform that the size hint has changed
}

private:
QSize sh;
};

Now, a dummy dialog that contains the above widget and a dialog button box:


int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QDialog dialog;
QVBoxLayout* layout = new QVBoxLayout(&dialog);
layout->setSizeConstraint(QLayout::SetFixedSize); // make the layout follow size hint
layout->addWidget(new Widget);
layout->addWidget(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel));
dialog.setLayout(layout);
return dialog.exec();
}

This makes the whole dialog follow the size hint of the "red widget". Is this what you wanted?

^NyAw^
11th February 2009, 10:48
Hi,

I've been trying it but I'm not able to make it works. Why the Widget have a timer that informs that its sizeHint have changed? Can't it be done on "resizeEvent", so when I call resize of the Widget it informs that the sizeHint have changed? Althought, sizeHint method is never called

It's so dificult to obtain the size of the internal widgets and the "spacing" between them and then resize the dialog to a calculated size?

Thanks,

jpn
11th February 2009, 11:44
Why the Widget have a timer that informs that its sizeHint have changed?
I said it's a dummy widget that changes its size hint periodically. Just to demonstrate that the whole dialog automatically follows the size hint change.


Can't it be done on "resizeEvent", so when I call resize of the Widget it informs that the sizeHint have changed?
No, you can't resize widgets by hand when they are in layouts. Layouts own geometries of widgets that are installed in layouts.

The layout calls sizeHint() first, calculates suitable geometry and calls setGeometry(). So if you start changing the size hint in resizeEvent(), you're already one round late and the layout has to do a complete recalculation. I recommend reading the Layout Classes document to get a basic understanding how layouts work.


Althought, sizeHint method is never called
In your example snippet it's missing the "const" keyword, thus you are not reimplementing the virtual method defined in the base class. In C++, to reimplement a virtual function, the function signature must match exactly, including function return value, parameter types and function constness.


It's so dificult to obtain the size of the internal widgets and the "spacing" between them and then resize the dialog to a calculated size?
That's a clear sign that you're doing something at wrong level. Let the layout calculate those things, just inform the layout about proper size for your widget by implementing the sizeHint() method properly.

^NyAw^
11th February 2009, 12:04
Hi,

It works now! Now I calculate the expected "sizeHint" on myWidget depending on proportional value.

I was confused on "sizeHint" signature.
So,


QSize sizeHint() const;

is different to


const QSize sizeHint();


I used to use the second signature and don't know wich is the difference.

Thanks for your help.

jpn
11th February 2009, 12:23
I used to use the second signature and don't know wich is the difference.
The former is a const function meaning that it won't change the state of the object, the latter just returns a const value. More info, C++ FAQ Lite: Const correctness (http://www.parashift.com/c++-faq-lite/const-correctness.html).

talk2amulya
11th February 2009, 12:27
well, its very important to know the difference between the two.

const QSize sizeHint() means the function would return a const QSize, whereas

QSize sizeHint() const means the function would not modify any of the data members or are read-only functions. such functions can be called for a const and non const objects and such functions have const this pointers