Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QScrollArea doesn't dynamically create Scroll Bars



  • Hi, I created a QScrollArea widget and placed some labels on it. However when the texts are longer, it doesn't dynamically add a scrollbar. How do I add it manually? Thanks in anticipation


  • Lifetime Qt Champion

    Hi
    Right click a free area on the ScrollArea and apply layout from menu.
    It should then show scrollbars.
    alt text



  • @mrjj hi, thanks for your time so far. am quite confused, because when I do it in designer mode it works , however when I do it by coding it doesn't work. Am placing the scrollArea on Qstackedwidget.
    This describes what I did :
    vlayout-> addwidget(mylabel);
    scrollarea->setlayout(vlayout);
    pagelayout->add widget(scrollarea); //QVBox object
    page->set layout(pagelayout);//stackedwidget's pahr


  • Qt Champions 2019

    @gblessed You can check the generated code when using the designer to compare it to your own code.



  • I had a similar problem earlier. This is the relevant code in my main():

      MainPanel *mainPanel = new MainPanel;
    
      QScrollArea *area = new QScrollArea;
      area->setWidget(mainPanel);
      area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      area->setMaximumSize(mainPanel->size() + QSize(2, 0));
      area->setMinimumHeight(mainPanel->height());
    
      MainWindow *mainWindow = new MainWindow;
      mainWindow->setCentralWidget(area);
      mainWindow->show();
    

    Two notes:
    MainWindow inherits QMainWindow.
    The addition of QSize(2,0) is only there because otherwise the scroll bar never disappeared.

    Now, the problem I had was that MainPanel contains an clickable image but QScrollArea didn't detect the size of it. That is until I added this in the MainWindow constructor:

        QImage image(getPanelImage().c_str());
        setMinimumSize(image.size());
        setMaximumSize(image.size());
    

    Those two last lines are probably what you're looking for. Don't know if this is the RIGHT way to do it, but it worked. ;)


Log in to reply