PDA

View Full Version : QToolBar: How do you suppress the right-click menu that allows hiding the toolbar?



ChrisW67
10th June 2010, 07:27
How do you suppress the right-click menu that allows hiding a QToolBar ? The tool bars are part of a QMainWindow if that makes a difference.

Should be a very simple thing to do but my searches in the documentation have drawn a blank. I can see where the label text can be set and can set it to an empty string but the right-click option remains.

Cheers,
Chris W

aamer4yu
10th June 2010, 07:35
You can override QWidget::contextMenuEvent and ignore the event.
That should I guess.

ChrisW67
10th June 2010, 08:33
I fear it is too late by the time a context menu event has been sent. You did give me a hint that lead to:


m_ui->toolBar->setContextMenuPolicy(Qt::NoContextMenu);
// or
m_ui->toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
which I thought might work. The first but didn't seem to have any effect. The second stopped a context menu if clicked on the tool bar, but a context menu allowing hiding the tool bar would still come up if the right-click happened over the menu bar. I had to do:


setContextMenuPolicy(Qt::NoContextMenu);
on the QMainWindow to stop both. Seems that the context menus on the menu and tool bars of a main window are delegated upward.

agathiyaa
10th June 2010, 09:26
Just Override QMenu * QMainWindow::createPopupMenu ()

QMenu * MyMainWindow::createPopupMenu ()
{
return NULL;
}

frankiefrank
6th November 2012, 15:02
Found a nice answer on StackOverflow. Just call:


setContextMenuPolicy(Qt::NoContextMenu);

inside your QMainWindow subclass.

http://stackoverflow.com/questions/650889/qtoolbar-is-there-a-way-to-make-toolbar-unhidable

ChrisW67
6th November 2012, 21:42
That information was already in this thread two years ago ;)