August 5, 2011

yuriyb yuriyb
Lab Rat
5 posts

Stretchable QToolBar

 

Hi Guys,

I have created two QToolBar in the top of QMainWindow. Example:

  1.    QToolBar * leftToolBar = new QToolBar (this);
  2.     QToolBar * rightToolBar = new QToolBar(this);
  3.  
  4.     leftToolBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  5.     rightToolBar->setFixedWidth(150);
  6.  
  7.     leftToolBar->setMovable(false);
  8.     rightToolBar->setMovable(false);
  9.  
  10.     addToolBar(leftToolBar);
  11.     addToolBar(rightToolBar);

But left tool bar has size according to its contents, and right tool bar stuck to left. Any combination with tool bars policy does not work.

Is there any way to stretch left tool bar ? May be exists some workaround ? Or it’s bug in framework ?

Thanks in advance.

12 replies

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts

Insert widget with QSizePolicy::Expanding into left toolbar using QToolBar::addWidget()

  1. QWidget *separator = new QWidget(this);
  2. separator->setSizePolicy(QSizePolicy::Expanding,
  3.                          QSizePolicy::Expanding);
  4. toolBar->addWidget(separator);

By the way, such convenience method as QToolBar::addStretch() would be nice. I’ve had this problem myself, some time ago.

 Signature 

Programmer is an organism that can turn caffeine into code.

August 5, 2011

yuriyb yuriyb
Lab Rat
5 posts
chernetsov0 wrote:
Insert widget with QSizePolicy::Expanding into left toolbar using QToolBar::addWidget()…. I’ve had this problem myself, some time ago.

I tried this one and I saw this solution before, but in case with two bars on one line it doesn’t work.

Sorry, just forgot… Right bar has ‘RightToLeft’ layout direction.

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts
yuriyb wrote:
Right bar has ‘RightToLeft’ layout direction.

Why do you use that?

 Signature 

Programmer is an organism that can turn caffeine into code.

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts

If you want some widgets to be in the far right of the toolbar – just join two toolbar into one and insert that separator between actions of the two.

 Signature 

Programmer is an organism that can turn caffeine into code.

August 5, 2011

yuriyb yuriyb
Lab Rat
5 posts
chernetsov0 wrote:
If you want some widgets to be in the far right of the toolbar – just join two toolbar into one and insert that separator between actions of the two.

How I can join two toolbars ?

I need tool bar like Google Chorme bookmarks bar, where exist place for link buttons and one right button with menu (in chrome ‘other bookmarks’ tool button).

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts

I’ve tried your solution and buttons on right toolbar seems to be on far right, so what is the problem?

 Signature 

Programmer is an organism that can turn caffeine into code.

August 5, 2011

yuriyb yuriyb
Lab Rat
5 posts

Button on far right because ‘Right to Left’ direction forces tool buttons appear in other order, but I want stretch left bar as far as possible. In my case right tool bar will always contain only one button.

Take a look:

screen

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts

I tried almost everything and still no result.
Workaround exists, but requires a lot of extra work.

  1. QToolBar *toolBar = new QToolBar(this);
  2. toolBar->setMovable(false);
  3.  
  4. QWidget *separator = new QWidget(this);
  5. separator->setSizePolicy(QSizePolicy::Expanding,
  6.                          QSizePolicy::Expanding);
  7.  
  8. toolBar->addAction("Bookmark 1");
  9. toolBar->addAction("Bookmark 2");
  10. toolBar->addWidget(separator);
  11. toolBar->addAction("More Bookmarks");

Dynamically updating tool bar contents will be trickery of another kind (that’s the hard part).

 Signature 

Programmer is an organism that can turn caffeine into code.

August 5, 2011

yuriyb yuriyb
Lab Rat
5 posts

Solution with stretcher before button isn’t suitable because in case of toolbar overfill, the right button will be invisible. In my application this tool bar filled from item model and handle signals from model to overload.

I’ve tried dirty hack with overload resizeEvent in main window and set fixed width for toolbar dinamically, but behaviour also isn’t suitable because in time of resizng main window in some cases hangs.

So, do you have any other idea ? As I saw on other forums this problem not just for me.

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts

Actually, maybe it’s not a bug but rather lack of feature. By default last toolbar get all the toolbar area space. And AFAIK there is no way to change that behavior.
If you remove setMovable(false) statements and try to drag that toolbar right, you will see it’s possible to do such a thing at the run time.
So how about trying not to resize toolbar in resizeEvent implementation but moving it?

 Signature 

Programmer is an organism that can turn caffeine into code.

August 5, 2011

chernetsov0 chernetsov0
Lab Rat
17 posts

By the way, right-to-left layout has nothing to do with issue.
It must, ideally, work without it fine.

 Signature 

Programmer is an organism that can turn caffeine into code.

August 6, 2011

yuriyb yuriyb
Lab Rat
5 posts

I was solve this problem in my app by another way. I removed border in style sheet and handle context menu requests for both tool bar in one point.

But would not be bad to see correct handling of QToolBar::sizePolicy by framework in next release.

Anyway thanks for your help.

 
  ‹‹ Labeled menu separators      [SOLVED]Want to expand QTextEdit .. ››

You must log in to post a reply. Not a member yet? Register here!