No context menu in QWidget when pressing the menu-Key?



  • Solved!

    need to call QWidget::setFocusPolicy().

    With menu Key, I mean this one:
    https://en.wikipedia.org/wiki/Menu_key

    I have here a neat Linux and the context key works fine in some list & tree views, it works fine inside edit fields (QLineEdit, QTextEdit, QSpinBox, ...) but inside a simple QWidget the context menu opens just with the mouse and not with the keyboard.

    #include <QApplication>
    #include <QDialog>
    #include <QAction>
    #include <QBoxLayout>
     
    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);
    
      QDialog *d = new QDialog();
      QVBoxLayout *l = new QVBoxLayout(d);
      QWidget *w = new QWidget(d);
      l->addWidget(w);
      d->resize(100, 100);
      w->setContextMenuPolicy(Qt::ActionsContextMenu);
      w->addAction(new QAction("Test", w));
      d->show();
      return d->exec();
    }
    

    Any hints or ideas?


  • Moderators

    Hi! As you marked the thread as solved, may I ask what the your solution was?


  • Lifetime Qt Champion

    Hi,

    @Wieland At the top of the post, @Wurgl mentions adding somewhere a call to setFocusPolicy.

    @Wurgl, on a side note, you have a memory leak in your application. You should either keep d on the stack or delete it after you return from exec.


  • Moderators

    @SGaist Oh, right. I think I need glasses :))


Log in to reply
 

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