PDA

View Full Version : How do I determine if a QTableWidget's scrollbar is visible?



fuzzywuzzy01
27th August 2007, 23:33
How do I determine if my vertical scrollbar is going to be displayed at the time I'm setting up my dialog? In Qt3 MyTable->verticalScrollBar()->isHidden() always worked for me. I've tried isEnabled(), isHidden() and isVisible() but they seem to return the same thing regardless of how many items are in the table. The scroll bar policy is set to as needed. I imagine isVisible would work after the dialog is displayed but that is too late.

Also does anyone know which of the following lines is the preferable way to determine the scroll bar width? They both seem to work on my dev machine.
int ScrollBarWidth1 = ui.MyTable->verticalScrollBar()->sizeHint().width();
int ScrollBarWidth2 = style()->pixelMetric(QStyle::PM_ScrollBarExtent);

Thanks!

fuzzywuzzy01
29th August 2007, 00:47
So I never found a built in way to figure this out. I ended up sub-classing QTableWidget and writing my own query to see if it was visible. Here it is for anyone else who runs into this problem.



bool ExtTable::IsVerticalScrollBarVisible()
{
bool IsVisible = false;

int HeightOfAllRows = 0;
for (int i = 0; i < rowCount(); i++)
HeightOfAllRows += rowHeight(i);

int HeaderHeight = horizontalHeader()->height();
int TableHeight = height();

if ( (HeightOfAllRows + HeaderHeight) > TableHeight )
IsVisible = true;

return IsVisible;
}

raven-worx
9th September 2011, 15:10
better late than never ;) :

since view->horizontalHeader() returns a (subclass of) QWidget you can just use isVisible()

jgver
23rd February 2012, 16:42
Even later, even better! ;)

I ran into this problem as well, and I hope this will help somebody. I found that if the scroll bar's maximum and minimum values are the same, (usually with the value 0), then the scroll bar will not be visible.