Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

scrollbar

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;
like image 247
fiz Avatar asked Jan 08 '15 11:01

fiz


1 Answers

You're not adding your scroll area anywhere. Its 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);
like image 175
thuga Avatar answered Nov 11 '22 05:11

thuga