5

I tried the CSS code:

QTableView QHeaderView::section {
    text-align: center;
 }

And it did not help me.

PS I write the SCADA system in the WinCC OA that uses Qt, and there you can change the appearance of components only using CSS

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
Max Lich
  • 221
  • 2
  • 10

2 Answers2

4

As seen in the source code for QHeaderView::paintSection

QVariant textAlignment = d->model->headerData(logicalIndex, d->orientation,
                                              Qt::TextAlignmentRole);
opt.rect = rect;
opt.section = logicalIndex;
opt.state |= state;
opt.textAlignment = Qt::Alignment(textAlignment.isValid()
                                  ? Qt::Alignment(textAlignment.toInt())
                                  : d->defaultAlignment);

QHeaderView does not uses the stylesheet to define the text alignment, only values from the defaultAlignment or the data from the header's model (Qt::TextAlignmentRole).

Now, if your default alignment is either Qt::AlignLeft or Qt::AlignRight, you can use the section padding to automatically center it:

QTableView QHeaderView::section {
    padding-left: auto;
    padding-right: auto;
}

The opposite is not true: if default alignment is center, other calculus affect how padding is used and cannot be used to automatically left- or right-align text.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
3

I have found one of the solutions:

QHeaderView {
    qproperty-defaultAlignment: AlignHCenter AlignVCenter;
}

Although it works in WinCC OA only partially.

Max Lich
  • 221
  • 2
  • 10