How to set absolute position of the widgets in qt

后端 未结 4 514
悲哀的现实
悲哀的现实 2020-12-09 15:57

I am using QT to develop a rich UI application.

  1. I need to position widgets at absolute positions
  2. I should be able to put the widget in background / f
4条回答
  •  轮回少年
    2020-12-09 16:24

    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_()
    

提交回复
热议问题