PDA

View Full Version : Horizontally Scrolling QToolbar



zethon
19th November 2012, 20:09
I am trying to create a toolbar similar to the one seem in Reditr with horizontal scrolling buttons (it's the only app I can think of offhand that has a toolbar like what I'm trying to make, though I know I've seen it plenty other places). I am using Qt 4.8

I want my QMainWindow app to have a horizontally scrolling toolbar. I would like to have a button on the left side of the QToolbar with "<<" and a button on the right side with ">>". Each button would scroll the toolbar's items in the appropriate direction.

My initial attempt looks like:


void MainWindow::createToolbar()
{
QFrame* frame = new QFrame(this);
frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

QPushButton* leftBtn = new QPushButton("<<", frame);
QPushButton* rightBtn = new QPushButton(">>", frame);

QScrollArea* scrollArea = new QScrollArea(this);
QToolBar* container = new QToolBar(this);

// add a bunch of items to the toolbar for testing purposes
for (int i=0; i < 50; i++)
{
// settings.png is just a 24x24 icon that shows up as expected
container->addAction(QIcon(":/icons/settings.png"), "Test");
container->addSeparator();
}

scrollArea->addScrollBarWidget(container, Qt::AlignCenter);

QHBoxLayout* layout = new QHBoxLayout(this);
layout->addWidget(leftBtn);
layout->addWidget(scrollArea);
layout->addWidget(rightBtn);

frame->setLayout(layout);

QToolBar* mainToolBar = new QToolBar(tr("mainToolBar"));
mainToolBar->addWidget(frame);
this->addToolBar(mainToolBar);
}

The idea is to put the toolbar into a QScrollArea that I put inside QHBoxLayout along with the QPushButton objects I want to control the scrolling. Then I put that into a QFrame that I can then add to the QMainWindow's toolbar. However, when I try this nothing shows up in the frame (scrollarea.png attached).

If I change


layout->addWidget(leftBtn);
layout->addWidget(scrollArea);
layout->addWidget(rightBtn);

To


layout->addWidget(leftBtn);
layout->addWidget(container); // add the QToolBar to the layout instead of the QScrollArea object
layout->addWidget(rightBtn);

Then I can see the items in the QToolBar, except that the I get QToolBar's push-button on the right to show all the hidden items (container.png attached).

Right now I am not concerned with the functionality of << and >> buttons, I am mostly interested in any help I can get trying to get things to look how I want. Does anyone have any suggestions?

Added after 1 9 minutes:

Doh!

If I use


scrollArea->setWidget(container);

I get the results I'm (mostly) looking for.