I have been given a qt widget containing some elements and layouts.
I am trying to add an empty layout to this main widget (My plan is to populate it with a widget in the future). I am expecting the main widget to resize automatically if the layout is empty. How can I do that?
I tried to play on the size policies of the widget but I did not get much results so far .
In the end the idea is to get the main widget (which is not contained in a layout by the way) to resize automatically if it contains an empty layout in itself
I think, you can look for adjustSize method of QWidget.
When you add or remove any internal widget in layout, you can make something like this:
QTimer::singleShot(0, this, SLOT(slotAdjustSize()));
...
void YourWidget::slotAdjustSize()
{
adjustSize();
}
Size of your widget will be fit to its contents.
Related
Below Code successfully auto-resizes QLineEdit field as needed (new width/height based on dynamic text). Now in order to make the resized QLineEdit fit the container widget, I do adjustsize() on the container widget.
Relevant elements in my code:
void theContainer::resize_to_content(QString text) {
{
// ...
lineedit.setFixedSize(calcualtedWidth, calculatedHigh);
theContainer::adjustSize();
}
the problem encountered is that even though both resizings of the QLineEdit and the parent widget work (to some extent), the limitation in this case is that the QlineEdit, after resizing, overlaps adjacent widgets!
It looks like that container widget -- when it adjusts -- seems to ignore other non manipulated children ...
Is there a systematic way to prevent such overlapping ?? not just a workaround I mean.
-- Widgets before adjustments
-- Widgets after text change and size adjustments:
I'm pretty sure using the classes Qt provides specifically for layout management will help.
See Qt documentation on layout
If it won't, I think you could resize the entire window to get more space instead of only the parent widget.
I would like to insert a QWidget inside a container QWidget (via its layout), but avoid parenting the inserted widget to the container widget.
There are good reasons behind this, the inserted widget is a toolbox tied to a document, and this toolbox is sent to a floating dock widget when the document becomes the active document.
Is this possible?
I would like to insert a QWidget inside a container QWidget (via its
layout), but avoid parenting the inserted widget to the container
widget.
There are good reasons behind this, the inserted widget is a toolbox
tied to a document, and this toolbox is sent to a floating dock widget
when the document becomes the active document.
Is this possible?
This simple answer is No - cannot avoid the widget being parented. But hold on. First, why the answer is No. Then how can we still do what you want.
Setting the layout for the widget does an implicit parent set.
Setting the widget for the layout does an implicit parent set.
QVBoxLayout* layout = new QVBoxLayout; // no parent yet
this->setLayout(layout); // it does layout->setParent(this);
QWidget* widget = new MyWidget; // no parent yet
layout->addWidget( widget ); // it does widget->setParent(this);
If layout is the layout manager on a different widget, setLayout()
will reparent the layout and make it the layout manager for this
widget.
With QLayout the reparenting is a bit more complicated because addWidget does call addItem:
void QLayout::addItem(QLayoutItem * item)
Implemented in subclasses to add an item. How it is added is specific
to each subclass.
This function is not usually called in application code. To add a
widget to a layout, use the addWidget() function; to add a child
layout, use the addLayout() function provided by the relevant QLayout
subclass.
Note: The ownership of item is transferred to the layout, and it's the
layout's responsibility to delete it.
See also addWidget(), QBoxLayout::addLayout(), and
QGridLayout::addLayout().
void QLayout::addWidget(QWidget * w)
Adds widget w to this layout in a manner specific to the layout. This
function uses addItem().
But we can still do something about such request:
There are good reasons behind this, the inserted widget is a toolbox
tied to a document, and this toolbox is sent to a floating dock widget
when the document becomes the active document.
Is this possible?
But that is of course possible. Say, by calling QWidget::setParent which is quite a common practice when we need to move the parent into new layout.
I have a central widget with several doc widgets surrounding it. When i hide it, the docs resizes to fill the space however, when i show the central widget back, it is show with its default size. I want to be able to resize and position it as it was before being hidden.
Before hiding do something like this:
QSize storedSize = mainwindow->getCentralWidget->size();
(assuming mainwindow is a * QMainWindow containing the central widget you are talking about)
When you want to resize it back to the old size:
mainwindow->getCentralWidget->resize(storedSize);
The child-parent hierarchy is the following: mainWindow -> centralWidget -> frame -> widget.
Widget is being resized during the application lifetime, however it is always set to a fixed size. I want the QMainWindow to resize based on that - to have a minimum size that is needed to display all the widgets.
To do that I currently have to do this.
widget->setFixedSize(x, y);
frame->setFixedSize(frame->sizeHint());
centralWidget->setFixedSize(centralWidget->sizeHint());
mainWindow->setFixedSize(mainWindow->sizeHint());
It doesn't work properly if I only resize the main window. All parents of widget need to be resized in order for this to work. Is there a more elegant way? Is it possible to make the main window call resize on all of it's children?
NOTE: All widgets except 'widget' have automatic layout management. So I find it strange that they don't resize themselves based on 'widget'.
It is the job of the layout to determine the size and position of the widgets. So if you explicitly resize a child widget which is inside a layout, you need to ensure that the new size is passed correctly through the layout system. This can be done by ensuring that the widget’s sizeHint() returns the new size. The value returned by the sizeHint() will be used when the layout calculates the new size. So after the sizeHint() has changed, simply call updateGeometry() [qt.nokia.com] that will notify the layout system that the widget has changed and may need to change geometry.
An alternative way to ensure that the parent widget is resized in response to its child being resized, is to call setFixedSize() on the child.
For more details check this...
http://qt-project.org/faq/answer/how_can_i_trigger_the_parent_widget_to_resize_when_its_child_widget_is_resi
You should let layout manage widgets size. As far as i am concerned there is 2 situations:
With layout when you are resizing a parent all children are resizing too regarding the size policy you provided to them. If you choose a fixed policy children will not be resized. So you have to resize them by hand... but it's quit weird.
When you are resizing a child like you are doing, parent is not automatically resized. You need to do it by hand. Fortunately there's a "magic function for that: AdjustSize. If you call it from QMainWindow all widget will be resized to their optimize size. It is possible (I can't test it here) that this does not work at runtime if size policy is set to fixed.
Hope that helps
I'm creating some graphic data displaying widget in Qt4 and I was tempted to use the QGraphicsScene for it, create QGraphicsItems for the data items etc.
However, I wanted to add some layer of controls (eg. scrollbars, zoom+other buttons - I want to make it in a similar style as eg. Google Maps, that is, the data would be displayed all over the widget, and the buttons would be shown atop of them) to the widget. So I thought it might be feasible to add them to the scene (perhaps as a child of a QGraphicsGroupItem that would be shown over the data). But I want them to move & resize when I resize the whole widget, so I should use a QGraphicsLayout for managing them. But at this point, I discovered things are pretty complicated.
The problem is that when using QGraphicsLayout, the following constraints hold:
Only a QGraphicsWidget can be managed by a layout
QGraphicsLayout can only be used to manage children of a QGraphicsWidget
Which means that I would have to create my controls as QGraphicsWidgets, add a top level QGraphicsWidget to the data widget, and manage the size of this top level widget myself.
So I want to ask:
Wouldn't a classic approach (ie. use plain old widgets for all controls, and use QGraphicsScene only for displaying the data) be more reasonable?
Is there any advantage in using QGraphicsScene in this case (performance or simplicity...)?
How should I use QGraphicsScene to exploit its strengths?
Since Qt 4.4 you can embed classic widgets in a QGraphicsScene by using QGraphicsProxyWidget :
QWidget *widget = new QWidget;
QGraphicsScene scene;
QGraphicsProxyWidget *proxy = scene.addWidget(widget);
If you think that QGraphicsScene (or whatever other widget you have) is appropriate for most of your display, use that. What we have done in the past for somewhat similar things is to make a custom widget that inherits (one way or another) from QWidget, and put the control widgets in a layout on top of that widget. This means that the whole widget is drawing whatever it is you want drawn, and the control widgets are on top of that, resizing as the whole widget is resized.
Alternatively, a couple of times we've had layouts that were just a bit too complicated for the layout widgets to easily handle. Rather than create a custom layout, we just positioned them with no layout, and moved them in code on the resize event. It works just as well.