Group widgets vertically and horizontally in a layout

1

I'm doing a framework in python and Qt, and I have a question. Let's see if with some help I can solve it.

My question is this: when making a layout I want some buttons to form a vertical layout and others a horizontal layout, so the screen would not be too big and everything would be more organized. For now I am only able to create a vertical layout type and add everything to it.

I would like to do everything in a class, because this layout is part of a StackedWidget and depending on the selected button, one or the other will be shown on the screen (which will be another different class).

This is what I have now:

But I would like something like this:

My current code is as follows:

class propiedades_ueye_class(QtGui.QWidget):
    def __init__(self, parent=None):
        super(propiedades_ueye_class, self).__init__(parent)
        self.foto = QtGui.QPushButton('Foto')
        self.preview = QtGui.QPushButton('Preview')
        self.distancia = QtGui.QPushButton('Distancia')
        self.livemode = QtGui.QPushButton('Live mode')
        self.timelapse = QtGui.QPushButton('TimeLapse')
        self.ejex = QtGui.QLineEdit()
        self.ejey = QtGui.QLineEdit()
        self.segundos= QtGui.QLineEdit()
        self.nfotos = QtGui.QLineEdit()
        self.lejex = QtGui.QLabel("Eje X")
        self.lejey = QtGui.QLabel("Eje Y")
        self.lsegundos = QtGui.QLabel("Segundos")
        self.lfotos = QtGui.QLabel("Fotos")
        self.centrado = QtGui.QCheckBox("Centrado")
        self.mallado = QtGui.QCheckBox("Mallado")
        self.definido = QtGui.QCheckBox("Definido")
        self.reticulo = QtGui.QCheckBox("Reticulo")
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.lejex)
        layout.addWidget(self.ejex)
        layout.addWidget(self.lejey)
        layout.addWidget(self.ejey)
        layout.addWidget(self.centrado)
        layout.addWidget(self.mallado)
        layout.addWidget(self.definido)
        layout.addWidget(self.foto)
        layout.addWidget(self.preview)
        layout.addWidget(self.distancia)
        layout.addWidget(self.livemode)
        layout.addWidget(self.reticulo)
        layout.addWidget(self.lsegundos)
        layout.addWidget(self.segundos)
        layout.addWidget(self.lfotos)
        layout.addWidget(self.nfotos)
        layout.addWidget(self.timelapse)
        self.setLayout(layout)
    
asked by Álvaro 28.07.2017 в 11:23
source

1 answer

0

You always have multiple options, remember that you can nest layouts inside others if you wish. However, for your case I think the simplest thing is to use a QGridLayout :

import sys
from PyQt4 import QtGui


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

        self.foto = QtGui.QPushButton('Foto')
        self.preview = QtGui.QPushButton('Preview')
        self.distancia = QtGui.QPushButton('Distancia')
        self.livemode = QtGui.QPushButton('Live mode')
        self.timelapse = QtGui.QPushButton('TimeLapse')
        self.ejex = QtGui.QLineEdit()
        self.ejey = QtGui.QLineEdit()
        self.segundos= QtGui.QLineEdit()
        self.nfotos = QtGui.QLineEdit()
        self.lejex = QtGui.QLabel("Eje X")
        self.lejey = QtGui.QLabel("Eje Y")
        self.lsegundos = QtGui.QLabel("Segundos")
        self.lfotos = QtGui.QLabel("Fotos")
        self.centrado = QtGui.QCheckBox("Centrado")
        self.mallado = QtGui.QCheckBox("Mallado")
        self.definido = QtGui.QCheckBox("Definido")
        self.reticulo = QtGui.QCheckBox("Reticulo")

        self.layout = QtGui.QGridLayout()
        self.setLayout(self.layout)
        self.layout.addWidget(self.lejex, 0, 0)
        self.layout.addWidget(self.ejex, 0, 1, 1, 2)
        self.layout.addWidget(self.lejey, 1, 0)
        self.layout.addWidget(self.ejey, 1, 1, 1, 2)
        self.layout.addWidget(self.centrado, 2, 0)
        self.layout.addWidget(self.mallado, 2, 1)
        self.layout.addWidget(self.definido, 2, 2)
        self.layout.addWidget(self.foto, 3, 1, 1, 2)
        self.layout.addWidget(self.preview, 4, 1, 1, 2)
        self.layout.addWidget(self.distancia, 5, 1, 1, 2)
        self.layout.addWidget(self.lsegundos, 6, 1)
        self.layout.addWidget(self.segundos, 6, 2)
        self.layout.addWidget(self.lfotos, 7, 1)
        self.layout.addWidget(self.nfotos, 7, 2)
        self.layout.addWidget(self.livemode, 8, 2)
        self.layout.addWidget(self.reticulo, 9, 1)
        self.layout.addWidget(self.timelapse, 9, 2)



def main():
    app = QtGui.QApplication(sys.argv)
    ex = Propiedades_ueye_class()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Basically we created a GridLayout of 10 rows and 3 columns. The only thing to keep in mind is the QGridLayout.addWidget method and its arguments:

QGridLayout.addWidget (self,
                       QWidget,
                       int row,
                       int column,
                       int rowSpan,
                       int columnSpan,
                       Qt.Alignment alignment = 0)

In this case, columnSpan is used for those widgets that occupy more than one column.

I do not know if I have placed each widget where it should go since your second image is more generic than the first, however the idea is that.

Exit:

    
answered by 28.07.2017 / 19:03
source