Stanton
Stanton

Reputation: 1076

Get QScrollBar Width upon change in QScrollBar Visibility

I'm trying to get the width of a qscrollbar as it changes visibility. I'm doing this because I want to change the width of my qtablewidget by the width of the qscrollbar if it is visible.

I'm using the rangeChanged(int min, int max) signal from my qscrollbar. If max > min then the qscrollbar is visible. My problem is that when I get the qscrollbar width using m_tableWidget->verticalScrollBar()->width() the value is not the current width, but rather the previous width before the rangeChanged signal was fired.

This is what my signal-slot connection looks like:

connect(m_tableWidget->verticalScrollBar(), SIGNAL(rangeChanged(int,int)),
        this, SLOT(verticalScrollBarChanged(int,int)));

And this is the slot function:

void ImageTableWidget::verticalScrollBarChanged(int min, int max)
{
    int verticalScrollBarWidth = 0;
    if (max-min)
        verticalScrollBarWidth = m_tableWidget->verticalScrollBar()->width();

    int horizontalHeaderWidth = m_tableWidget->horizontalHeader()->length();
    m_tableWidget->setFixedWidth(verticalScrollBarWidth + horizontalHeaderWidth);
}

Does anyone know how I could obtain the new qscrollbar width at the time it becomes visible/invisible?

Upvotes: 0

Views: 874

Answers (1)

Alexander V
Alexander V

Reputation: 8698

Qt has a better way of taking care of such async resize of some widget container area. You should probably remove all the code that explicitly takes care of it and instead add this:

// m_tableWidget constructed
// at least first parameter for horizontal sizing should be Expanding. 
m_tableWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

And for that table widget class it makes sense to overload resizeEvent virtual method which will be triggered with every resize and do the cells adjust from there or resizeColumnsToContents().

Upvotes: 1

Related Questions