How to set absolute position of the widgets in qt

I am using QT to develop a rich UI application.

  • I need to position widgets at absolute positions
  • I should be able to put the widget in background / foreground to add a few effects.
  • Simple example would be, I have to show a boiler with the water level inside the feed tank.

  • Take a feed tank image and embed in a label.
  • Position a progress bar in the center of the feedtank to display water level.
  • Now in this case the progress bar would be in the foreground and the label in the background.

    Regards,


    You just need to create your widget, 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 independent widget.

    In these conditions your widget 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 behavior when resizing parent's QWidget.


    使用QWidget :: move()来设置位置,QWidget :: resize()设置大小并重新实现父级的resizeEvent()处理程序,如果您需要重新定位窗口小部件的父级调整大小。


    Additionally to the answers by Patrice Bernassola and Frank Osterfeld the stacking order might be of interest. It corresponds to the order of the children which you get by findChildren and the ways to manipulate the order are using raise_ (with the trailing underscore), lower or stackUnder .

    An example changing the order with each of the 3 available functions using PySide is here:

    from PySide import QtGui
    
    app = QtGui.QApplication([])
    
    window = QtGui.QWidget()
    window.resize(400, 300)
    window.show()
    
    rA = QtGui.QLabel()
    rA.name='square'
    rA.resize(100, 100)
    rA.setStyleSheet('background-color:red;')
    rA.setParent(window)
    rA.show()
    
    text = QtGui.QLabel('text')
    text.name='text'
    text.setParent(window)
    text.show()
    
    # text was added later than rA: initial order is square under text
    
    rA.raise_()
    rA.lower()
    text.stackUnder(rA)
    
    children = window.findChildren(QtGui.QWidget)
    for child in children:
        print(child.name)
    
    app.exec_()
    
    链接地址: http://www.djcxy.com/p/77396.html

    上一篇: PyQt,小部件没有显示

    下一篇: 如何在qt中设置小部件的绝对位置