Home > Net >  Is there a way to only colour part of a bottom border in QT without QPainter?
Is there a way to only colour part of a bottom border in QT without QPainter?

Time:12-27

I can change the top, left, right and bottom of borders using the setStyleSheet funcion:

self.button1.setStyleSheet("""border-bottom: 1px solid #654321; border-top: 1px solid #123456""")

and this creates a button like so:

enter image description here

However is it possible to create borders like the one below using qss stylesheets:

enter image description here

Where the bottom border in only not starting at the very edge.

CodePudding user response:

Well, it is possible, but only whenever the following aspects are respected and considered:

  • both vertical borders are always explicitly hidden (border-left and border-right are set to none);
  • the border radius is only specified for the bottom corners, and it only sets the horizontal radius, while leaving the vertical one to 0;
  • this is a "hack" that specifically uses geometry aspects and only works if the above are respected;
    bottomMargin = 24
    self.button1.setStyleSheet("""
        QAbstractButton {{
            border: 1px solid black;
            border-left: none;
            border-right: none;
            border-bottom-left-radius: {margin}px 0;
            border-bottom-right-radius: {margin}px 0;
        }}
    """.format(margin=bottomMargin)
  • Related