QGridLayout to arrange the elements at the top of the screen

2

I am using QGridLayout in Pyqt5 to build a window in python. What I want to do is that the elements are not available to occupy the entire width and height of the window, but they are grouped in the upper area of the window. My code is:

def __init__(self, fileInfo, parent=None):
    super(tabBuscadorProductos, self).__init__(parent)

    #Fichero de configuracion
    config = configparser.ConfigParser()
    config.read('properties.conf')

    self.label = QtWidgets.QLabel()
    self.label.setGeometry(QtCore.QRect(10, 40, 131, 16))
    self.label.setObjectName("label")
    self.label.setText("Criterio de busqueda:")

    self.comboBox = QtWidgets.QComboBox()
    self.comboBox.setGeometry(QtCore.QRect(150, 40, 121, 22))
    self.comboBox.setObjectName("comboBox")
    list = config.get('COMBO_CRITERIOS', 'productos').split(" ")
    self.comboBox.addItems(list)

    self.label_2 = QtWidgets.QLabel()
    self.label_2.setGeometry(QtCore.QRect(350, 40, 121, 16))
    self.label_2.setObjectName("label_2")
    self.label_2.setText("Producto:")

    self.lineEdit = QtWidgets.QLineEdit()
    self.lineEdit.setGeometry(QtCore.QRect(470, 40, 271, 22))
    self.lineEdit.setObjectName("lineEdit")

    self.label_3 = QtWidgets.QLabel()
    self.label_3.setGeometry(QtCore.QRect(10, 90, 55, 16))
    self.label_3.setObjectName("label_3")
    self.label_3.setText("Local: ")

    self.comboBox_2 = QtWidgets.QComboBox()
    self.comboBox_2.setGeometry(QtCore.QRect(150, 80, 121, 22))
    self.comboBox_2.setObjectName("comboBox_2")
#         locales = config.get('COMBO_EMPRESAS', 'empresas')
    self.comboBox_2.addItems(obtenerLocales())

    self.pushButton = QtWidgets.QPushButton()
    self.pushButton.setGeometry(QtCore.QRect(920, 130, 93, 28))


    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(os.getcwd()+"\images\lupa2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)

    self.pushButton.setIcon(icon)
    self.pushButton.setObjectName("pushButton")
    self.pushButton.setText("Buscar")
    self.pushButton.clicked.connect(self.buscar)

    self.checkBox = QtWidgets.QCheckBox()
    self.checkBox.setGeometry(QtCore.QRect(10, 130, 211, 20))
    self.checkBox.setObjectName("checkBox")
    self.checkBox.setText("Incluir productos con stock a 0")

    mainLayout = QGridLayout()
    mainLayout.addWidget(self.label, 0, 0, 1, 1)
    mainLayout.addWidget(self.comboBox, 0, 1, 1, 1)
    mainLayout.addWidget(self.label_2, 0, 4, 1, 1)
    mainLayout.addWidget(self.lineEdit, 0, 5, 1, 1)
    mainLayout.addWidget(self.label_3, 1, 0, 1, 1)
    mainLayout.addWidget(self.comboBox_2, 1, 1, 1, 1)
    mainLayout.addWidget(self.pushButton, 3, 5, 1, 1)
    mainLayout.addWidget(self.checkBox, 3, 0, 1, 1)
    mainLayout.setColumnStretch(0, 1)
    mainLayout.setColumnStretch(3, 1)
    mainLayout.setRowStretch(0, 1)
    mainLayout.setRowStretch(3, 1)

   # mainLayout.addStretch(1)
    self.setLayout(mainLayout)

And what I want to get is this (the table is not yet implemented):

    
asked by Adriana_0_0 13.08.2018 в 13:30
source

1 answer

1

A simple solution is to place a QVBoxLayout to the widget, and in that widget place the QGridLayout, and then add the stretch with addStretch ():

from PyQt5 import QtWidgets, QtCore, QtGui


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        vboxLayout = QtWidgets.QVBoxLayout(self)

        self.label = QtWidgets.QLabel()
        self.label.setObjectName("label")
        self.label.setText("Criterio de busqueda:")

        self.comboBox = QtWidgets.QComboBox()
        self.comboBox.setObjectName("comboBox")
        self.label_2 = QtWidgets.QLabel()
        self.label_2.setObjectName("label_2")
        self.label_2.setText("Producto:")

        self.lineEdit = QtWidgets.QLineEdit()
        self.lineEdit.setObjectName("lineEdit")

        self.label_3 = QtWidgets.QLabel()
        self.label_3.setObjectName("label_3")
        self.label_3.setText("Local: ")

        self.comboBox_2 = QtWidgets.QComboBox()
        self.comboBox_2.setObjectName("comboBox_2")

        self.pushButton = QtWidgets.QPushButton()
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Buscar")

        self.checkBox = QtWidgets.QCheckBox()
        self.checkBox.setObjectName("checkBox")
        self.checkBox.setText("Incluir productos con stock a 0")

        mainLayout = QtWidgets.QGridLayout()
        mainLayout.addWidget(self.label, 0, 0, 1, 1)
        mainLayout.addWidget(self.comboBox, 0, 1, 1, 1)
        mainLayout.addWidget(self.label_2, 0, 4, 1, 1)
        mainLayout.addWidget(self.lineEdit, 0, 5, 1, 1)
        mainLayout.addWidget(self.label_3, 1, 0, 1, 1)
        mainLayout.addWidget(self.comboBox_2, 1, 1, 1, 1)
        mainLayout.addWidget(self.pushButton, 3, 5, 1, 1)
        mainLayout.addWidget(self.checkBox, 3, 0, 1, 1)
        vboxLayout.addLayout(mainLayout)
        vboxLayout.addStretch()


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

    
answered by 13.08.2018 / 13:59
source