如何更改 QGridLayout 的小部件的大小?
How can I change the size of a QGridLayout's widgets?
取下面的代码:
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
win_w, win_h = 854, 735
self.setGeometry((1920 - win_w) // 2, (1080 - win_h) // 2, win_w, win_h)
self.setWindowTitle('Test')
self.setFont(QtGui.QFont('Times', 12))
self.central_widget()
def central_widget(self):
widget = QtWidgets.QWidget()
grid = QtWidgets.QGridLayout()
text_edit1 = QtWidgets.QTextEdit()
text_edit2 = QtWidgets.QTextEdit()
grid.addWidget(text_edit1, 0,0)
grid.addWidget(text_edit2, 0,1)
widget.setLayout(grid)
self.setCentralWidget(widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
产生这个结果:
。但是我想让左边的textEdit小一点,像这样:
即使我 setGeometry
在将它添加到布局之前(或之后),它也会被重置。
有没有办法在将小部件添加到布局后更改其几何形状?
答案取决于你是否愿意:
固定宽度:
text_edit1.setFixedWidth(100)
使其与容器的宽度成比例:[=12=]
grid.setColumnStretch(0, 2)
grid.setColumnStretch(1, 3)
取下面的代码:
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
win_w, win_h = 854, 735
self.setGeometry((1920 - win_w) // 2, (1080 - win_h) // 2, win_w, win_h)
self.setWindowTitle('Test')
self.setFont(QtGui.QFont('Times', 12))
self.central_widget()
def central_widget(self):
widget = QtWidgets.QWidget()
grid = QtWidgets.QGridLayout()
text_edit1 = QtWidgets.QTextEdit()
text_edit2 = QtWidgets.QTextEdit()
grid.addWidget(text_edit1, 0,0)
grid.addWidget(text_edit2, 0,1)
widget.setLayout(grid)
self.setCentralWidget(widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
产生这个结果:
setGeometry
在将它添加到布局之前(或之后),它也会被重置。
有没有办法在将小部件添加到布局后更改其几何形状?
答案取决于你是否愿意:
固定宽度:
text_edit1.setFixedWidth(100)
使其与容器的宽度成比例:[=12=]
grid.setColumnStretch(0, 2) grid.setColumnStretch(1, 3)