Results 1 to 6 of 6

Thread: How to make toolBar scrollable?

  1. #1
    Join Date
    Jul 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default How to make toolBar scrollable?

    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.

    Qt Code:
    1. void MainWindow::createLayerToolBar() {
    2. ...
    3. layerToolBar = new QToolBar(tr("Layers"));
    4. addToolBar(Qt::RightToolBarArea, layerToolBar);
    5. ...
    6. QScrollArea * scrollArea = new QScrollArea(this);
    7. scrollArea->setWidget(layerToolBar);
    8. scrollArea->setAlignment(Qt::AlignCenter);
    9. scrollArea->viewport()->setBackgroundRole(QPalette:ark);
    10. scrollArea->viewport()->setAutoFillBackground(true);
    11. scrollArea->setVerticalScrollBarPolicy(Qt:crollBarAlwaysOn);
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by wysota; 7th July 2009 at 10:09. Reason: missing [code] tags

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make toolBar scrollable?

    how about adding a qscrollarea widget to a toolbar, then adding another toolbar to qscrollarea which contains the actual buttons...

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make toolBar scrollable?

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make toolBar scrollable?

    Thank you for your help.
    I change my code as you said and get what I want.

    Qt Code:
    1. void MainWindow::createLayerToolBar() {
    2. ...
    3. layerToolBar = new QToolBar(tr("Layers"));
    4. addToolBar(Qt::RightToolBarArea, layerToolBar);
    5. ...
    6.  
    7. QWidget * layerWidget = createLayerWidget();
    8. ...
    9.  
    10. QScrollArea * scrollArea = new QScrollArea;
    11. layerToolBar->addWidget(scrollArea);
    12. scrollArea->setWidget(layerWidget);
    13. scrollArea->setAlignment(Qt::AlignCenter);
    14. scrollArea->viewport()->setBackgroundRole(QPalette::Light);
    15. scrollArea->viewport()->setAutoFillBackground(true);
    16. scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn) ;
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    But now I don't understand the following.

    If I wrote

    Qt Code:
    1. QScrollArea * scrollArea = new QScrollArea(layerToolBar);
    2. scrollArea->setWidget(layerWidget);
    To copy to clipboard, switch view to plain text mode 

    the toolBar isn't appeared.
    But if I wrote

    Qt Code:
    1. QScrollArea * scrollArea = new QScrollArea;
    2. layerToolBar->addWidget(scrollArea);
    3. scrollArea->setWidget(layerWidget);
    To copy to clipboard, switch view to plain text mode 

    everything is fine. Why???
    Last edited by wysota; 7th July 2009 at 10:11. Reason: missing [code] tags

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make toolBar scrollable?

    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...

  6. #6
    Join Date
    Jul 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make toolBar scrollable?

    Thank you wysota. Your suggested solution gives me better result.

    Qt Code:
    1. void MainWindow::createLayerDockWidget() {
    2.  
    3. ...
    4.  
    5. QWidget * layerWidget = createLayerWidget();
    6.  
    7. QDockWidget *dock = new QDockWidget(tr(" Layers "), this);
    8. dock->setBackgroundRole(QPalette::Light);
    9. dock->setAutoFillBackground(true);
    10. dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    11.  
    12. QScrollArea * scrollArea = new QScrollArea;
    13. dock->setWidget(scrollArea);
    14. scrollArea->setWidget(layerWidget);
    15. scrollArea->setAlignment(Qt::AlignTop);
    16. scrollArea->viewport()->setBackgroundRole(QPalette::Light);
    17. scrollArea->viewport()->setAutoFillBackground(true);
    18. scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    19.  
    20. addDockWidget(Qt::RightDockWidgetArea, dock);
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. Trouble while viewing a created a toolbar!!
    By aditya.kaole in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2009, 21:04
  2. Toolbar in dockwidget
    By AD in forum Qt Programming
    Replies: 7
    Last Post: 2nd October 2008, 14:34
  3. unable to hide combobox or spinbox in toolbar
    By Sandip in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2008, 11:52
  4. Window OS make distclean && qmake && make one line
    By patrik08 in forum General Programming
    Replies: 4
    Last Post: 22nd March 2007, 10:43
  5. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.