Remove a sublayout in qt? - python

In PyQt 4.5, I have a layout inside another layout. I'd like to remove the sublayout from its parent, and hide it. I can say parent_layout.removeItem(child_layout) to remove the layout from its parent, but it still shows on the widget. I can't find any way to hide it in one step, as QLayout doesn't have a hide() method like QWidget does.

The easy solution would be to have an interior widget, not an interior layout. You could assign the layout you desire to the widget, then just remove/hide the widget when you want to do so. A good rule of thumb is if you just want to arrange how widgets appear, then use a layout; if you want to hide/show them as a group, use a widget.

With some help from flupke on #qt, I came up with:
for i in range(0, child_layout.count()):
child_layout.itemAt(i).widget().hide()
parent_layout.removeItem(child_layout)
Which assumes all the child layout's children are widgets. Is there a simpler solution?

Related

Rescaling the absolute coordinate x,y values of self items in order to resize items fit into different resolutions [duplicate]

I want to have a small QFormLayout that grows to fill its parent widget.
I created a new .ui file using the QWidget template in Qt Designer. I put a QFormLayout inside that 'window', then put some controls inside that QFormLayout.
This all works reasonably well, but the QFormLayout always stays at the size I set in Qt Designer. I would like the QFormLayout to fill its parent widget and grow/shrink with it.
How can I accomplish that?
In Designer, activate the centralWidget and assign a layout, e.g. horizontal or vertical layout.
Then your QFormLayout will automatically resize.
Always make sure, that all widgets have a layout! Otherwise, automatic resizing will break with that widget!
See also
Controls insist on being too large, and won't resize, in QtDesigner
I found it was impossible to assign a layout to the centralwidget until I had added at least one child beneath it. Then I could highlight the tiny icon with the red 'disabled' mark and then click on a layout in the Designer toolbar at top.
The accepted answer (its image) is wrong, at least now in QT5. Instead you should assign a layout to the root object/widget (pointing to the aforementioned image, it should be the MainWindow instead of centralWidget). Also note that you must have at least one QObject created beneath it for this to work. Do this and your ui will become responsive to window resizing.
You need to change the default layout type of top level QWidget object from Break layout type to other layout types (Vertical Layout, Horizontal Layout, Grid Layout, Form Layout).
For example:
To something like this:

How can i dynamically "fill" a Layout with already existing objects in PyQt5?

I have a ScrollArea with a Grid Layout inside of it.
Inside that grid layout, i am initially creating a lot of checkboxes inside that grid Layout.
Now when i use .hide() on a checkbox, the white space persists. Only when i hide all checkboxes within a row, the row is hidden.
How can i ensure that when i hide some checkboxes, the checkboxes below "move up" to fill the space? I already used .update() on the parent widget. I cant provide a code example but i assume this is a pretty basic UI operation so i hope someone knows what i am talking about. I tried fiddling around with the sizePolicy but didnt find the solution sadly.
Thanks in advance!

How to avoid layouts to change their size?

I set a QMainWindow with two layouts, called A and B, in a Pyqt4 app written in Python 3.5
They usually display as shown in this schema:
But at some point, something in Layout B makes wider. Therefore Layout B makes wider too, overlapping Layout A, which remain partially hidden:
Could anybody please give me a hand to find a solution to this behaviour?
I would like to lock Layout A and B widths somehow to avoid this issue.
Without seeing your code, I suspect you add both layouts directly to your main window, without putting them into a main layout that would control their resizing. This way you're creating unnecessary problems, as you need to manage the layout's position and size manually. Read more in the Qt docs.
To solve this, you just add a QHBoxLayout to the main window, then the child layouts to that using addLayout() with an appropriate stretch factor. Here's an example in C++ (untested) to illustrate the necessary steps:
QHBoxLayout* mainLayout = new QHBoxLayout();
QGridLayout* childLayout0 = new QGridLayout();
QGridLayout* childLayout1 = new QGridLayout();
//Add widgets to the child layouts
mainLayout->addLayout(childLayout0, 2);
mainLayout->addLayout(childLayout1, 1);
mainWindow->setLayout(mainLayout);

PySide : "Dynamic" positionning of buttons on GUI?

I'm using python 2.7.5 on OSX 10.8. I'm learning PySide and trying to build a simple GUI.
I managed to use buttons (WOAAA!) used to chose a path or execute functions :
pathBtn = QtGui.QPushButton("FITS file path", self)
pathBtn.setToolTip('Choose the <b>path</b> to your FITS file')
pathBtn.clicked.connect(essai)
pathBtn.resize(pathBtn.sizeHint())
pathBtn.move(200, 100)
My problem is, when the program is running and I change the size of the window with the mouse cursor, the buttons don't move, don't adapt to the size variation.
I tried to find some answer (hell yeah google) and I understand that "QVBoxLayout" should do what I want (some kind of "dynamic" positionning, don't know if there's a specific name for that), but I didn't understand its syntax nor how to use it...
Any help appreciated!
In Qt widgets, layouts and the widget's size hints determine how things resize. The general procedure to layout a widget would be (for example):
dialog = QDialog()
layout = QVBoxLayout()
label = QLabel('This is a label')
edit = QLineEdit('This is a line edit box')
layout.addWidget(label)
layout.addWidget(edit)
dialog.setLayout(layout)
(*I cannot test this code here at work (no Qt/PySide), so consider this "pseudo code" :-)
This results in a dialog widget with a label and an edit box. If you resize the dialog widget, the layout and the resize properties of the widgets ensure that the label and edit box resize appropriately: horizontally both expand maximally, but vertically the edit will keep the same size while the label takes all the remaining space. This is because the resize hint of the edit box says it wants to keep its height (namely, one line of text).
If you do not specify a layout, your widgets (buttons, labels) don't do anything whenr resizing their parent widget, which is what you are observing. Hence, the solution is indeed the QVBoxLayout, use it as I described above.
By the way: for more complicated layouts, you probably want to use the Designer tool provided with Qt: this tool lets you see and test your GUI a priori.

Assiociate Widgets into Groups, for Hide and Show, wxpython

I have a bunch of widgets and right now I am using Hide() and Show() to each widget individually when I flip through different sections/pages of my program.
Because I did this, You can see each widget leaving/showing one by one (which kinda sucks).
Is there anyway to group all these widgets and then be able to Hide() and Show() this group, to avoid this "one by one" habit?
Try using Freeze/Thaw/Layout when you are showing and hiding the widgets. This way they should all appear/disappear at the same time.
Put your group of widgets organized in a sizer in the same parent container (p.e. a panel) and hide the parent. All the widgets disappear with the parent.
Note that sometimes hiding (for example) buttons or checkboxes is not the best solution. Available functionality for the user can be also modulated using widget.Disable()

Categories

Resources