QWidget* ToolAction::createWidget( QWidget *parent ) { QToolBar *toolBar = qobject_cast< QToolBar * >( parent ); if ( !toolBar ) return 0; QToolButton *button = new QToolButton( toolBar ); button->setAutoRaise( true ); button->setFocusPolicy( Qt::NoFocus ); button->setIconSize( toolBar->iconSize() ); button->setToolButtonStyle( toolBar->toolButtonStyle() ); button->setPopupMode( QToolButton::DelayedPopup ); button->setMenu( new QMenu( button ) ); button->setCheckable( true ); connect(toolBar, &QToolBar::iconSizeChanged, button, &QToolButton::setIconSize); connect(toolBar, &QToolBar::toolButtonStyleChanged, button, &QToolButton::setToolButtonStyle); connect(button, &QToolButton::triggered, toolBar, &QToolBar::actionTriggered); connect( button->menu(), &QMenu::triggered, this, &ToolAction::slotNewDefaultAction ); m_buttons.append( button ); if ( !m_actions.isEmpty() ) { button->setDefaultAction( m_actions.first() ); foreach ( QAction *action, m_actions ) { button->menu()->addAction( action ); if ( action->isChecked() ) button->setDefaultAction( action ); }
QWidget *KToolBarPopupAction::createWidget(QWidget *_parent) { QToolBar *parent = qobject_cast<QToolBar *>(_parent); if (!parent) { return QWidgetAction::createWidget(_parent); } QToolButton *button = new QToolButton(parent); button->setAutoRaise(true); button->setFocusPolicy(Qt::NoFocus); button->setIconSize(parent->iconSize()); button->setToolButtonStyle(parent->toolButtonStyle()); button->setDefaultAction(this); connect(parent, &QToolBar::iconSizeChanged, button, &QAbstractButton::setIconSize); connect(parent, &QToolBar::toolButtonStyleChanged, button, &QToolButton::setToolButtonStyle); connect(button, &QToolButton::triggered, parent, &QToolBar::actionTriggered); if (d->delayed) if (d->stickyMenu) { button->setPopupMode(QToolButton::MenuButtonPopup); } else { button->setPopupMode(QToolButton::DelayedPopup); } else { button->setPopupMode(QToolButton::InstantPopup); } return button; }
void tst_QToolBar::toolButtonStyle() { { QToolBar tb; QSignalSpy spy(&tb, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle))); // no-op QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); tb.setToolButtonStyle(Qt::ToolButtonIconOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(spy.count(), 0); tb.setToolButtonStyle(Qt::ToolButtonTextOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonTextOnly); QCOMPARE(spy.count(), 1); spy.clear(); // no-op tb.setToolButtonStyle(Qt::ToolButtonTextOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonTextOnly); QCOMPARE(spy.count(), 0); tb.setToolButtonStyle(Qt::ToolButtonIconOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(spy.count(), 1); spy.clear(); // no-op tb.setToolButtonStyle(Qt::ToolButtonIconOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(spy.count(), 0); tb.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonTextBesideIcon); QCOMPARE(spy.count(), 1); spy.clear(); // no-op tb.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonTextBesideIcon); QCOMPARE(spy.count(), 0); tb.setToolButtonStyle(Qt::ToolButtonTextUnderIcon); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonTextUnderIcon); QCOMPARE(spy.count(), 1); spy.clear(); // no-op tb.setToolButtonStyle(Qt::ToolButtonTextUnderIcon); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonTextUnderIcon); QCOMPARE(spy.count(), 0); tb.setToolButtonStyle(Qt::ToolButtonFollowStyle); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonFollowStyle); QCOMPARE(spy.count(), 1); } { QMainWindow mw; QToolBar tb; QSignalSpy mwSpy(&mw, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle))); QSignalSpy tbSpy(&tb, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle))); mw.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); // explicitly set the tb to the default tb.setToolButtonStyle(Qt::ToolButtonIconOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(tbSpy.count(), 0); mw.addToolBar(&tb); // tb icon size should not change since it has been explicitly set QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(tbSpy.count(), 0); mw.setToolButtonStyle(Qt::ToolButtonIconOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(tbSpy.count(), 0); mw.setToolButtonStyle(Qt::ToolButtonTextOnly); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(tbSpy.count(), 0); mw.setToolButtonStyle(Qt::ToolButtonTextUnderIcon); QCOMPARE(tb.toolButtonStyle(), Qt::ToolButtonIconOnly); QCOMPARE(tbSpy.count(), 0); // note: there is no way to clear the explicitly set tool // button style... once you explicitly set it, the toolbar // will never follow the mainwindow again } }