Widget 在 GridLayout 中相互重叠
Widget overlap eachother in GridLayout
我正在尝试使用 GridLayout
创建一个简单的程序,QWidget
window 中的所有小部件确实显示并正确缩放,但是 LineEdit
与window 的标题 Label
。
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()
根据docs您正在使用以下方法:
void QGridLayout::addWidget(QWidget *widget, int fromRow, int
fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment =
...)
第三个和第四个参数分别表示将占用的数量和列,在您的情况下,标题将占用 0 行和 0 列,这是不正确的。
使用这些条件你想要的是:
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_())
我正在尝试使用 GridLayout
创建一个简单的程序,QWidget
window 中的所有小部件确实显示并正确缩放,但是 LineEdit
与window 的标题 Label
。
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()
根据docs您正在使用以下方法:
void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = ...)
第三个和第四个参数分别表示将占用的数量和列,在您的情况下,标题将占用 0 行和 0 列,这是不正确的。
使用这些条件你想要的是:
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_())