简体   繁体   中英

Disable scroll area in QTableView/QTableWidget

I currently have a QTableWidget into a QScrollArea and I would like to trigger the scrollbar of my scroll area when it's needed instead of the one on the TableView. I managed to disable the scrollbar of the TableView but the other one is not triggered.

    auto *scroll = new QScrollArea;
    auto *scrollWidget = new QWidget;
    auto *gridLayout = new QGridLayout(scrollWidget);

    auto *table = new QTableWidget;

    table->setRowCount(3);
    table->setColumnCount(4);

    // Disable scroll bar of the table
    table->horizontalScrollBar()->setDisabled(true);
    table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    gridLayout->addWidget(table);

    scroll->setWidget(scrollWidget);
    scroll->setWidgetResizable(true);
    scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

I think it's about the viewport of the TableView. Maybe the idea is to have the viewport adjusted to the content.

Thanks for your help.

Fixed setting the fixedWidth of the table like this :

int size{ 0 };
for (int i = 0; i < table->columnCount(); ++i)
    size += table->columnWidth(i);

table->setFixedWidth(size);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM