Adding item to toolbar context menu

  • Is there a way to add an item to the context menu that is used for showing/hiding QDockWidgets and QToolbars. I have a widget that I don't think makes sense as either of these that I would like to hide/show using this menu.

  • Added an Action to your context menu connected to a slot of your QMainWindow where you can call void removeDockWidget ( QDockWidget * dockwidget ) or void removeToolBar ( QToolBar * toolbar )

  • The widget is neither a dockwidget nor a toolbar.

  • where the widget is inserted? inside of a QLayout? (QLayout has a removeWidget(QWidget*) )

    did you tried QWidget::setVisible ( bool visible ) method?

  • What I don't know how to do is add something to the applications built in context menu

  • You can reimplement "QMainWindow::createPopupMenu()": and return your own menu. Alternatively, you can add to the existing context menu and return that one. For example:


    #include <QtGui>

    class MainWindow : public QMainWindow
    QDockWidget *dock1 = new QDockWidget(this);
    dock1->setWindowTitle("Dock 1");
    dock1->setWidget(new QTextEdit(dock1));
    QToolBar *tool = new QToolBar(this);
    tool->addAction("Test action");

    addDockWidget(Qt::TopDockWidgetArea, dock1);

    setCentralWidget(new QTextEdit(this));

    QMenu* createPopupMenu ()
    QMenu *menu = QMainWindow::createPopupMenu();
    menu->addAction("An added action");
    return menu;


    #include "main.moc"

    int main(int argc, char** argv)
    QApplication app(argc, argv);
    MainWindow window;;
    return app.exec();


    Does this give you what you need?

  • Thanks sigrid, that looks like what I'm looking for.

  • Is it possible to add items to the taskbar context menu? I am referring
    to the menu produced when right clicking on the time within Windows XP
    (ie "Toolbars" to Properties").

    I want to add items onto this menu, but have not found a guide on how to
    do this yet (I expect there is a registry entry required to do this). I
    would either like to add items to this menu or edit the existing items
    on the menu.

    Does anyone know how to do this?

  • It is not possible to add items to the taskbar context menu using Qt.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.