create ms word-like context menu



  • i want to create a custom context menu where i'll have something like this (the "paste options" and the icon below).
    alt text

    i tried using QWidgetAction:

    QMenu *contextMenu = new QMenu(this);
    
    QWidgetAction *widget = new QWidgetAction(contextMenu);
    widget->setIcon(QIcon(":/icon_path"));
    widget->setText(QString("Paste Options:"));
    
    contextMenu->insertAction(beforeActionPtr, widget);
    

    but it doesn't actually do what i want.

    i thought i could make a QActionGroup and add the action to the group, and then set the action group as widget's default widget via:

    widget->setDefaultWidget(actionGroup);
    

    but QActionGroup is derived from QObject so it wasn't a good idea.

    how can i do this?


  • Moderators

    @user4592357
    Almost right way. You are just using it wrong.

    QVBoxLayout* layout = new QVBoxLayout;
         layout->setContentMargins( 20, 2, 2, 2 );
         layout->addWidget( ... );  // add your (sub-)options there
    
    QWidget* w = new QWidget( contextMenu );
    w->setLayout( layout );
    
    QWidgetAction* wa = new QWIdgetAction( contextMenu );
        wa->setDefaultWidget( w );
    contextMenu->addAction( wa );
    

    Also you need to make sure once a sub-action (widget) is triggered the menu is closed. Either by calling contextMenu->close() or by calling wa->trigger()

    What exactly triggers your sub-action depends on the type you insert.



  • i have another question. how can i add an icon to the left of the menu. in qt docs, it says:
    This property holds the action's icon.
    In toolbars, the icon is used as the tool button icon; in menus, it is displayed to the left of the menu text. There is no default icon.

    so here's my code:

    auto button1 = new QToolButton;
    button1 ->setIcon(QIcon(":/icon1"))
    
    auto button1 = new QToolButton;
    button1 ->setIcon(QIcon(":/icon2"))
    
    // add a Paste Options label and the tool buttons to a grid layout
    auto widget = new QWidget;
    auto layout = new QGridLayout(widget);
    layout->setContentsMargins(0, 0, 0, 3);
    layout->setColumnMinimumWidth(0, 37);
    
    auto pasteOptionslabel = new QLabel(QObject::tr("Paste Options:"), contextMenu);
    pasteOptionslabel->setStyleSheet("background-color: #D5D5D5;");
    
    layout->addWidget(pasteOptionslabel, 0, 1, 1, 3);
    layout->addWidget(button1, 1, 1);
    layout->addWidget(button2, 1, 2);
    layout->setColumnStretch(5, 255);
    
    // add a widget action to the context menu
    auto pasteOptionsWidget = new QWidgetAction(contextMenu);
    pasteOptionsWidget->setIcon(QIcon(":/icon1"));
    pasteOptionsWidget->setDefaultWidget(widget);
    

    it doesn't add an icon (i suppose it's because i already have a widget added?). so how can i add an icon?

    and another question is, i wanna add a background to label, but the background shows only behind text. how can i do that?

    thanks


  • Moderators

    @user4592357 said in create ms word-like context menu:

    it doesn't add an icon (i suppose it's because i already have a widget added?). so how can i add an icon?

    the widget action stretches over the full menu width. So there is no icon shown. You can place the icon inside your widget though.

    and another question is, i wanna add a background to label, but the background shows only behind text. how can i do that?

    i don't quite understand this? Isn't a background not supposed to show behind the content?? o.O


Log in to reply
 

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