PDA

View Full Version : What is a Qframe? can I use it as a layout/container?



Nfrancisj
28th June 2016, 06:14
In the Designer, I can create/drag a Qframe onto the canvas, then add items inside. To me this feels like a container/layout object. Any truth to that? Can I use Qframe as I would a Layout?
I did check out the docs, and that says I should treat Qframe like a styling element and not a layout element. So I'm a bit confused.

I did compile the UI from the designer to see the code, and it wasn't helpful. The code doesn't reflect a "layout style" workflow, meaning it doesn't add items via a ".setLayout" or "addItems" equivalent.

I've added a quick pic of what I have in the designer. I'm trying to write that via Python to get the exact same result. (via code alone)

I have 3 labels. One is a pixmap. How do I add those to to the frame to get what I did in the designer?

Thanks!

12007

anda_skoa
28th June 2016, 09:45
QFrame is most often used as a container, i.e. other widgets are add as its children and then layouted within its boundaries.

It can be used as just a visual item, e.g. as a separator line.

As for your screenshot: if you look at the object tree you will see that both container widgets (the main form itself and the frame) have the "missing layout" indicator item.

Which is why your generated code did not have any layout related calls, there aren't any in there yet.

Cheers,
_

Nfrancisj
29th June 2016, 04:28
Thanks Anda.
I used .setLayout() member on the Qframe object, which seems to work fine, but you mentioned adding widgets as a child to the Qframe. Which class member would I use to add child?

Although I did get it working and was able to replicate what I have in the designer, I feel that it's the incorrect way as it's not done the same way as the auto generate code does it. I still don't understand how the designer generated code gets the result it gets.
The generated code creates a Qframe object, and adds it to the Qlabel object as an argument....that's it! and it all works. I don't see how it connected, or how it sets the position. :/

anda_skoa
29th June 2016, 08:47
I used .setLayout() member on the Qframe object, which seems to work fine, but you mentioned adding widgets as a child to the Qframe. Which class member would I use to add child?

When you are using a layout, then you don't have to care about this. The layout will ensure the correct parent/child relationship between the frame and the elements in its layout.



Although I did get it working and was able to replicate what I have in the designer, I feel that it's the incorrect way as it's not done the same way as the auto generate code does it.

Sounds good to me, but you can show your code if you'd like someone to have a look.



I still don't understand how the designer generated code gets the result it gets.

Your designer scene was missing the layouts, so uic couldn't generate any code for that.



The generated code creates a Qframe object, and adds it to the Qlabel object as an argument....that's it! and it all works.

It passes the frame (container) as the parent to the widgets that are inside the container.



I don't see how it connected, or how it sets the position. :/
Since there is no layout very likely by calling setGeometry() on each child.

Cheers,
_

Nfrancisj
30th June 2016, 01:47
thanks! makes sense.

Diving deeper into Qframe, is there a way to give it mouse clicked events like a push button?
i used the Qframe to make thumbnails, but now i need click and context menu actions. :?

thanks

anda_skoa
30th June 2016, 09:53
Diving deeper into Qframe, is there a way to give it mouse clicked events like a push button?

Yes, you just need to overwrite the mouse event handler functions that you need, e.g. QWidget::mousePressEvent().



i used the Qframe to make thumbnails, but now i need click and context menu actions. :?

For context menu there are several options.
The easiest way is to add actions to the widget, see QWidget::addAction().

Cheers,
_