Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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()":http://doc.qt.nokia.com/latest/qmainwindow.html#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
    {
    Q_OBJECT
    public:
    MainWindow()
    {
    QDockWidget *dock1 = new QDockWidget(this);
    dock1->setWindowTitle("Dock 1");
    dock1->setWidget(new QTextEdit(dock1));
    QToolBar *tool = new QToolBar(this);
    tool->setWindowTitle("Tool");
    tool->addAction("Test action");

    addDockWidget(Qt::TopDockWidgetArea, dock1);
    addToolBar(tool);

    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;
    window.show();
    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