Results 1 to 1 of 1

Thread: Horizontally Scrolling QToolbar

  1. #1
    Join Date
    Nov 2012
    Qt products
    Qt4 Qt5 Qt/Embedded
    MacOS X Windows Android

    Default Re: Horizontally Scrolling QToolbar

    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:

    Qt Code:
    1. void MainWindow::createToolbar()
    2. {
    3. QFrame* frame = new QFrame(this);
    4. frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    6. QPushButton* leftBtn = new QPushButton("<<", frame);
    7. QPushButton* rightBtn = new QPushButton(">>", frame);
    9. QScrollArea* scrollArea = new QScrollArea(this);
    10. QToolBar* container = new QToolBar(this);
    12. // add a bunch of items to the toolbar for testing purposes
    13. for (int i=0; i < 50; i++)
    14. {
    15. // settings.png is just a 24x24 icon that shows up as expected
    16. container->addAction(QIcon(":/icons/settings.png"), "Test");
    17. container->addSeparator();
    18. }
    20. scrollArea->addScrollBarWidget(container, Qt::AlignCenter);
    22. QHBoxLayout* layout = new QHBoxLayout(this);
    23. layout->addWidget(leftBtn);
    24. layout->addWidget(scrollArea);
    25. layout->addWidget(rightBtn);
    27. frame->setLayout(layout);
    29. QToolBar* mainToolBar = new QToolBar(tr("mainToolBar"));
    30. mainToolBar->addWidget(frame);
    31. this->addToolBar(mainToolBar);
    32. }
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. layout->addWidget(leftBtn);
    2. layout->addWidget(scrollArea);
    3. layout->addWidget(rightBtn);
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. layout->addWidget(leftBtn);
    2. layout->addWidget(container); // add the QToolBar to the layout instead of the QScrollArea object
    3. layout->addWidget(rightBtn);
    To copy to clipboard, switch view to plain text mode 

    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:


    If I use

    Qt Code:
    1. scrollArea->setWidget(container);
    To copy to clipboard, switch view to plain text mode 

    I get the results I'm (mostly) looking for.
    Attached Images Attached Images
    Last edited by zethon; 19th November 2012 at 20:09.

Similar Threads

  1. QGraphicsView scaling horizontally
    By pssss in forum Qt Programming
    Replies: 10
    Last Post: 18th March 2011, 23:28
  2. QtabBar tabText from vertikally to horizontally
    By trusch in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2009, 19:02
  3. how to add QAction vertically and horizontally in QToolBar
    By sanjayshelke in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2008, 09:56
  4. QTreeWidget won't scroll horizontally
    By JimDaniel in forum Qt Programming
    Replies: 3
    Last Post: 23rd January 2008, 08:53
  5. Center TabBar horizontally on the TabWidget
    By forrestfsu in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2007, 14:26

Tags for this Thread


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.