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 4.8k 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 Offline
    J Offline
    JesusM97
    wrote on 27 Jun 2022, 08:43 last edited by
    #1

    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!

    J 1 Reply Last reply 27 Jun 2022, 08:45
    0
    • J JesusM97
      27 Jun 2022, 08:43

      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!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 27 Jun 2022, 08:45 last edited by
      #2

      @JesusM97 Do you use layouts?

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

      J 1 Reply Last reply 27 Jun 2022, 08:56
      0
      • J jsulm
        27 Jun 2022, 08:45

        @JesusM97 Do you use layouts?

        J Offline
        J Offline
        JesusM97
        wrote on 27 Jun 2022, 08:56 last edited by
        #3

        @jsulm yes, all the fields are inside layouts

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Jun 2022, 19:15 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 28 Jun 2022, 10:14
          0
          • N Offline
            N Offline
            ndias
            wrote on 28 Jun 2022, 08:08 last edited by
            #5

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

            J 1 Reply Last reply 28 Jun 2022, 10:19
            0
            • S SGaist
              27 Jun 2022, 19:15

              Hi,

              Which layout are you using ?

              J Offline
              J Offline
              JesusM97
              wrote on 28 Jun 2022, 10:14 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
              • N ndias
                28 Jun 2022, 08:08

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

                J Offline
                J Offline
                JesusM97
                wrote on 28 Jun 2022, 10:19 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
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 28 Jun 2022, 20:35 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 29 Jun 2022, 11:30
                  0
                  • S SGaist
                    28 Jun 2022, 20:35

                    I would suggest QFormLayout

                    J Offline
                    J Offline
                    JesusM97
                    wrote on 29 Jun 2022, 11:30 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 29 Jun 2022, 12:26 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 29 Jun 2022, 13:23
                      1
                      • S SGaist
                        29 Jun 2022, 12:26

                        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 29 Jun 2022, 13:23 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
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 29 Jun 2022, 18:29 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
                          • S SGaist
                            29 Jun 2022, 12:26

                            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 30 Jun 2022, 07:20 last edited by
                            #13

                            @SGaist I will try that, thanks!

                            1 Reply Last reply
                            0
                            • S SGaist
                              28 Jun 2022, 20:35

                              I would suggest QFormLayout

                              J Offline
                              J Offline
                              JesusM97
                              wrote on 4 Jul 2022, 07:45 last edited by JesusM97 7 Apr 2022, 07:47
                              #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
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 4 Jul 2022, 19:58 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 5 Jul 2022, 08:28
                                0
                                • S SGaist
                                  4 Jul 2022, 19:58

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

                                  J Offline
                                  J Offline
                                  JesusM97
                                  wrote on 5 Jul 2022, 08:28 last edited by JesusM97 7 May 2022, 08:28
                                  #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
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 7 Jul 2022, 11:20 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 11 Jul 2022, 10:53
                                    0
                                    • S SGaist
                                      7 Jul 2022, 11:20

                                      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 11 Jul 2022, 10:53 last edited by JesusM97 7 Nov 2022, 11:24
                                      #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
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 11 Jul 2022, 18:39 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 12 Jul 2022, 07:11
                                        0
                                        • S SGaist
                                          11 Jul 2022, 18:39

                                          Which version of PySide6 do you have ?

                                          J Offline
                                          J Offline
                                          JesusM97
                                          wrote on 12 Jul 2022, 07:11 last edited by
                                          #20

                                          @SGaist PySide6 6.3.1

                                          1 Reply Last reply
                                          0

                                          1/22

                                          27 Jun 2022, 08:43

                                          • Login

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