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
Forum Updated to NodeBB v4.3 + New Features

Adding border with stylesheet changes QLineEdit default size

Scheduled Pinned Locked Moved Unsolved Qt for Python
22 Posts 5 Posters 5.0k 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.
  • 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
                      • SGaistS SGaist

                        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.

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

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

                        1 Reply Last reply
                        0

                        • Login

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