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.
Related
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:
I've got a GUI in PyQt that does the following:
The main window is a grid layout (2 columns and 3 rows). This is the scheme (soory I can't post images yet):
Now, when resizing the Main Window I'd like the QTableView widgets to be resized. However, it happens otherwise:
And the tables stay almost fixed in size (but every size-fixing property is set not to fix anything), they just expand for about 50 pĂxels. I've tried changing the main layout to a horizontal layout and then putting vertical layouts there but no change. I'm designing the GUI with the QtDesigner as I have no clue on how to doing it by hand-writing the code, and I need to export it to python.
What's the property determining which layout gets expanded and which one not?
I fixed it! As there were some LineEdits on the left side, they had to expand too. Setting its expanding policy to "fixed" or setting a maximum fixed it.
Thanks everybody for the help and dont worry, i'm going to learn to handwrite Qt GUIs soon.
I am using QTDesigner to design an application (I am trying my hand at a dual pane file manager). I can't figure out how to have the 2 widgets side by side so that they both resize when I resize the application
A layout, as JRazor mentioned, is a good solution if you want your Tree Views to have always same size. If not, use a QSplitter
From the Qt documentation:
A splitter lets the user control the size of child widgets by dragging
the boundary between them.
QSplitter *splitter = new QSplitter(parent);
QListView *listview = new QListView;
QTreeView *treeview = new QTreeView;
splitter->addWidget(listview);
splitter->addWidget(treeview);
EDIT
Sorry I didn't notice that you are actually looking for a python solution. I have provided a C++ example code, but I believe that it's not that big difference doing it with python.
Use layout:
layout = QHBoxLayout(self)
layout.addWidget(left_tree)
layout.addWidget(right_tree)
If you want use QtDesigner: http://doc.qt.io/qt-4.8/designer-layouts.html
I need to write an application in Python with a User Interface. I decided to go for PyQt4 to make the GUI. It is included in the Anaconda distribution of Python.
To make a GUI running smoothly on a 4K display is not trivial. Most components are way too small by default. Some components - like buttons - are easy to resize:
btn = QtGui.QPushButton("Quit", self)
btn.resize(100,100)
But how do I resize other components, like the QMenuBar? Can I make the QMenu buttons (Like "File", "Edit", ..) that populate the QMenuBar larger? Can I make the WindowTitle in the top left corner also larger?
Thank you very much.
You can change the font and font-size of individual widgets.
font = self.menuBar().font()
font.setPointSize(20)
self.menuBar().setFont(font)
But typically something like this would be handled by your OS/window manager. I'm assuming all your other applications have text that looks just as small? For high-res displays, most popular OS's have scaling features to emulate the size of a lower resolution monitor.
Can I use pack once the main loop has been showed, or should I use something else to add /remove widgets to /from a vbox afterwards ?
I have this gtk.Window() that contains a vbox, where a menu, a treeview and a button are packed. At the push of this button, I want to display an image in a new container inside this window / vbox, and ideally, close said container at will.
(think image viewer with a file list, you click on an image file and a pane opens displaying it, if you click on another image file the new image is displayed in place of the old, and you can close the image pane)
My question is : How do you do that ? My trials so far led me to believe that once the vbox has been show()'d, you cant pack anything else into it..?
Has the "image" container have to exist prior to being displayed...?
What is the proper process to do this, in witch direction of the GTK manual should I look?
In GTK+ all widgets are hidden by default (which I think was a stupid design decision, but oh well). You usually call show_all() on a window, so indirectly show all widgets contained in it by the time of the call. If you add (pack, whatever) a widget later, don't forget to show() it manually.