PDA

View Full Version : How to make toolBar scrollable?



hasmik
7th July 2009, 09:34
Hi,

I create editor which enables user to draw different shapes. There is a centralWidget where shapes are drawn and toolbar on the right side which contains many toolButtons. ToolButtons are laid out by QVBoxLayout. I want that toolbar could be scrollable. If I use srollArea I don't get what I want.


void MainWindow::createLayerToolBar() {
...
layerToolBar = new QToolBar(tr("Layers"));
addToolBar(Qt::RightToolBarArea, layerToolBar);
...
QScrollArea * scrollArea = new QScrollArea(this);
scrollArea->setWidget(layerToolBar);
scrollArea->setAlignment(Qt::AlignCenter);
scrollArea->viewport()->setBackgroundRole(QPalette:ark);
scrollArea->viewport()->setAutoFillBackground(true);
scrollArea->setVerticalScrollBarPolicy(Qt:crollBarAlwaysOn);

}

I know that this line is written wrong: QScrollArea * scrollArea = new QScrollArea(this).
What I should change in my code that scrollArea is appeared on the right side of the central widget and contains layerToolBar?
Is there another solution?

Thanks in advance.

nish
7th July 2009, 10:21
how about adding a qscrollarea widget to a toolbar, then adding another toolbar to qscrollarea which contains the actual buttons...:)

wysota
7th July 2009, 10:36
In general that's a very bad idea. A more sane way is to use a QDockWidget instead of a toolbar. You can insert whatever you want into the dock widget (including a scrollarea).

hasmik
7th July 2009, 11:02
Thank you for your help.
I change my code as you said and get what I want.


void MainWindow::createLayerToolBar() {
...
layerToolBar = new QToolBar(tr("Layers"));
addToolBar(Qt::RightToolBarArea, layerToolBar);
...

QWidget * layerWidget = createLayerWidget();
...

QScrollArea * scrollArea = new QScrollArea;
layerToolBar->addWidget(scrollArea);
scrollArea->setWidget(layerWidget);
scrollArea->setAlignment(Qt::AlignCenter);
scrollArea->viewport()->setBackgroundRole(QPalette::Light);
scrollArea->viewport()->setAutoFillBackground(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn) ;

}

But now I don't understand the following.

If I wrote


QScrollArea * scrollArea = new QScrollArea(layerToolBar);
scrollArea->setWidget(layerWidget);

the toolBar isn't appeared.
But if I wrote


QScrollArea * scrollArea = new QScrollArea;
layerToolBar->addWidget(scrollArea);
scrollArea->setWidget(layerWidget);

everything is fine. Why???

nish
7th July 2009, 11:39
because toolbar only shows those widgets which are added as its child using addWidget(),

new QScrollArea(layerToolBar) will simply make layerToolBar the parent of the scrollarea but will not show it,.

btw.. i never thought you will take my advice and implement it.... i am surprised...:)

hasmik
7th July 2009, 12:24
Thank you wysota. Your suggested solution gives me better result.



void MainWindow::createLayerDockWidget() {

...

QWidget * layerWidget = createLayerWidget();

QDockWidget *dock = new QDockWidget(tr(" Layers "), this);
dock->setBackgroundRole(QPalette::Light);
dock->setAutoFillBackground(true);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

QScrollArea * scrollArea = new QScrollArea;
dock->setWidget(scrollArea);
scrollArea->setWidget(layerWidget);
scrollArea->setAlignment(Qt::AlignTop);
scrollArea->viewport()->setBackgroundRole(QPalette::Light);
scrollArea->viewport()->setAutoFillBackground(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

addDockWidget(Qt::RightDockWidgetArea, dock);

}



Thank you MrDeath too. Don't surprise I am new in Qt.