Gino
Gino

Reputation: 15

How to expand width of QSpinBox buttons

I created a small GUI in QtCreator and had it converted to python to execute on a Raspberry Pi. issue is the QSpinBox buttons are only scaled by QSpinBox height. how can i increase the width of the buttons in the python code?

here is the python code that was generated for one of the boxes, SpinBox_7:

    self.spinBox_7 = QtWidgets.QSpinBox(self.gridLayoutWidget)
    self.spinBox_7.setMinimumSize(QtCore.QSize(0, 50))
    palette = QtGui.QPalette()
    brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
    brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.HighlightedText, brush)
    brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight, brush)
    brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.HighlightedText, brush)
    brush = QtGui.QBrush(QtGui.QColor(51, 153, 255))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, brush)
    brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.HighlightedText, brush)
    self.spinBox_7.setPalette(palette)
    font = QtGui.QFont()
    font.setPointSize(20)
    font.setBold(False)
    font.setWeight(50)
    self.spinBox_7.setFont(font)
    self.spinBox_7.setAlignment(QtCore.Qt.AlignCenter)
    self.spinBox_7.setMaximum(12)
    self.spinBox_7.setSingleStep(2)
    self.spinBox_7.setObjectName("spinBox_7")
    self.gridLayout.addWidget(self.spinBox_7, 2, 3, 1, 1)

Upvotes: 1

Views: 3028

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

One possible solution is to set the widths using Qt StyleSheet to the down-button and up-button of the QSpinBox:

QSpinBox::down-button{
    width: 50
}
QSpinBox::up-button{
    width: 50
}

To use it there are at least the following 3 options:

  1. To use a Qt Application you must have a QApplication, then you must set it there:

    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet("""
    QSpinBox::down-button{
        width: 50
    }
    QSpinBox::up-button{
        width: 50
    }""")
    
  2. Apply it to each QSpinBox:

    self.spinBox_7.setStyleSheet("""
       QSpinBox::down-button{
        width: 50
    }
    QSpinBox::up-button{
        width: 50
    }""")
    
  3. Use Qt Designer, to do this right click on the QSpinBox and select the option "Change StyleSheet ..", a dialog will open and there you must place the stylessheet as shown in the following image and press the ok button.

enter image description here

Upvotes: 4

Related Questions