QListWidget detect Scrollbars visibility

  • Hi all

    Is there an easy way to read if a vertical or horizontal scrollbar is visible?

    if i try

     // QListWidget *list exists and has several items
    qDebug() << list->verticalScrollBar()->isVisible();

    i always get false, even if the Scrollbar is visible.

    Any hints what i done wrong?


    typo in Code

  • @the_
    How about something like this:


    We got the same problem, still don't know the difference though

  • Moderators

    when/where do you call this code?

  • @jpalbertini

    I tryed that, it says true, so this region is empty

  • @raven-worx

    I call it after adding items to the list to cut off long text to avoid horizontal scroll bars.

  • Moderators

    right after adding the items?
    If yes the view most probably hasn't reacted on it yet. So either you do your check when the view layouts or you force the view to layout before calling your code which checks the scrollbar visibility.

    Probably the most easiest way to force a relayout of QScrollAreas children (scrollbars, viewport, etc) is:

    QEvent event( QEvent::LayoutRequest );
    QApplication::sendEvent( listView, &event );
    // check the scrollbar visibility here afterwards

  • @raven-worx
    yes i tried it right after adding the list items.

    As i figured out, this does not work if you add items to a QListWidget that is already visible. Adding items and then set the widget visible returns then correct values.

    Triggering a relayout did not work for me when the widget is already visible.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.