Adding border with stylesheet changes QLineEdit default size
-
I would suggest QFormLayout
-
That's that, one QFormLayout for the left "colum" of fields and one for the right and finally both inside the QHBoxLayout.
-
That's that, one QFormLayout for the left "colum" of fields and one for the right and finally both inside the QHBoxLayout.
-
For that kind of input, QFormLayout does the job just fine.
-
That's that, one QFormLayout for the left "colum" of fields and one for the right and finally both inside the QHBoxLayout.
-
I would suggest QFormLayout
@SGaist I tried with QFormLayout and I have same problem.
I found that with QFormLayout the problem appears when I add this line of code:formLayout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)
but I don't want the fields to grow so I need that line.
-
Can you provide a complete minimal script that shows that behaviour ?
-
@SGaist Of course. I created a
QMainWindow
with aQLineEdit
and aQPushButton
. When I press the button, there is a function that checks if the QLineEdit is empty. If it is empty it changes its border to red but it also changes the field size:import sys from PySide2 import QtWidgets from PySide2.QtCore import Qt from PySide2.QtWidgets import QMainWindow, QFormLayout, QLineEdit, QWidget, QVBoxLayout, QPushButton def checkEmpty(lineEdit): if lineEdit.text() == "": lineEdit.setStyleSheet("border-style: solid;border-width: 2px;border-color: red") if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) main_window = QMainWindow() centalWidget = QWidget() main_window.setCentralWidget(centalWidget) verticalLayout = QVBoxLayout() formLayout = QFormLayout() inputEdit = QLineEdit() formLayout.addRow("Input", inputEdit) formLayout.setRowWrapPolicy(QFormLayout.DontWrapRows) formLayout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint) formLayout.setFormAlignment(Qt.AlignHCenter | Qt.AlignTop) verticalLayout.addLayout(formLayout) okButton = QPushButton("OK") okButton.clicked.connect(lambda: checkEmpty(inputEdit)) verticalLayout.addWidget(okButton) centalWidget.setLayout(verticalLayout) main_window.show() app.exec_()
When I run the app this is what it shows:
and when I click the button this is what happens:
This doesn't happen if I remove:
formLayout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)
but I need it
-
Can you test that with PySide6 ?
I tried it on macOS and the QLineEdit just changed the border without the changing it size with the sample script you posted. -
Can you test that with PySide6 ?
I tried it on macOS and the QLineEdit just changed the border without the changing it size with the sample script you posted. -
Which version of PySide6 do you have ?
-
Yes, I used your example verbatim.
It looks like it might be something Windows specific.
I also monkeypatched sizeHint to return a custom value and its properly called twice (once at startup and once when clicking the OK button) and size stays the same. 6.3.0 and 6.3.1 are consistent.
-
Yes, I used your example verbatim.
It looks like it might be something Windows specific.
I also monkeypatched sizeHint to return a custom value and its properly called twice (once at startup and once when clicking the OK button) and size stays the same. 6.3.0 and 6.3.1 are consistent.
@SGaist Well, so I will try to get the sizeHint at startup and then try to set the value after modifying the border. Maybe this will work.
EDIT: I have tried that and it works.
When I create the
QLineEdit
, I set its fixed size based on its size hint.verticalLayout = QVBoxLayout() formLayout = QFormLayout() inputEdit = QLineEdit() size = inputEdit.sizeHint() inputEdit.setFixedSize(size) formLayout.addRow("Input", inputEdit)
Now, when I change the QLineEdit Stylesheet the field size doesn't change.