Hi,
I see this message every time I show a menu inside a QToolButton in a QToolBar:
QToolButton: Menu in setMenu() overriding actions set in addAction!
What I want to do is a button which can change function (to switch the selection mode of a QGraphicsView).
In the code there is tool button for "scroll" mode and a tool button for "select" mode wich contains a menu with "select intersected" and "select contained".
All the 3 actions are mutually exclusive.
The code works very well and does exactly what I needed, but I don't like that message which is shown each time I click to show the menu (not when I click an action).
Here is the code used to create them:
// Create the toolbar
m_selectionToolbar->setOrientation(Qt::Horizontal);
m_selectionToolbar->setWindowTitle(tr("Selection toolbar", "Toolbar"));
// Group mutually exclusive actions
// Use mouse to scroll the view
m_actionScrollHand
= new QAction(actionGroup
);
m_actionScrollHand->setCheckable(true);
m_actionScrollHand->setText(tr("&Scroll", "Action"));
m_actionScrollHand->setStatusTip(tr("Drag to scroll the draw area", "Action"));
m_actionScrollHand
->setIcon
(QIcon(":/images/move.png"));
connect(m_actionScrollHand, SIGNAL(triggered()), this, SLOT(actionScrollHand_triggered()));
m_selectionToolbar->addAction(m_actionScrollHand);
// Use mouse to select items
m_selectionModeMenu
= new QMenu;
m_actionIntersectsItem
= new QAction(actionGroup
);
m_actionIntersectsItem->setCheckable(true);
m_actionIntersectsItem->setText(tr("Select &intersected", "Action"));
m_actionIntersectsItem->setStatusTip(tr("Drag to select items intersected by the rubber band", "Action"));
m_actionIntersectsItem
->setIcon
(QIcon(":/images/selectintersected.png"));
connect(m_actionIntersectsItem, SIGNAL(triggered()), this, SLOT(actionIntersectsItem_triggered()));
m_selectionModeMenu->addAction(m_actionIntersectsItem);
m_actionContainsItem
= new QAction(actionGroup
);
m_actionContainsItem->setCheckable(true);
m_actionContainsItem->setText(tr("Select &contained", "Action"));
m_actionContainsItem->setStatusTip(tr("Drag to select items contained by the rubber band", "Action"));
m_actionContainsItem
->setIcon
(QIcon(":/images/selectcontained.png"));
connect(m_actionContainsItem, SIGNAL(triggered()), this, SLOT(actionContainsItem_triggered()));
m_selectionModeMenu->addAction(m_actionContainsItem);
m_selectionModeButton
->setPopupMode
(QToolButton::MenuButtonPopup);
m_selectionModeButton->setMenu(m_selectionModeMenu);
// Set default action
m_selectionModeButton->setDefaultAction(m_actionIntersectsItem);
m_actionIntersectsItem->setChecked(true);
m_selectionToolbar->addWidget(m_selectionModeButton);
// Create the toolbar
m_selectionToolbar = new QToolBar;
m_selectionToolbar->setOrientation(Qt::Horizontal);
m_selectionToolbar->setWindowTitle(tr("Selection toolbar", "Toolbar"));
// Group mutually exclusive actions
QActionGroup *actionGroup = new QActionGroup(this);
// Use mouse to scroll the view
m_actionScrollHand = new QAction(actionGroup);
m_actionScrollHand->setCheckable(true);
m_actionScrollHand->setText(tr("&Scroll", "Action"));
m_actionScrollHand->setStatusTip(tr("Drag to scroll the draw area", "Action"));
m_actionScrollHand->setIcon(QIcon(":/images/move.png"));
connect(m_actionScrollHand, SIGNAL(triggered()), this, SLOT(actionScrollHand_triggered()));
m_selectionToolbar->addAction(m_actionScrollHand);
// Use mouse to select items
m_selectionModeMenu = new QMenu;
m_actionIntersectsItem = new QAction(actionGroup);
m_actionIntersectsItem->setCheckable(true);
m_actionIntersectsItem->setText(tr("Select &intersected", "Action"));
m_actionIntersectsItem->setStatusTip(tr("Drag to select items intersected by the rubber band", "Action"));
m_actionIntersectsItem->setIcon(QIcon(":/images/selectintersected.png"));
connect(m_actionIntersectsItem, SIGNAL(triggered()), this, SLOT(actionIntersectsItem_triggered()));
m_selectionModeMenu->addAction(m_actionIntersectsItem);
m_actionContainsItem = new QAction(actionGroup);
m_actionContainsItem->setCheckable(true);
m_actionContainsItem->setText(tr("Select &contained", "Action"));
m_actionContainsItem->setStatusTip(tr("Drag to select items contained by the rubber band", "Action"));
m_actionContainsItem->setIcon(QIcon(":/images/selectcontained.png"));
connect(m_actionContainsItem, SIGNAL(triggered()), this, SLOT(actionContainsItem_triggered()));
m_selectionModeMenu->addAction(m_actionContainsItem);
m_selectionModeButton = new QToolButton;
m_selectionModeButton->setPopupMode(QToolButton::MenuButtonPopup);
m_selectionModeButton->setMenu(m_selectionModeMenu);
// Set default action
m_selectionModeButton->setDefaultAction(m_actionIntersectsItem);
m_actionIntersectsItem->setChecked(true);
m_selectionToolbar->addWidget(m_selectionModeButton);
To copy to clipboard, switch view to plain text mode
And here are the slots:
void GraphicsDrawView::actionScrollHand_triggered()
{
}
void GraphicsDrawView::actionIntersectsItem_triggered()
{
m_drawView->setRubberBandSelectionMode(Qt::IntersectsItemShape);
m_selectionModeButton->setDefaultAction(m_actionIntersectsItem);
}
void GraphicsDrawView::actionContainsItem_triggered()
{
m_drawView->setRubberBandSelectionMode(Qt::ContainsItemShape);
m_selectionModeButton->setDefaultAction(m_actionContainsItem);
}
void GraphicsDrawView::actionScrollHand_triggered()
{
m_drawView->setDragMode(QGraphicsView::ScrollHandDrag);
}
void GraphicsDrawView::actionIntersectsItem_triggered()
{
m_drawView->setDragMode(QGraphicsView::RubberBandDrag);
m_drawView->setRubberBandSelectionMode(Qt::IntersectsItemShape);
m_selectionModeButton->setDefaultAction(m_actionIntersectsItem);
}
void GraphicsDrawView::actionContainsItem_triggered()
{
m_drawView->setDragMode(QGraphicsView::RubberBandDrag);
m_drawView->setRubberBandSelectionMode(Qt::ContainsItemShape);
m_selectionModeButton->setDefaultAction(m_actionContainsItem);
}
To copy to clipboard, switch view to plain text mode
Here instead are the variables (which are in the header):
QPointer< DrawView > m_drawView; // Inherits QGraphicsView
QPointer<
QToolBar > m_selectionToolbar;
QMenu *m_selectionModeMenu;
QPointer< DrawView > m_drawView; // Inherits QGraphicsView
QPointer< QToolBar > m_selectionToolbar;
QAction *m_actionScrollHand;
QAction *m_actionIntersectsItem;
QAction *m_actionContainsItem;
QToolButton *m_selectionModeButton;
QMenu *m_selectionModeMenu;
To copy to clipboard, switch view to plain text mode
Bookmarks