Qt - change QWidget layout - qt

Let's consider we have a QWidget and a QLayout named general_layout that contains other widgets and layouts. general_layout is set as the QWidget layout like this:
setLayout(general_layout)
Now I should to change the content of QWidget. How can I do that? I have tried to delete and create a new layout for the QWidget and that new layout set as a layout of the QWidget, but could not complete my intentions successfully.
This is my code:
delete general_layout;
general_layout = new QHBoxLayout;
general_layout->addLayout(some_layout);
myQWidget->setLayout(general_layout);

The problem is that the widgets of a layout are not destroyed when deleting a layout. This results in all child widgets of myQWidget still being present, be it without a layout.
The solution is simple: add a
qDeleteAll(myQWidget->children());
after
delete general_layout;

Related

How can overlap QPushButton without applying layout on QGridLayout?

How to place independently QPushButton without being affected by QGridLayout using Qt Designer if possible?
You can always remove a widget from layout. But you have to take care of its geometry..
use removeWidget(QWidget *widget).
Refer
http://doc.qt.io/qt-5/qlayout.html#removeWidget

QT Flowlayout - issue with multiline labels

I have a small problem with QT layout.
I have a toolbox, and I want to populate it with some checkable buttons with a description. So I create a QWidget with a QGridLayout and put the QButton in the first cell and the QLabel in the second.
This is the most important part of the code (I removed dependancies from other irrelevant code in the app):
QWidget *createCellWidget()
{
QToolButton *button = new QToolButton(this);
button->setCheckable(true);
button->setMinimumSize(57,57);
button->setMaximumSize(57,57);
QWidget *widget = new QWidget(this);
QGridLayout *layout = new QGridLayout(widget);
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
QLabel *lbl = new QLabel("my very long caption");
lbl->setWordWrap(true);
lbl->setAlignment(Qt::AlignCenter);
layout->addWidget(lbl, 1, 0, Qt::AlignTop);
widget->setMaximumWidth(80);
widget->setMinimumWidth(80);
return widget;
}
then I create a QGridLayout and populate it with these controls:
QWidget *itemWidget_FlowControl = new QWidget(this);
QGridLayout *_flowControl_layout = new QGridLayout(itemWidget_FlowControl);
_flowControl_layout->addWidget(createCellWidget(), 0, 0);
This works well and produces this output:
this is a nice layout. Unluckily if I enlarge the window the controls do not "flow", so I tried to replace the QGridLayout with a flowlayout (here are the source files).
Now the behavior is much better. BUT...
this is what I get. The longer captions are laid out as if they were single-lined, so the text overlaps with the button.
What can I do to make it look like before, but keeping it as a "flow layout"? Or do you know if there is any alternative in QT 5.2?
Thank you
What's wrong?
You are on the right track of using flowlayout.
The only problem is the internal layouts (QGridLayout in your case) of your cell widgets also stretch in response to the resize events.
Solution
The solution is surprisingly simple:
Try to limit the stretch of the internal layout.
Within the factory function QWidget *createCellWidget():
[Option 1]
Add lbl->setMaximumWidth(60); to manually limit the stretch of label width. This makes the internal layout not stretch so "freely."
[Option 2]
Add layout->setSizeConstraint(QLayout::SetFixedSize); to restrain the internal layout stretch. By doing this, you may want to manually add some line break code (\n) to your "very long caption" in case the label width automatically decided by Qt doesn't suit your need.
Result
IMO the best approach would be use off style sheets (flow layout is Ok).
Simply use QButton but customize its view by altering its stye sheet.
Currently I have no time to write an example.

QT: two layouts add the same widget

In Qt: I create a widget-ui class, and I want to make the widget appear in two different layouts in two separate base widget(or window). So I want to:
widget_based_class* inside = new widget_based_class(base_widget1);
QHBoxLayout *lay1=new QHBoxLayout(base_widget1);
base_widget->setLayout(lay1);
lay1->addWidget(inside);
base_widget1.show();
-------------------------------
base_widget1.hide();
QHBoxLayout *lay2=new QHBoxLayout(base_widget2);
base_widget->setLayout(lay2);
lay2->addWidget(inside);
base_widget2.show();
How cound I achieve this? (My program is more complicated, and I didn't see the code work.)
Tank you.
A QWidget has only one parent widget and only one geometry (position and size) in that parent. Every call of QLayout::addWidget() will reparent that widget to the widget, the layout is installed on.
Your second call of setLayout won't work as expected, because you have to delete the the existing layout manager before setting the new one:
delete base_widget->layout();
base_widget->setLayout(lay2);
If base_widget hasn't already got a layout manager, the layout manager lay2 would simply be reparented.
If the widget will never be displayed twice on the screen, I don't see why you can't reparent it by addWidget/removeWidget.
In the OP, the parent widget/window is always hidden before the other one is shown. addWidget is called on the fly. We should also call removeWidget on the fly. It should be possible to move the widget around.
widget_based_class* inside = new widget_based_class(base_widget1);
QHBoxLayout *lay1=new QHBoxLayout(base_widget1);
base_widget->setLayout(lay1);
lay2->removeWidget(inside); // remove widget from other layout
lay1->addWidget(inside); // add widget to this layout
base_widget1.show();
-------------------------------
base_widget1.hide();
QHBoxLayout *lay2=new QHBoxLayout(base_widget2);
base_widget->setLayout(lay2);
lay1->removeWidget(inside); //remove widget from other layout
lay2->addWidget(inside); // add widget to this layout
base_widget2.show();
Make one widget and use a pointer in each layout?

Resizing a QDialog after adding components to a child widget

I'm a bit new to QT but have to work on existing code. Here's the case:
I have a class extending QDialog. the constructor sets a QGridLayout then adding three other widgets to it. One of the widgets is a QScrollArea containing a QGroupBox. this QGroupBox has a QVBoxLayout and there I'm adding a list of widgets at runtime. The size of the scroll area should grow until a given limit is reached before showing the scrollbars so that they are only used when the dialog would grow too high. I've found that the sizeHint of the outer layout doesn't update when the sizeHint of the scroll area updates. How can I refresh this, or is there a better way to resize the parent dialog?
What about using widgetResizable property of QScrollArea? It should try to resize view to avoid using scorllbars.

How does QWidget size when used as a window?

Several examples on trolltech use QWidget rather than a QMainWindow as the main window for simple Qt applications.
Now I need just a simple window with a single content widget (QGlWidget) that covers the entire client area.
However, when I show a QWidget window with a single content window it automatically resizes itself to be tiny.
If I create the QWidget parent window without a child It is a nice large default size.
I don't want to resort to using Layouts for a single child window :/
What I understand is that you use a QWidget to display your QGIWidget. Try calling the show method of your QGIWidget directly (if your QGIWidget class inherits QWidget), Qt will create the window decoration for you.
Otherwise if you really need your widget to be inside one another, and fit its size, you'll have to use a layout.
Either follow gregseth's advice or you can simply resize the widget yourself.
(though this way you'll loose nice auto-resizing which is provided by Qt when you use layouts)
In your case you can basically do something like:
QGlWidget* myWidget = new QGlWidget;
myWidget->resize(QApplication::desktopWidget()->availableGeometry().size());
// or maybe instead of resizing show it in fullscreen:
myWidget->showFullScreen();
(actually I don't remember if showFullScreen() will do resizing for you, maybe you'll need both resize+showFullScreen :))
Cheers :)
P.S. Using layout is actually an option. It's not expensive and it's flexible. All it gets: "layout = new QVBoxLayout(myWidget); layout->addWidget(myWidget);" and you're done :)
Not always.
I've found that a QMainWindow will not work as the parent widget when using the QVBoxLayout and QHBoxLayout to arrange child widgets.
If you create a QWidget and use that in place of the QMainWindow then the layouts will work correctly.
QWidget* centralWidget = new QWidget( MainWindow );
MainWindow->setCentralWidget( centralWidget );
If you use QtCreator and look at the code it creates you can see it creating a 'hidden' widget if you try to use the layouts directly at the top level.
It's not obvious, intuitive, or documented anywhere that I've found.

Resources