PyQt4: Hide widget and resize window - python

I'm working with several widgets but the solution just won't come out. What I have is a series of buttons in series of QHBoxLayouts. Some buttons are hidden by default, but they will appear when needed. To solve space issues, all buttons have a minimum and maximum size so they always look well packed. Also I have a QTextEdit, visible by default, which is in a QVBoxLayout with the QHBoxLayout that hold the buttons
So the problem is this: When I hide the QTextEdit and show the other buttons, the window won't resize.
After searching I found that using self.ui.layout().setSizeConstraint(QtGui.QLayout.SetFixedSize) will do the trick, but the problem is that it takes the maximum size from all widgets, therefore I end a huge window. Doing self.ui.layout().setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize) won't resize the window
I already tried using self.ui.resize(0,0), and when doing a self.ui.layout().update() I got False (which I find odd, http://doc.trolltech.com/4.6/qlayout.html#activate), and also tried to override sizeHint() but it keeps using the max size for all widgets.
Is there a way to resize the window and while taking care of the min and max size of a widget?
Thanks in advance

The answer was quite lame... Just needed to change the QVBoxLayout for a QGridLayout and use self.ui.layout().setSizeConstraint(QtGui.QLayout.SetFixedSize)

Related

Qt Designer: How to lock window size?

So Qt Designer has this feature where you can resize the window you're working on. And that's fine and dandy, but every now and then, I accidentally drag it, and the window gets larger or smaller than I wanted it to be. The undo action doesn't undo the resizing, so that's a bummer. Also setting max and min sizes for the central widget doesn't do anything to fix this issue.
Is there a way I can have the window size locked?
Here's a demo:
Use QWidget::setFixedSize(w, h) for explicitly set width and height or QLayout::setSizeConstraint(QLayout::SetFixedSize) if you want the fixed size to be determined automatically based on content.

Automatically resizing a guizero window

I have written a guizero code that creates a new window with different amounts of information on it depending on what is selected. I was just wondering if there is a function to resize the window to fit all of the information. I am currently just making the window big enough to fit the largest amount of information but I would rather have it so it resizes it automatically.
And also is there a simple function so that if the window size is changed all of the widgets inside of it change size as well?
I think is not possible to automaticly resize the window but you can count the number of lines and resize the window.
numLines*defaultHeightByLine + height of the other elements

Tkinter - scale all components on resize?

Is it possible that when a Tkinter window is made bigger or larger, everyhting in the window is scaled?
So all the proportions stay the same but their sizes vary.
Now when the window is resized all the buttons etc stay the same so I disabled resize because there is no point, it just looks bad.
No, it is likely not possible, depending on what you mean by "everything" and what you mean by "scaled". Any widget can be made to stretch to fill its allotted space. Text widgets and canvas widgets, for example, scale nicely. A button or label will fill the space it's in, but the text inside the widget won't change (that is also true of text and canvas widgets).
It's possible to organize your GUI so that when the window resizes, everything remains in its proper place at its original size. Having buttons that automatically scale is not something most people would expect. Disabling resizing usually results in a poor user experience -- users should have the ability to make a window larger or smaller.

Resize QMainWindow to minimal size after content of layout changes

I'm using a subclass of QMainWindow, in which I declared a central widget. This widget contains, among other things, a QGridLayout, which holds a set of buttons. The amount of buttons can grow or shrink, depending on the user's input. The spacing is set to zero, so that all buttons are clumped together. By default, it looks like this:
If the amount of buttons is increased, the grid and window will grow too just fine; if, however, the amount of buttons is reduced, it will look like this:
Now I would like to resize the window/layout/widget so that all buttons may use the minimal space. I've tried various things, but all to no avail. I had a look at this and this question, as well at various threads in the Qt board, but none of them worked for me.
My layout is build as follows:
self.grid = QtGui.QGridLayout()
self.grid.setSpacing(0)
hBox = QtGui.QHBoxLayout()
hBox.addWidget(...)
vBox = QtGui.QVBoxLayout(self.widget)
vBox.addLayout(hBox)
vBox.addLayout(self.grid)
self.setCentralWidget(self.widget)
I tried resizing it with ...
self.widget.layout().activate()
self.resize(self.minimumSize())
# self.resize(self.sizeHint())
... and various other methods. I also tried setting the size policy of my window and grid.
You can resize the window to minimumSizeHint() after the number of widgets is changed :
self.resize(minimumSizeHint())
This will shrink the window to minimum size. But you should consider that the minimum size is not computed until some events are processed in the event loop. So when the number of buttons are changed, just process the event loop for some iterations and then resize to minimum.
It's like :
for i in range(0, 10):
QApplication.processEvents()
self.resize(minimumSizeHint())
Another option is to single shot a QTimer which calls a slot in which you resize the window to minimum. This way when you resize the window, the minimum size hint is computed correctly.
Though this was already answered, I found a very simple, elegant solution here. It was hard to find because the question is for this issue in Qt not PySide. You will have to translate it to PySide but it's really simple.
Essentially you want to set the size constraint of the layout you want to have automatically resized to QtGui.QLayout.SetFixedSize like so:
vBox = QtGui.QVBoxLayout(self.widget)
vBox.setSizeConstraint(QtGui.QLayout.SetFixedSize)
The code is not intuitive AT ALL but it works.
for those who, like me, don't immediately get how to use the QTimer:
def function_with_blown_window_size(self)
...
# 1 ms was too low for a refresh -> singleShot timer with 10ms delay
QTimer.singleShot(10, self.window_size_readjust)
def window_size_readjust(self):
self.adjustSize() # worked in my case
#self.resize(self.minimumSizeHint()) # alternative worked equally well

How to I prevent widgets in a QGridLayout from overlapping?

I have a QGridLayout that has a number of widgets in six rows. Rows 1, 3 and 5 contain a QLabel, a QLineEdit and a QPushButton that opens up a file selection dialog. Rows 2, 4, and 6 contain a QLabel that is used to present validation errors to the user. The errors are hidden in the image below.
When the window is able to take up as much space as it likes, this issue does not appear. But when I restrict the size of the window at all, the elements in this list look like this:
So my question is, how do I ensure that all qwidgets in the QGridLayout can take up as much space as they need?
EDIT:
Following #Avaris' suggestion, I tried calling setMinimumSize() for each widget in the QGridLayout (all 6 rows), and the visible elements look much better (thanks, #Avaris!). Yet now when I cause a validation error, the error itself (a red QLabel) is laid over the other widgets. Can I force the QGridLayout to not overlap?
Here's what it looks like now:
While searching around for a solution, I found something in the TrollTech archives that supported #Avaris's idea about setting the minimum size of the widget. The post suggests setting a minimum size for each widget in the QGridLayout.
My implementation looks like this as I loop through all elements to be added to my QGridLayout:
widget = QtGui.QWidget()
hint = widget.sizeHint()
if hint.isValid():
widget.setMinimumSize(hint)
The validity of a hint varies on the layout used, but widget sizehints always seem to be valid when I use a QGridLayout.

Categories

Resources