5

Kaveish offers a nice piece of code to work with on this How does designer create a Line widget? How would I change the color of those lines to blue from their black default color.

Thanks for any help

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
E Purdy
  • 143
  • 1
  • 1
  • 9

2 Answers2

3

There are a couple of different approaches for this, depending on your need. I'm going to post examples in Python, but porting to C++ is simple enough.

Set the QPalette

If you want to keep the look of the current style, you could try to set the palette colors. This works fine on Linux with the default Breeze and Oxygen styles:

palette = self.line.palette()
palette.setColor(palette.Window, QtGui.QColor(QtCore.Qt.lightGreen)
self.line.setPalette(palette)

Unfortunally not all theme/styles are implemented in the same way, and both the Windows and Fusion styles need to set the color for the palette.Dark role, which makes the whole solution very platform and style dependent, as an user could have his/her own styles installed.

Stylesheet

The stylesheet solution requires to implement the border properties of the QFrame widget subclass, but doesn't look always very good and it's better to setFixedHeight (or width for vertical lines) to 2 pixel maximum.

self.line.setStyleSheet('''
    MyLineClass {
        border: 0.5px solid green;
        border-style: inset;
    }
''')

QPaintEvent

Then there's the custom paint solution. This is a simple implementation that obtains an effect similar to the Oxygen "smooth" lines.

class MyHorizontalLine(QtWidgets.QWidget):
    grad = QtGui.QLinearGradient(0, 0, 1, 0)
    grad.setCoordinateMode(self.grad.StretchToDeviceMode)
    grad.setCoordinateMode(self.grad.ObjectBoundingMode)
    grad.setColorAt(0, QtGui.QColor(QtCore.Qt.transparent))
    grad.setColorAt(.5, QtGui.QColor(QtCore.Qt.darkGray))
    grad.setColorAt(1, QtGui.QColor(QtCore.Qt.transparent))
    gradPen = QtGui.QPen(QtGui.QBrush(grad), 1)

    def paintEvent(self, event):
        qp = QtGui.QPainter(self)
        qp.setRenderHints(qp.Antialiasing)
        qp.translate(.5, self.rect().center().y() - .5)
        qp.setPen(self.gradPen)
        qp.drawLine(0, 0, self.width(), 0)
        qp.translate(0, 1)
        qp.setOpacity(.5)
        qp.drawLine(0, 0, self.width(), 0)
musicamante
  • 41,230
  • 6
  • 33
  • 58
1

Update: Following @VaderB's comment, color attribute works:

QFrame *verticalLine = new QFrame(this);
verticalLine->setFrameShape(QFrame::VLine);
verticalLine->setStyleSheet("color: rgb(221, 221, 221)");

Putting StyleSheet works for me. For example, in Designer:
#line, #line_2 {
    background-color: rgb(0, 0, 255);
}

Here, line and line_2 are lines (: And in widget class:

line->setStyleSheet(QString("background-color: #0000FF;"));
Bobur
  • 545
  • 1
  • 6
  • 21
  • It doesn't work. This will change the _background color_, not the _color_ of the line. – Federico Rubbi Sep 01 '18 at 23:03
  • the code works, I tested. It changes **background color** of the **line** which is the **color** of the **line** :) – Bobur Sep 03 '18 at 05:53
  • I tested this and it changed the color of the area containing the line instead of the line color. – Federico Rubbi Sep 03 '18 at 10:14
  • @FedericoRubbi, It seems you are right. If you use it in a layout, it just gives an impression of changed line color. You can use in the same way. I will update my answer – Bobur Sep 04 '18 at 02:02
  • 1
    Just use `color` attribute, like this: `#line{color: white;}` – Vader B Jan 16 '20 at 17:37