How to set absolute position of the widgets in qt

后端 未结 4 516
悲哀的现实
悲哀的现实 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_()
    
    0 讨论(0)
  • 2020-12-09 16:26

    Use QWidget::move() to set the position, QWidget::resize() to set the size and reimplement the parent's resizeEvent() handler if you need to re-position the widgets if their parent resizes.

    0 讨论(0)
  • 2020-12-09 16:38

    I do it by creating a class based on Qwidget put its position in the constructor

    ex: QPointer a = new QWidget (0, posx, posy, width, height)

    in the constructor of this Qwidgetclass add these integrers posx, posy, width, height and then do this.move(posx,posy); this.resize(width,height);

    0 讨论(0)
  • 2020-12-09 16:40

    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.

    0 讨论(0)
提交回复
热议问题