Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Adding border with stylesheet changes QLineEdit default size
Qt 6.11 is out! See what's new in the release blog

Adding border with stylesheet changes QLineEdit default size

Scheduled Pinned Locked Moved Unsolved Qt for Python
22 Posts 5 Posters 17.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JesusM97

    Hello, I am adding a red border to a QLineEdit if a field is mandatory and empty but, when I apply the stylesheet, the default size of the QLineEdit changes. How can I keep the default size?

    This is the stylesheet code:

    border-style: solid;border-width: 2px;border-color: red
    

    This is the default QLineEdit size:
    QLineEditDefault.PNG

    And this is the styled one:
    QLineEditStyled.PNG

    I am using PySide2, thanks!

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @JesusM97 Do you use layouts?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    J 1 Reply Last reply
    0
    • jsulmJ jsulm

      @JesusM97 Do you use layouts?

      J Offline
      J Offline
      JesusM97
      wrote on last edited by
      #3

      @jsulm yes, all the fields are inside layouts

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        Which layout are you using ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • ndiasN Offline
          ndiasN Offline
          ndias
          wrote on last edited by
          #5

          Hi @JesusM97 ,
          Can you share a code snippet illustrating the issue?
          Regards

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Which layout are you using ?

            J Offline
            J Offline
            JesusM97
            wrote on last edited by JesusM97
            #6

            @SGaist I am using QVBoxLayout and QHBoxLayout
            Here it is a look before applying stylesheet:
            QLineEditDefault.PNG

            and after:
            QLineEditStyled.PNG

            1 Reply Last reply
            0
            • ndiasN ndias

              Hi @JesusM97 ,
              Can you share a code snippet illustrating the issue?
              Regards

              J Offline
              J Offline
              JesusM97
              wrote on last edited by
              #7

              @ndias of course, for example, I have this setup:

                      layout = QVBoxLayout()
              
                      hlayout = QHBoxLayout()
                      self.trialNameLabel = QLabel("Nombre del ensayo")
                      self.trialNameLabel.setToolTip("Nombre con el que quieres identificar el ensayo")
                      self.trialNameInput = QLineEdit()
                      self.trialNameInput.textChanged.connect(self.trialNameInputChanged)
              
                      self.trialIdLabel = QLabel("ID del ensayo")
                      self.trialIdLabel.setToolTip("Identificador del ensayo")
                      self.trialIdInput = QLineEdit()
              
                      hlayout.addWidget(self.trialNameLabel)
                      hlayout.addWidget(self.trialNameInput)
                      hlayout.addWidget(self.trialIdLabel)
                      hlayout.addWidget(self.trialIdInput)
                      hlayout.setAlignment(self.trialNameLabel, Qt.AlignRight)
                      hlayout.setAlignment(self.trialNameInput, Qt.AlignLeft)
                      hlayout.setAlignment(self.trialIdLabel, Qt.AlignRight)
                      hlayout.setAlignment(self.trialIdInput, Qt.AlignLeft)
                      hlayout.setContentsMargins(0, 0, 0, 5)
                      layout.addLayout(hlayout)
                      self.setLayout(layout)
              

              This code represents this row:
              Example.PNG

              At the end of the window I have an OK button. When this button is clicked I check if all the mandatory fields are filled and, if not, I change their stylesheets:

               if not self.trialNameInput.text():
                          self.trialNameInput.setStyleSheet("border-style: solid;border-width: 2px;border-color: red")
              

              Example 2.PNG

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                I would suggest QFormLayout

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                J 2 Replies Last reply
                0
                • SGaistS SGaist

                  I would suggest QFormLayout

                  J Offline
                  J Offline
                  JesusM97
                  wrote on last edited by
                  #9

                  @SGaist Do you mean QFormLayout inside QHBoxLayout ir order to have multiple rows? As far as I know QFormLayout only allows to place fields vertically within the layout.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    That's that, one QFormLayout for the left "colum" of fields and one for the right and finally both inside the QHBoxLayout.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    JoeCFDJ J 2 Replies Last reply
                    1
                    • SGaistS SGaist

                      That's that, one QFormLayout for the left "colum" of fields and one for the right and finally both inside the QHBoxLayout.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #11

                      @SGaist Is not GridLayout simpler? And set a minimum width for the qlineedits which can be referenced with font size.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        For that kind of input, QFormLayout does the job just fine.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          That's that, one QFormLayout for the left "colum" of fields and one for the right and finally both inside the QHBoxLayout.

                          J Offline
                          J Offline
                          JesusM97
                          wrote on last edited by
                          #13

                          @SGaist I will try that, thanks!

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            I would suggest QFormLayout

                            J Offline
                            J Offline
                            JesusM97
                            wrote on last edited by JesusM97
                            #14

                            @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.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              Can you provide a complete minimal script that shows that behaviour ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Can you provide a complete minimal script that shows that behaviour ?

                                J Offline
                                J Offline
                                JesusM97
                                wrote on last edited by JesusM97
                                #16

                                @SGaist Of course. I created a QMainWindow with a QLineEdit and a QPushButton. 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:
                                DefaultStyle.PNG

                                and when I click the button this is what happens:
                                RedBorder.PNG

                                This doesn't happen if I remove:

                                formLayout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)
                                

                                but I need it

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  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.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  J 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    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.

                                    J Offline
                                    J Offline
                                    JesusM97
                                    wrote on last edited by JesusM97
                                    #18

                                    @SGaist Really? I tried the example with PySide6 and I have the same result. Did you add

                                    formLayout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)
                                    

                                    to your form layout? If not it doesn't happen.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      Which version of PySide6 do you have ?

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      J 1 Reply Last reply
                                      0
                                      • SGaistS SGaist

                                        Which version of PySide6 do you have ?

                                        J Offline
                                        J Offline
                                        JesusM97
                                        wrote on last edited by
                                        #20

                                        @SGaist PySide6 6.3.1

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #21

                                          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.

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          J 1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved