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. QComboBox can't set margins with setContentsMargins()
Qt 6.11 is out! See what's new in the release blog

QComboBox can't set margins with setContentsMargins()

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 995 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 last edited by
    #1

    Hello, I have a window with a QVBoxLayout and inside it I have some QHBoxLayout to set my widgets. All the widgets on the left have a Qt.AlignRight and all the widgets on the right have a Qt.AlignLeft so they are centered. Then I added some margins to separate a bit right widgets from left widgets. To do that I use setContentMargins() but for the QComboBox it doesn't work.

    ensayo.PNG

                #Here I create the first QLabel and the QLineEdit
                hlayout = QHBoxLayout()
                self.ensayoLabel = QLabel("Nombre del ensayo")
                self.nombreEnsayoInput = QLineEdit("")
                self.nombreEnsayoInput.setFixedSize(150, 25)
                #I set some left margin to the QLineEdit so it is a bit separated from the QLabel
                self.nombreEnsayoInput.setContentsMargins(10, 0, 0, 0)
                hlayout.addWidget(self.ensayoLabel)
                hlayout.addWidget(self.nombreEnsayoInput)
                hlayout.setAlignment(self.ensayoLabel, Qt.AlignRight)
                hlayout.setAlignment(self.nombreEnsayoInput, Qt.AlignLeft)
    
                layout.addLayout(hlayout) #This is the vertical layout of the window
               .
               .
               .
               .
    
                #Here I create a QCheckBox and a QComboBox
                h4layout = QHBoxLayout()
                self.rotacionArenaCheckBox = QCheckBox("Rotar arena")
                self.rotacionArenaCheckBox.stateChanged.connect(lambda: self.rotacionArenaCheckboxChanged())
                self.direccionRotacionArenaCombobox = QComboBox()
                self.direccionRotacionArenaCombobox.addItem("Ateatorio")
                self.direccionRotacionArenaCombobox.addItem("Norte")
                self.direccionRotacionArenaCombobox.addItem("Sur")
                self.direccionRotacionArenaCombobox.addItem("Este")
                self.direccionRotacionArenaCombobox.addItem("Oeste")
                #I add some left margin to the QComboBox to separate it a bit from the QCheckBox,  but it doesn't work
                self.direccionRotacionArenaCombobox.setContentsMargins(10, 0, 0, 0)
                self.direccionRotacionArenaCombobox.setEnabled(False)
                h4layout.addWidget(self.rotacionArenaCheckBox)
                h4layout.addWidget(self.direccionRotacionArenaCombobox)
                h4layout.setAlignment(self.rotacionArenaCheckBox, Qt.AlignRight)
                h4layout.setAlignment(self.direccionRotacionArenaCombobox, Qt.AlignLeft)
    
      
    
    

    What is happening?

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

      Hi,

      From the looks of it, you could simplify your code using 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 1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        The first 4 rows can be laid out with grid layout. Then align widgets in the first column to the right and the widgets in the second column to the left.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          From the looks of it, you could simplify your code using QFormLayout.

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

          @SGaist said in QComboBox can't set margins with setContentsMargins():

          Hi,

          From the looks of it, you could simplify your code using QFormLayout.

          @JoeCFD said in QComboBox can't set margins with setContentsMargins():

          The first 4 rows can be laid out with grid layout. Then align widgets in the first column to the right and the widgets in the second column to the left.

          Hello, thanks for yous answers, I will try both solution and let you know the results.

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

            After some tries, using QGridLayout for the first 4 rows and QHBoxLayout for the rest is the best solution as I need multiple columns in some rows. I get this result but now I want to add some vertical space between grids to separate them a bit. setContentMargins() does not work neither in QGridLayout. What can I use?
            ensayo.PNG
            Thanks

            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