Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Widget overlap eachother in GridLayout

I am trying to create a simple Program using GridLayout, all the widgets inside the QWidget window do show up and scale properly however the LineEdit overlaps the Title Label of the window.

from PySide2 import QtWidgets, QtCore, QtGui
import sys

class SimGrid(QtWidgets.QWidget):
    def __init__(self):
        super(SimGrid, self).__init__()
        self.setWindowTitle("My attempt at Grid Layout")
        grid = QtWidgets.QGridLayout()
        self.setLayout(grid)

        title = QtWidgets.QLabel("This is some big sample text to fill up")
        title.setAlignment(QtCore.Qt.AlignHCenter)
        text_edit = QtWidgets.QTextEdit()
        success = QtWidgets.QPushButton("Success", self)
        cancel = QtWidgets.QPushButton("Cancel", self)

        grid.addWidget(title, 0, 0, 0, 0)
        grid.addWidget(text_edit, 1, 0, 1, 2)
        grid.addWidget(success, 4, 0)
        grid.addWidget(cancel, 4, 1)
        self.show()
like image 463
Sahil Avatar asked Nov 24 '25 11:11

Sahil


1 Answers

According to the docs you are using the following method:

void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = ...)

The third and fourth parameters indicate the quantities and columns that will occupy, respectively, in your case the title will occupy 0 rows and 0 columns which is incorrect.

Using these criteria what you want is the following:

import sys
from PySide2 import QtWidgets, QtCore, QtGui


class SimGrid(QtWidgets.QWidget):
    def __init__(self):
        super(SimGrid, self).__init__()
        self.setWindowTitle("My attempt at Grid Layout")
        grid = QtWidgets.QGridLayout()
        self.setLayout(grid)

        title = QtWidgets.QLabel("This is some big sample text to fill up")
        title.setAlignment(QtCore.Qt.AlignHCenter)
        text_edit = QtWidgets.QTextEdit()
        success = QtWidgets.QPushButton("Success", self)
        cancel = QtWidgets.QPushButton("Cancel", self)

        grid.addWidget(title, 0, 0, 1, 2)
        grid.addWidget(text_edit, 1, 0, 1, 2)
        grid.addWidget(success,2, 0, 1, 1)
        grid.addWidget(cancel,  2, 1, 1, 1)
        self.show()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = SimGrid()
    w.show()
    sys.exit(app.exec_())

enter image description here

like image 165
eyllanesc Avatar answered Nov 27 '25 01:11

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!