Important: Please read the Qt Code of Conduct -

How to set top margin to QScrollBar in QTableView.

  • Hello
    I have an object of QTableView with horizontal header view, and vertical scroll bar.
    the problem is that scroll slider goes with header view also, I want to set properties to scrollbar to be visible it only under header view. I tried this but it doesn't affects.

      QScrollScrollBar *vscroll = new QScrollBar(this);
      vscroll -> setAttribute(Qt::WA_OpaquePaintEvent, false);
      vscroll -> setContentsMargins(0, 20, 0, 0);
      tableView -> setVerticalScrollBar(vscroll);

    any idea?

  • Would something like this work?

    QScrollScrollBar *vscroll = tableView -> verticalScrollBar();
    vscroll->setMaximumHeight(tableView->height() - tableView->horizontalHeader()->height());
    vscroll->move(tableView->horizontalHeader()->height(), vscoll->y());

    Else you could try it with stylesheets?

  • Moderators

    i think you wont be successful this way. Since the item view widget just layouts it's widgets (viewport, scrollbars, headerviews, ...) this way. And this happens on every resize and on some other actions.
    And this is hidden in the private part of the item view widget.

    And also this is caused already by class definition:
    The scrollbars come from the QAbstractScrollArea and the header view comes from the specific item view. So they are more or less unrelated.

  • @raven-worx: while I'm not sure it is the right solution for what @tokafr wants to do, he can take ownership of the vertical scrollbar and have some influence this way. I have done it before that way.

    @tokafr: Instead of trying to influence the hidden layout of the QTableView, you could mask the space between the header view and the vertical scrollbar to make it look like it being part of the header view. Stylesheets are probably the best place to try.