Right aligning a button in a QToolBar
Sometimes you will want to add a right-aligned button to a QToolBar. The first thing that probably comes to mind is to add a QSpacerItem to the toolbar, but that won’t work as QSpacerItem is not a child of QWidget, so you can’t use the addWidget() member of QToolBar. Instead, make a basic QWidget, set its sizePolicy to Expanding, and add that widget to your QToolBar. For example:
QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// toolBar is a pointer to an existing toolbar
toolBar->addWidget(spacer);
toolBar->addAction("Right-aligned button");
If you end up using a lot of these spacers you could even create a subclass.
Comments
Simon Greenwold
Saturday, August 8, 2009 @ 12:48
Ulf
Thursday, December 3, 2009 @ 06:25
ken ampel
Friday, February 12, 2010 @ 14:54
mistik1
Friday, January 21, 2011 @ 11:11
richardwb
Monday, January 24, 2011 @ 00:05
Ramiz
Monday, February 21, 2011 @ 01:41
developer
Thursday, April 7, 2011 @ 02:42
jingwei
Tuesday, August 30, 2011 @ 05:50
blueliuyun
Wednesday, November 23, 2011 @ 04:02