12

How do I overlay widgets in Qt?

I want to create some widgets and place them out-of-layout, but rather tweak their size and position when some other widget's geometry is changed.

Something like the buttons on the screenshot:

alt text

Community
  • 1
  • 1
Andrew T
  • 5,549
  • 7
  • 43
  • 55

3 Answers3

12

You just need to create your QPushButton (or any QWidget), indicate its parent QWidget and then display it.

Don't add it to the parent layout else you will not be able to move it as you want.

Don't forget to indicate its parent else it will be displayed as independant QWidget.

In these conditions your QPushButton will be considered as child of the QWidget but not member of the parent's layout. So it will be a "floating" child and you must manage it's behaviour when resizing parent's QWidget.

If you want a unified behaviour for all overlay buttons, you should subclass QLayout and redefine members bahaviour.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
Patrice Bernassola
  • 14,136
  • 6
  • 46
  • 59
  • For the right-alignment seen in the screen shot, one can reimplement resizeEvent() for the parent widget and move the child widgets manually in there. – Frank Osterfeld Aug 31 '10 at 13:03
3

If they're child of a widget without a layout, you should be able to move them around as you please, I think.

Macke
  • 24,812
  • 7
  • 82
  • 118
2

I needed a widget like this for a project I'm working on, so I took Patrice advice and wrote this code (Python) PyQt4 Widget Overlay

D3f0
  • 98
  • 9