PyQt: How can I create a custom scroll area? - python

I am pretty new to GUI programming and to Qt itself, albeit having a lot of experience with HTML, CSS and JS (for UI design).
I am struggling to understand even the basics of designing custom data-display elements with Qt.
How can I create a scrollable area with custom rectangular data
displaying elements in it? I just want a scroll area with a
background image, and I want it to be populated with one or more
copies of the same data-viewing item which is to have 2 labels (like
twitter), a background image and some buttons.
I have been doing research for days now but could not find any examples.
Should I design a "widget" using Qt Designer? Then how do I load that
.ui as a widget and manipulate the labels on it?

For future reference a "very custom" way to do this would be as follows:
Create a QScrollArea.
Place a Widget inside it (QWidget for example)
Create a Layout for the QScrollArea's widget which will be the one scrolling.
Style QScrollArea and the Widget inside as desired.
Place custom widgets inside the layout.
Set autoscroll etc for the QScrollArea so it will resize and show elements as desired.

Related

PyQT how to change layouts size

I am trying to make a GUI that is very similar to Spotify using PyQT
I've already designed the main window and I am struggling with applying the design to QT Creator.
This is what I want it to look like
But I'm trying to use Layouts in order to organize every widget.
For example
Image of the main window, split into 3 parts
like in the sketch I've made the software will be split into 3 parts, left bar, mid which is where the explore title is and right bar.
The problem I have is that I can't control the layouts size and the size of what's below them for example in the sketch the mid bar is wider than the right bar and the right bar is wider than the left bar but in the QTCreator I've no idea how to change the width and height of objects inside Layouts.
The first option (which I personally prefer) is to fill your layout with it's contents first (buttons, labels, etc.). This will already start to scale your layout, if there are more buttons in the middle bar than on the other bars. If your layout still isn't what you want it to be, you can use Spacers. They can push and pull puttons and position the in relation to other parts of the layout, as well as to the layout itself. The scaling will be adjusted automatically according to the spacers position.
This option has the advantage, that your application will be correctly scaled and not completely chaotic when it is run on a device with a display aspect ratio which is not the same as the one of the machine your developing it on.
There is, however, also a minimumSize and maximumSize attribute to the layouts, which provide a much more straightforward possibility, but sometimes cause your layouts to become very weird when adding or removing a button, or changing the text of a label. More on this option can be found in the Qt docs:
https://doc.qt.io/qt-5/qlayout.html

Pyqt add 9 elements in layout and then lot of layout in group box

I want to make elements in row in one layout and a lot of layouts will be in GroupBox, it must be like this.
Result Interface
I try to release this
formLayout = QFormLayout()
for i in range(50):
formLayout.addRow(QLabel(i), QPushButton("Start"))
groupBox.setLayout(formLayout)
self.scrollArea.setWidget(groupBox)
self.scrollArea.setWidgetResizable(True)
Result from code
I know that i should make in grib layout, because i have a lot of elements, but how can I create a layout with layouts which everyone have 9 elements?
Just explain how it should be grouped , like this scrollArea->groupBox->formLayout.addrow(elements)
I don't need full code, him I can write myself.
The layout you're showing is not a form layout, it could be a grid layout, but it actually seems more like a QTableView (or QTableWidget) or even a QTreeView (if those small arrows on the left are used to expand elements). Using nested layouts in this case might not be a good solution, as each layout would be independent from the others, with the result that the widgets might not be properly aligned. This small arrows for expand element in that case a QTreeView with a QStandardItemModel or a QTreeWidget might help, then use setItemWidget() to add buttons. –
musicamante

how to create an overlaying frame with qt4 in python

I would like to know if it is possible and how to create an overlaying frame (or any other widget) at a fixed position that would overlaying the content of the MainWindow with PyQt4.
The idea is to get a html-like render we can get with position: fixed or position: absoute. From what i know, all widgets have to inherit from the MainWindow's widget but is there a way to change this behavior or any way to simulate a widget with such level ?
Thank you
I suppose your problem is to insert an object inside a frame that uses a layout, and avoid respecting that layout.
I am afraid the only thing i can come up with in your situation is either to avoid using layouts altogether (but that's very uncomfortable) or to extend a layout class and place the frame outside of the layout.
To make your frame stay on top, look for z-order.
http://qt-project.org/search/tag/z-order

Python : UI Image Wipe, compare two images

I wanted to know if anyone knew where to start in terms of recreating this sort of functionality?
http://www.learningnuke.com/wp-content/uploads/nukewipepreview.png
In the picture you can drag the centre line to reveal Image A or Image B or parts of each, interactively.
I want to be able to wipe/reveal across two images, maybe it's possible doing some sort of interactive crop of sorts.
Wanting to add this feature to a window in Maya, so maybe with QT, but not essential.
Just some pointers would be great.
I can tell you that this is possible via Qt/PyQt in maya. You can create a dialog that displays QPixmaps with some form of mouse interaction to control their display. I would forget about trying to extend the actual Render View as this would be a pain in the ass.
Just focus on a Qt solution. Unfortunately beyond this, I'm not sure what more I can offer unless you have a specific question about its implementation.
I would probably stack the QPixmaps on top of each other inside of custom QLabel widgets. The QLabel would have a custom mousepress/move event that would resize maybe the right edge to simulate the wipe effect, and reveal the one stacked underneath.
Also, it does resemble the functionality of a QSplitter so that might also work, with an image on each side of the layout and a custom style to the split bar.

How do I overlap widgets with the Tkinter pack geometry manager?

I want to put a Canvas with an image in my window, and then I want to pack widgets on top of it, so the Canvas acts as a background.
Is it possible to have two states for the pack manager: one for one set of widgets and another for another set?
The answer to your specific question is no. You can't have two states or otherwise use pack two different ways in the same parent.
However, what I think you want to accomplish is simple. Use the built-in features of the canvas to create an image item that is part of the canvas, then pack things into the canvas as if it were a frame.
You can accomplish a similar thing by creating a label widget with an image, then pack your other widgets into the label.
One advantage to using a canvas is you can easily tile an image to fill the whole canvas with a repeating background image so as the window grows the image will continue to fill the window (of course you can just use a sufficiently large original image...)
I believe that Bryan's answer is probably the best general solution. However, you may also want to look at the place geometry manager. The place geometry manager lets you specify the exact size and position of the widget... which can get tedious quickly, but will get the job done.
... turned out to be unworkable because I wanted to add labels and more canvases to it, but I can't find any way to make their backgrounds transparent
If it is acceptable to load an additional extension, take a look at Tkzinc. From the web site,
Tkzinc (historically called Zinc) widget is very similar to the Tk Canvas in that they both support structured graphics. Like the Canvas, Tkzinc implements items used to display graphical entities. Those items can be manipulated and bindings can be associated with them to implement interaction behaviors. But unlike the Canvas, Tkzinc can structure the items in a hierarchy, has support for scaling and rotation, clipping can be set for sub-trees of the item hierarchy, supports muti-contour curves. It also provides advanced rendering with the help of OpenGL, such as color gradient, antialiasing, transparencies and a triangles item.
I'm currently using it on a tcl project and am quite pleased with the results. Extensions for tcl, perl, and python are available.
Not without swapping widget trees in and out, which I don't think can be done cleanly with Tk. Other toolkits can do this a little more elegantly.
COM/VB/MFC can do this with an ActiveX control - you can hide/show multiple ActiveX controls in the same region. Any of the containers will let you do this by changing the child around. If you're doing a windows-specific program you may be able to accomplish it this way.
QT will also let you do this in a similar manner.
GTK is slightly harder.

Categories

Resources