You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Latem

Intermediate

  • "Latem" is male
  • "Latem" started this thread

Posts: 278

Location: New Brunswick, Canada

Occupation: Student/Programmer

  • Send private message

1

Friday, December 23rd 2005, 10:00pm

Resize toolbar buttons?

Hello,

I have a main window with a dock witget. Inside the dock widget I have a Toolbox. Each toolbox item has a vertical toolbar to which I add actions. However it doesnt look the best. I have attached the screenshot. You can see the toolbar button is only big enough as the text. This usually makes sense for a button, and even toolbar button, since toolbars are usually horizontal. In reality the texts in the buttons will not be all the same lenght like they are in the image. This results in a rather ugly looking dockwidget, with all the buttons having varying sizes. Is there any way to make all the buttons stretch horizontally so that they fill 100% of the toolbar, all the way to the right side. I have tried a few things trying to force a layout onto toolbar, and playing with size policies; but all without sucess. This is done in Qt4, which I am stil getting familiar with. Any help is much appreciated.

Latem
Latem has attached the following file:
  • toolbar2.png (9.23 kB - 372 times downloaded - latest: Mar 23rd 2013, 2:11pm)
The march of progress:
C:
printf("%10.2f", x);
C++:
cout << setw(10) << setprecision(2) << showpoint << x;
Java:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Friday, December 23rd 2005, 10:18pm

RE: Resize toolbar buttons?

Did you try changing the horizontal size policy to Expanding? Do you have a layout set for the toolbox page?

Latem

Intermediate

  • "Latem" is male
  • "Latem" started this thread

Posts: 278

Location: New Brunswick, Canada

Occupation: Student/Programmer

  • Send private message

3

Friday, December 23rd 2005, 11:13pm

Here's the code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void MainWindow::createToolbox()
{
	QDockWidget *dw = new QDockWidget( tr("Toolbox"), this );
	dw->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
	dw->setFeatures(QDockWidget::AllDockWidgetFeatures);
	m_settingsMenu->addAction(dw->toggleViewAction());
        
	addDockWidget( Qt::LeftDockWidgetArea, dw );
    
	m_toolBox = new QToolBox( dw );
	
	dw->setWidget( m_toolBox );
	dw->setMinimumWidth( 110 );
	
	for(int groupNumber = 0; groupNumber < m_nGroups; groupNumber++)
	{
		QToolBar* tb = new QToolBar(m_toolBox);
		tb->setOrientation(Qt::Vertical);
		tb->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
		
		/*QVBoxLayout *layout = new QVBoxLayout();
		tb->setLayout(layout);
		tb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);*/

		m_toolBox->addItem(tb, m_groupIcons.at(groupNumber), m_groupNames.at(groupNumber));

		for(int actionNumber = 0; actionNumber < m_groups[groupNumber].size(); actionNumber++)
		{
			tb->addAction(m_groups[groupNumber].at(actionNumber));
		}
	}    		

	dw->show();
}


I have tried the code in the comment. Or just setting the size policty, and no layout. I also tried using MinimumExpanding. That had to effect on the buttons. I think thhe size policy stuff acts on the toolbar, in respect to its parent. not how the widgets in the toolbar will look. Also when I set the layout, it prints:

Source code

1
QWidget::setLayout() : QLayout "" added to QToolBar "", which already has a layout


Not sure if this is good or bad.

Were you refering to this? Or what widget?

I can achieve the desired look if I use QPushButtons. This is the code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void MainWindow::createToolbox3()
{
	QDockWidget *dw = new QDockWidget( tr("Toolbox"), this );
	dw->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
	dw->setFeatures(QDockWidget::AllDockWidgetFeatures);
	m_settingsMenu->addAction(dw->toggleViewAction());
        
	addDockWidget( Qt::LeftDockWidgetArea, dw );
    
	m_toolBox = new QToolBox( dw );
	
	dw->setWidget( m_toolBox );
	dw->setMinimumWidth( 110 );
	
	for(int groupNumber = 0; groupNumber < m_nGroups; groupNumber++)
	{
		QFrame* frame = new QFrame(m_toolBox);
		frame->setFrameStyle(QFrame::NoFrame);
		frame->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);

		QVBoxLayout *layout = new QVBoxLayout();

		m_toolBox->addItem(frame, m_groupIcons.at(groupNumber), m_groupNames.at(groupNumber));

		for(int actionNumber = 0; actionNumber < m_groups[groupNumber].size(); actionNumber++)
		{
			QPushButton* btn = new QPushButton(m_groups[groupNumber].at(actionNumber)->icon(), m_groups[groupNumber].at(actionNumber)->text(), frame);
			btn->setToolTip(m_groups[groupNumber].at(actionNumber)->toolTip());
			btn->setStatusTip(m_groups[groupNumber].at(actionNumber)->statusTip());
			btn->addAction(m_groups[groupNumber].at(actionNumber));
			connect(btn, SIGNAL(clicked()), m_groups[groupNumber].at(actionNumber), SLOT(trigger()));
		
			layout->addWidget(btn);
		}
		frame->setLayout(layout);
		
	}    		

	dw->show();
}


However, this isn't as nice code as just having the actions. It looks a little different, than with toolbar, but it is ok. Also there is some weird little drawing artifact when I do it this way. See the attached image. It goes away after all the toolbox items have been clicked. Not the individual buttons, just the groups.

I am not really opposed to doing it this way. But if I can get the buttons to stretch in a toolbar, I'd rather use that. So if there is a way I'd like to know.

Thanks,

Latem
Latem has attached the following file:
  • toolbar3.png (8.35 kB - 326 times downloaded - latest: Mar 23rd 2013, 2:13pm)
The march of progress:
C:
printf("%10.2f", x);
C++:
cout << setw(10) << setprecision(2) << showpoint << x;
Java:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

4

Saturday, December 24th 2005, 12:01am

Quoted

Originally posted by Latem

Source code

1
QWidget::setLayout() : QLayout "" added to QToolBar "", which already has a layout
Not sure if this is good or bad.

It's bad.

Quoted

Were you refering to this? Or what widget?

To the QFrame from your second example.

Quoted

I can achieve the desired look if I use QPushButtons.

Use QToolButtons instead.

Try something like this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void MainWindow::createToolbox3()
{
   ...	
   for(int groupNumber = 0; groupNumber < m_nGroups; groupNumber++)
   {
      ...
      for(int actionNumber = 0; actionNumber < m_groups[groupNumber].size(); actionNumber++)
      {
         QToolButton* btn = new QToolButton( frame );
         btn->setDefaultAction( m_groups[groupNumber].at(actionNumber) );
         btn->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
         layout->addWidget(btn);
      }
      frame->setLayout(layout);
   }    		
   dw->show();
}

Latem

Intermediate

  • "Latem" is male
  • "Latem" started this thread

Posts: 278

Location: New Brunswick, Canada

Occupation: Student/Programmer

  • Send private message

5

Saturday, December 24th 2005, 12:47am

OOhh right; I forgot about, or missed the QToolButton. Ok that is better. I think actually I'll use that.

Thanks jacek,

Latem
The march of progress:
C:
printf("%10.2f", x);
C++:
cout << setw(10) << setprecision(2) << showpoint << x;
Java:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

This post has been edited 1 times, last edit by "Latem" (Dec 24th 2005, 12:48am)