QListView Mac

If you have lists or tables in your application you can see that Qt widgets have scroll bar for full height of widget instead of only viewport area. What to do to make it looks like native mac widget? It is easy. Remember that QAbstractScrollArea has method: addScrollBarWidget()

So, we create small widget, apply header style and put it in scroll bar area.

QWidget * w = new QToolButton;
w->setFixedHeight(horizontalHeader()->height());
w->setAutoFillBackground(true);
w->setStyleSheet("QToolButton {\
        border: 1px solid #a0a0a0;\
        border-top: 0px;\
        border-left: 0px;\
        border-right: 0px;\
        background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,\
            stop:0 #ffffff, stop:0.5 #e0e0e0, stop:1 #ffffff);\
    }");
addScrollBarWidget(w, Qt::AlignTop);

Because this is QToolButton, you can expand functionality later to have for example menu to select visible columns, etc.

But if you would like to have it system-independent and apply system QStyle, I recommend to sub-class QToolButton (or QWidget, as you wish) and implement such paintEvent() method:

void ColumnSelectorButton::paintEvent(QPaintEvent * pe)
{
    int w = width();
    int h = height();
#ifdef Q_WS_MAC
    w++;
#endif

    QPainter p(this);
    p.setClipRect(pe->rect());

    QStyleOptionHeader opt;
    opt.initFrom(m_header);
    opt.state |= QStyle::State_Horizontal |
                 QStyle::State_Enabled |
                 QStyle::State_Raised;
    opt.rect = QRect(0,0, w,h);
    style()->drawControl(QStyle::CE_Header, &opt, &p, m_header);
    p.end();
}

Note that because you paint with drawControl, you do not need to apply style sheet to this widget anymore. Instead it uses even style sheet of QHeaderView automatically, and this is great. (I applied w++ on Mac because else it paints right border line from style sheet of header, see my previous blog entry about headers)