[PyQt] Position widgets over widgets
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. Any clues? Best regards, Mads import sys from PyQt4 import QtGui class Widget(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self) self._label2 = QtGui.QLabel('XXX', self) self._label2.show() self._label1 = QtGui.QLabel() self._label1.setAutoFillBackground(True) palette = QtGui.QPalette() palette.setColor(self._label1.backgroundRole(), QtGui.QColor('blue')) self._label1.setPalette(palette) layout = QtGui.QVBoxLayout() self.setLayout(layout) layout.addWidget(self._label1) def paintEvent(self, event): w = self._label2.width() h = self._label2.height() x = (self.rect().width() - w)/2.0 y = (self.rect().height() - h)/2.0 self._label2.setGeometry(x,y,w,h) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec_()) ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Position widgets over widgets
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 listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Position widgets over widgets
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? 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 listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt -- +-+ | Mads Ipsen | +--+--+ | Florsgade 7, 4. th | | | DK-2200 København N | phone: +45-29716388 | | Denmark | email: mads.ip...@gmail.com | +--+--+ 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') self._button = QtGui.QPushButton('&Push Me', self) layout = QtGui.QVBoxLayout() self.setLayout(layout) layout.addWidget(self._label) 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_()) ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Position widgets over widgets
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 listPyQt@riverbankcomputing.com > > http://www.riverbankcomputing.com/mailman/listinfo/pyqt ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt