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