QWidget::layout() can't return QGridLayout? - c++

I have a QWidget that contains a QGridLayout, which in turn contains a handful of QPushButtons. These are all generated programmatically. Later in the code (separate from where the layout is defined), I need to be able to add more pushbuttons to specific row/column positions in the layout.
I tried using: widget->layout()->addWidget(button, row, col) to reference the layout and add the buttons. However, widget->layout() only returns a generic QLayout item, which does not allow me to specify row and column values. Is there any way to reference a QGridLayout from a specific widget, without having to know the layout by name? I'm using Qt 4.8 if it makes a difference.

You can always cast it to QGridLayout* by dynamic_cast:
auto gridLayout = dynamic_cast<QGridLayout*>(widget->layout());
If you're sure widget->layout() points to your QGridLayout you don't have to check and can use static_cast. Otherwise, check gridLayout against nullptr.

Related

Qt Layout relationship between QLayoutItem and QWidget

this isn't well clarified in the documentation, but what really happens when for instance, I call QBoxLayout::insertWidget. It allocates a QLayoutItem, somehow associates it with the widget and then adds the QLayoutItem to the layout? Why the need for such an indirection? I'm making a custom layout and want to be able to insert widgets at any index of that layout, however I am not fully aware of the mechanics.
Because layouts operate layout items, not widgets. QLayoutItem contains a list of it's own functions that are used to position the layoutitem inside the layout, and properly resize/align it.
Take a simple example: you have a vertical layout that is 300px wide. That means each layout item that you add will also be 300px wide. Now imagine adding a 50x50 widget into it. Since the layout has it's own geometry, sizeHing, and other stuff, you will be able to properly insert the widget (the layoutItem will stay 300px wide, and the widget will stay 50px wide, nothing will break), which would be hard/impossible if you operated widgets directly.

How to expand widgets with window resize?

I have a simple qt application with a QTabWidget inside the main window. I also have a few QPushButton(s) and QRadioButton(s).
What I want is that when I resize the window either manually or by maximizing/minimizing it should resize the containers in the same way.
In other words, what I want is equivalent of DockStyle.Fill in qt C++
How can I do that ?
In Qt you have to use Layouts:
The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that
they make good use of the available space.
In short, all components in a layout will be relocated to new places after the window, to which the layout belongs, is resized.
If you are using deisgner:
1. Click the empty space of a widget to select itself(or a main Window, I use just a base widget here for demonstration), and the layout option will be hightlighted:
2. Choose a desired layout
Here is what object monitor looks like after a QVBoxLayout is used:
If your widget doesn't use layout, it will look like this:
What we have done here is to make the base widget/mainWindow equip a main layout. You can see that the buttons are automatically aligned, when you resize the widget, those component will be relocated according to the layout:
Perhaps you will find it nettlesome of those expanding space, so the next move is to add a Spacer to the layout; so when layout is resized, only the spacer will stretch.
(Another option is to make your widgets expandable, see ** at the end of this post)
3. Besides, you can add a layout into another to create a nested layout
For example, first I choose A and B (by pressing Ctrl) and use QVBoxLayout. This additional layout is not base layout and hence highlighted by red rectangle.
Then I choose C and the layout which contains A & B, and use QHBoxLayout on them,
Finally I use another QVBoxLayout as my main layout on the base widget, just like what we did previously.
And the object monitor:
If you like the special feeling of hitting keyboard and always handcraft the code:
For the last example:
QWidget *Form = new QWidget;
QPushButton *pushButton_A = new QPushButton("A");
QPushButton *pushButton_B = new QPushButton("B");
QPushButton *pushButton_C = new QPushButton("C");
QVBoxLayout *verticalLayout = new QVBoxLayout;
QHBoxLayout *horizontalLayout = new QHBoxLayout;
QVBoxLayout *mainLayout = new QVBoxLayout;
verticalLayout->addWidget(pushButton_A);
verticalLayout->addWidget(pushButton_B);
horizontalLayout->addWidget(pushButton_C);
horizontalLayout->addLayout(verticalLayout);
mainLayout->addLayout(horizontalLayout);
Form->setLayout(mainLayout);
Form->show();
In your case
Here is an example of layout:
Notice that QMainWidget has a centralwidget as a base widget. Besides, each tab of QTabWidget has it's own base widget (tab and tab_2 in the picture) which adopts another base layout.
*Don't forget to add Spacer in layouts to shape them as you like.
** You can set size policy on each widget (QTabWidget, QPushButton etc) to make them horizontally/vertically expandable or fixed, this cooperates with the layout strategy. For example, in the very begin example if we set
button A to be vertically fixed, horizontally expanding
button B to be vertically expanding, horizontally expanding
button C to be vertically expanding, horizontally fixed
It will look like this when resizing:
you need to look into how to use layouts in your application
http://qt-project.org/doc/qt-4.8/layout.html
As a quick and simple first try, in the Designer you can right-click on the main window, and choose "layout" from the drop-down menu. Here you can pick the grid layout, for instance.

How to add a widget to a pre-existing QLayout?

I'm looking for a super simple example, and can't seem to find one. I have a MainWindow. When a button gets pressed I want to create a new window that gets opened up in the layout of MainWindow, to become a part of the mainwindow.
I have the code that sets up when a button is pushed to call this slot...when it gets called my QLabel shows up, but my QWidget does not
QWidget *test = new QWidget();
test->setGeometry(QRect(100,100,100,100));
layout->addWidget(test,0,0)
//Operation Mode
QLabel *operationalModeLabel1 = new QLabel("TEST");
layout->addWidget(operationalModeLabel1,2,1);
The reason for "lack of examples" is that you think of it wrong. What you describe is done all the time, by every single Qt example that uses layouts! I mean it. It doesn't matter when you add a widget to a layout. There's nothing magical about adding widgets "now" vs. adding them "later".
Just think of the title of the question: it makes no sense. All widgets must be added to layouts that already exist! By definition, no less. If there's no layout, how could you add a widget to it?
Your code is wrong, that's all. It's always pointless to set a geometry on a widget that is to be managed by a layout. As soon as you add it to the layout, the layout will change the geometry.
Since you're adding an empty widget into the layout, you most likely won't be able to see it. That's why the label shows up - it's not an empty widget.
If you want a widget that has a fixed size, to make it easier to notice, just set the fixed size on it. Even better, make it red so that it stands out.
QWidget * test = new QWidget();
test->setStyleSheet("QWidget { background-color: red }");
test->setFixedSize(100, 100);
layout->addWidget(test, 0, 0);

Remove Widget from QGridLayout in Qt? [duplicate]

This question already has answers here:
Removing widgets from QGridLayout
(2 answers)
Closed 9 years ago.
I have a very basic doubt regarding QGridLayout.
For adding a widget in QGridLayout we give QWidget * that should be added along with the row & column no (some other args as well).
Now for removing a widget there is no function to remove a widget according to row & column no i.e. something like this:
int row, column;
gridObj->remove(row, column);
I think QGridLayout must be maintaining a sort of QList to store references of Widgets & there positions. So why is there no function for removing widgets by position only?
It only has 1 remove function for which we need to specify the reference of QWidget object.
If this is a limitation somehow then is there a workaround for this problem?
Maintaining a QList by myself is a solution but it is pretty tedious.
Thank You
I might be mistaken here, but from skimming the documentation, try this:
Get the QLayoutItem at the position (QGridLayout::itemAtPosition(row, column)).
Use the QLayoutItem to get the widget pointer (QLayoutItem::widget()).
Use the widget pointer to find the index of the widget in the QGridLayout (QLayout::indexOf(widgetPointer)).
Use the index to take ownership of the widget from the layout (QGridLayout::takeAt(index)).
Wrap it all in a convenience function?
I've always had trouble reordering widgets in layouts, removing widgets from layouts, and etc... Oftentimes, I just resort to deleting the layout and re-adding the widgets. =(
For removing a widget within a QGridLayout by layout position, you can simply use
layout->removeWidget(layout->itemAtPosition(row, column)->widget());
However, you have to note the following about it:
This code assumes that there actually is an item on the
specified position in the layout. If it isn't, itemAtPosition() will
return null. So you need to be sure about the position, or check it explicitly.
This code will remove the widget from the layout, but not delete it.
You have to put the widget back into a different layout or give the
widget a reasonable geometry yourself. If you don't, the widget will simply keep
visible at its current position. If you want the widget to be
destructed, you'll have to delete it explicitly after it has been
removed from the layout.
This code will only work for top-level widgets within the layout which have been
added with addWidget(). It won't work for nested layouts added with addLayout().
If you need to care about nested layouts as well, see
my answer about removing rows and columns from grid layouts.

QSpacerItem In QFormLayout - Vertical Expand

I would like to force an expanding space in my QFormLayout, but no matter what QFormLayout only uses the QSpaceItem::sizeHint(). Does anyone know a way around this, or the proper way to handle this?
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
SetupLayout();
}
void MyWidget::SetupLayout()
{
QFormLayout * layout = new QFormLayout();
layout->addRow("Something1", new QComboBox());
layout->addRow("Something2", new QSpinBox());
//Spacer
layout->addItem(new QSpacerItem(0,10, QSizePolicy::Expanding, QSizePolicy::Expanding));
layout->addRow(QPushButton("Something3"));
setLayout(layout);
}
So there were a few different issues:
QFormLayout do not expand like other layouts. My widgets (a few of them) were being placed into a QFormLayout. This prevented them from expanding. I switched my main parent layout from QFormLayout to QVBoxLayout. This made me have to use QLayout::setAlignment(Qt::AlignTop)
This fixed a few problems with a few of my other widgets not expanding. However these widgets used QVBoxLayout. The widget above uses a QFormLayout. To get this expand, I had to use the following line in my QSpacerItem:
QSpacerItem * my_spacer = new QSpacerItem(0,1000, QSizePolicy::Expanding, QSizePolicy::Expanding);
I am supplying some example code. The goal is to show the hierarchy, and where QFormLayout would cause trouble.
Example code:
//A main Widget class
void SetupLayout()
{
QHBoxLayout * main_layout = new QHBoxLayout();
main_layout->addWidget(Some_Widget);
//Create a control widget
control_widget = new QWidget(); // IMPORTANT control_widget is a member
QVBoxLayout * layout = new QVBoxLayout(); //IMPORTANT!!!! - Here it was QFormLayout
layout->setAlignment(Qt::AlignTop); //IMPORTANT - Needed this after switching to QVBoxLayout
layout->addWidget(new QComboBox("stuff")); //Some combo box
control_widget->setLayout(layout);
main_layout->addWidget(control_widget);
}
//Later on, we have a "Put a new widget in the control area" task
void NewControlArea()
{
if(current_control)
control_widget->removeWidget(current_control); //current_control is a member variable
current_control = new MyWidget(); //FROM ABOVE
control_widget->addWidget(current_control);
}
If MyWidget uses a QFormLayout, things are not expanded unless I add spacers with size hints. However, if MyWidget uses a QVBoxLayout, any QWidgets inside it are expanded correctly.
After lot of time with manual as well as lots of tries i guess it's impossible to do what you want using QFormLayout.
This layout is desinged for windows with lot of fields to fill, but only for that.
If you want to add bigger spacing between sections of your form you can use QVBoxLayout with a couple of QFormLayout's inside it separated by spacings.
Notice that in this case each section will have own width of first and second column so maybe that is not the best solution.
The other solution (if we are talking about grouping some options) is to use a couple of QGroupBoxes with QFormLayouts in it. The groups will not be separated by growing spacing, but it will be very readable and you can name your groups. If grouping options is what you want to do - this is probably the most common and user friendly way to do this.
If you only want visual effect you pointed - columns with same width in every section and growing spacing between sections, you can use QGridLayout with 2 columns and add spacers in rows between sections. In this case you have to create QLabel to put into first column by yourself.
Just for posterity, I was having a similar problem. I found that having an organization like the following would cause anything in the inner layout (QHBoxLayout) to not expand vertically as they would if I had dropped them into the QFormLayout by themselves:
QFormLayout
-QHBoxLayout
--QListWidget
However, if I added a layer of indirection by putting the HBoxLayout into a QWidget, then sizing worked correctly:
QFormLayout
-QWidget
--QHBoxLayout
---QListWidget
So you might try adding a QWidget in there and putting your spacer inside of it.
What I did is probably a bit of hacking and is not very elegant, yet it did the trick in just one row:
addRow(" ", (QWidget*)nullptr);
This doubles the space between rows added before and after this call.
I suspect that MyWidget has the wrong size. The layout will use the widget's size to work out how best to layout its own items. Just because one of those items is set to an expanding policy does not mean that the layout will force the size of MyWidget to expand. If the size of MyWidget is fixed there is nothing the internal layout can do.
If you make MyWidget bigger you should see the layout working as you hope. Perhaps you need to put MyWidget in a vertical layout with its policy set to expanding? Without knowing how MyWidget gets its size it's hard to be sure.