Resize widget force layout resizing
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,
Re: Resize widget force layout resizing
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. :)
Re: Resize widget force layout resizing
implement the resizeEvent function and do all relevant changes there, it shouldn't be difficult
Re: Resize widget force layout resizing
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,
Re: Resize widget force layout resizing
Re: Resize widget force layout resizing
Hi,
Thanks JPN. I think that this is what I need.
Good job on this extension library. ;)
Re: Resize widget force layout resizing
Hi,
Not exactly waht I need. As I read from docs, "QxtLetterBoxWidget 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,
Re: Resize widget force layout resizing
Quote:
Originally Posted by
^NyAw^
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:
Code:
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.
Quote:
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? :)
Re: Resize widget force layout resizing
Hi,
Quote:
Originally Posted by
jpn
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:
Code:
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.
}
Quote:
Originally Posted by
jpn
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,
Re: Resize widget force layout resizing
Quote:
Originally Posted by
^NyAw^
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.
1 Attachment(s)
Re: Resize widget force layout resizing
Hi,
Code:
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,
Re: Resize widget force layout resizing
u forgot to attach the "attached image" :)
Re: Resize widget force layout resizing
Ok, here's an example dummy widget that changes its size hint periodically:
Code:
{
public:
setStyleSheet("background: red"); // just to make it easier to see
startTimer(2000);
}
QSize sizeHint
() const { return sh;
} // notice "const"
protected:
if (sh.width() > 640)
updateGeometry(); // inform that the size hint has changed
}
private:
};
Now, a dummy dialog that contains the above widget and a dialog button box:
Code:
int main(int argc, char* argv[])
{
layout
->setSizeConstraint
(QLayout::SetFixedSize);
// make the layout follow size hint layout->addWidget(new Widget);
dialog.setLayout(layout);
return dialog.exec();
}
This makes the whole dialog follow the size hint of the "red widget". Is this what you wanted?
Re: Resize widget force layout resizing
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,
Re: Resize widget force layout resizing
Quote:
Originally Posted by
^NyAw^
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.
Quote:
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.
Quote:
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.
Quote:
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.
Re: Resize widget force layout resizing
Hi,
It works now! Now I calculate the expected "sizeHint" on myWidget depending on proportional value.
I was confused on "sizeHint" signature.
So,
is different to
I used to use the second signature and don't know wich is the difference.
Thanks for your help.
Re: Resize widget force layout resizing
Quote:
Originally Posted by
^NyAw^
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.
Re: Resize widget force layout resizing
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