2

Take the following code:

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

Which produces this result:
result. But I would like to have the textEdit on the left to be smaller, like this:
this Even if I setGeometry before (or after) adding it to the layout, it is reset.

Is there any way to change the geometry of a widget after adding it to a layout?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Have a nice day
  • 1,007
  • 5
  • 11
  • If there's a layout manager, using `setGeometry()` is pointless (the layout *manages* sizes and positions). Read more about [stretch factors](https://doc.qt.io/qt-5/layout.html#stretch-factors). – musicamante Apr 20 '21 at 18:59
  • @musicamante how do I apply the stretch? do I apply it to the widget or the layout? I didn't notice anything in the link addressing that. – Have a nice day Apr 20 '21 at 19:03
  • @Haveaniceday The big question is: how width should the element on the left side be? Should it be a fixed width or should it be a proportion of the width of the window? – eyllanesc Apr 20 '21 at 19:10
  • @eyllanesc I'm not sure in the long run but for now I want fixed width, if possible – Have a nice day Apr 20 '21 at 19:11

1 Answers1

2

The answer depends if you want:

  • a fixed width:

    text_edit1.setFixedWidth(100)
    
  • make it a proportion of the width of the container:

    grid.setColumnStretch(0, 2) 
    grid.setColumnStretch(1, 3)
    
eyllanesc
  • 235,170
  • 19
  • 170
  • 241