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