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

  • Solved!

    need to call QWidget::setFocusPolicy().

    With menu Key, I mean this one:

    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);
      d->resize(100, 100);
      w->addAction(new QAction("Test", w));
      return d->exec();

    Any hints or ideas?

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

  • Lifetime Qt Champion


    @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.

  • @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.