fiz
fiz

Reputation: 934

How to add a Scroll Bar to a Grid Layout in Qt?

I have a QGridLayout which will contain a bunch of widgets. The problem arose when the loop added too many widgets and they couldn't all fit on the page. I want to add a scroll bar but it doesn't display correctly.

This function returns a tab which is added to the main layout, it contains the grid layout:

QTabWidget *RegistersTab::createTab()
{
    QTabWidget *tab = new QTabWidget(this);

    std::vector<QGridLayout*> loVec; //to add to master layout

    for(int i=0; i<2; i++) //number of pages
    {
        QWidget *client = new QWidget(this); //this part breaks it
        QScrollArea *scrollArea = new QScrollArea(this);
        scrollArea->setWidget(client);

        QTabWidget *tabPage = new QTabWidget(client);

        QGridLayout *loGrid = new QGridLayout(client);
        tabPage->setLayout(loGrid);

        QString title = QString("Page %1").arg(i);
        tab->addTab(tabPage, title);

        loVec.push_back(loGrid);
    }
    m_loGridVec.push_back(loVec);

    return tab;
}

The GridLayout vector is there so I can add widgets and manipulate it later. At the moment I just get a grey box over the top of my tabs - so something is broken. If I remove the scroll area and set (client) to (this).

I'm guessing there's a simple correction to be made?

EDIT (how tab is made):

ui->lo->addWidget(m_tab);

m_tab->addTab(createTab(), title); // m_tabCbc is a QTabWidget;

Upvotes: 6

Views: 13536

Answers (2)

thuga
thuga

Reputation: 12901

You're not adding your scroll area anywhere. It's going to be inside the QTabWidget (this).

As you mentioned, you want nested tabs. So you need to add a page for the tabPage widget and add the scroll area inside its layout.

It should be something like this:

tabPage 
    => pageWidget(QWidget)
        => layout 
            => scrollArea
                => scrollAreaWidget(client?)
                    => layout(loGrid)

 

QWidget *client = new QWidget;
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(client);
QGridLayout *loGrid = new QGridLayout;
client->setLayout(loGrid);

QTabWidget *tabPage = new QTabWidget;
QWidget *pageWidget = new QWidget;
pageWidget->setLayout(new QVBoxLayout);
pageWidget->layout()->addWidget(scrollArea);
tabPage->addTab(pageWidget, "Page");    

QString title = QString("Page %1").arg(i);
tab->addTab(tabPage, title);

Upvotes: 9

Daniel
Daniel

Reputation: 1447

I think you need a simple QWidget (e.g. 'client'), set its Layout to your GridLayout (to have the gridLayout "bundled" in a widget). Then put that Widget into a Scroll Area (which will add the desired scroll bar behavior), which you then add to the main Widget you want to return.

edit: I think in some cases you're using the parenting mechanism in a wrong way, i.e.

QWidget *client = new QWidget(this); //creates a new widget which has "this" as its parent, meaning it will be deleted if "this" is destroyed
QGridLayout *loGrid = new QGridLayout(client); //a layout with client as its parent
QGridLayout *loGrid2 = new QGridLayout();
loGrid2->addWidget(xyz)
client->setLayout(loGrid2);  //I think this is what you want

Upvotes: 0

Related Questions