[PyQt] Position widgets over widgets

Hans-Peter Jansen hpj at urpla.net
Thu Apr 7 14:56:24 BST 2011


On Thursday 07 April 2011, 15:36:47 Mads Ipsen wrote:
> Hi,
>
> I tried that, but is does not seem to work. The 'XXX' is still hidden
> by the widget in the layout.
>
> What I eventually would like to achieve is to add a QPushButton in,
> say, the upper left corner of the parent widget.
>
> I have attached a similar example, this time with a QPushButton. This
> QPushButton is rendered useless (i.e.) you cannot press it.
>
> I thought solving this problem would be a walk in the park. Any
> clues?

import sys

from PyQt4 import QtGui

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self)

        self._label  = QtGui.QLabel('A Label')

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self._label)

        self._button = QtGui.QPushButton('&Push Me', self)

    def paintEvent(self, event):
        w = self._button.width()
        h = self._button.height()

        x = (self.rect().width()  - w)/2.0
        y = (self.rect().height() - h)/2.0
        self._button.setGeometry(x,y,w,h)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    widget = Widget()
    widget.show()

    sys.exit(app.exec_())

I will abjure any relation to this code..

Pete

> Best regards,
>
> Mads
>
> On 2011-04-07 15:19, Hans-Peter Jansen wrote:
> > On Thursday 07 April 2011, 14:58:30 Mads Ipsen wrote:
> >> Hi,
> >>
> >> I have attached a simple example where a widget sets up two
> >> labels. One which is added to the layout of the widget, and one
> >> which is not.
> >>
> >> In the paintEvent() of the parent widget I instead position the
> >> non-layout label using setGeometry to make it appear in the center
> >> of the parent widget.
> >>
> >> However, the label positioned by this approach is always obscured
> >> by the widget which was added to the layout. I want it to appear
> >> as visible, i.e. visible on top of the parent widget.
> >
> > Create _label2 after _label1, but this is a questionable approach.
> >
> > Why can't you use something like QStackedLayout?
> >
> > Pete
> > _______________________________________________
> > PyQt mailing list    PyQt at riverbankcomputing.com
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt




More information about the PyQt mailing list