Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-311

QMenu geometry and aboutToShow (regression against 3.3)

    XMLWordPrintable

Details

    • 243926

    Description

      It seems QMenu somehow figures out it's geometry before aboutToShow is emitted. The following example demonstrates this problem.

      The menu will be cleared and have some items added to it when:

      A) The menu is about to show
      B) The button is clicked

      If you start the app and just click the arrow you'll see the menu appear in a strange location.

      If you close the menu and reopen it will appear in a more reasonable location.

      If you restart the app and click the button before bringing up the menu it will appear in right position the first time as well.

      #include <QWidget>
      #include <QWidget>
      #include <QToolButton>
      #include <QMenu>
      #include <QApplication>
      #include <QRect>
      #include <QAction>
      #include <QVBoxLayout>

      class Widget : public QWidget
      {
      Q_OBJECT
      public:
      Widget(QWidget *parent = 0)
      : QWidget(parent)

      { tb_arrow = new QToolButton(this); QVBoxLayout *l = new QVBoxLayout(this); l->addWidget(tb_arrow, 0, Qt::AlignRight); tb_arrow->setPopupMode(QToolButton::MenuButtonPopup); QMenu * menu = new QMenu(tb_arrow); tb_arrow->setMenu(menu); connect(tb_arrow, SIGNAL(clicked()), this, SLOT(sl_updateMenu())); connect(menu, SIGNAL(aboutToShow()), this, SLOT(sl_updateMenu())); }

      public slots:
      void sl_updateMenu()

      { QMenu * menu = tb_arrow->menu(); menu->clear(); menu->addAction("OneOneOneOne"); menu->addAction("TwoTwoTwo"); }

      private:
      QToolButton *tb_arrow;
      };

      #include "main.moc"

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      Widget w;

      const QRect screenSize = QApplication::desktop()->availableGeometry(&w);
      w.move(screenSize.width()-w.width(), screenSize.height()/3);
      QRect r(QPoint(0, 0), w.sizeHint());
      r.moveRight(screenSize.right());
      w.setGeometry(r);
      w.show();
      return app.exec();
      }

      Rejected:
      Unfortunately QMenu does not support populating in a slot connected to aboutToShow(). QMenu's positioning algorithm relies on its size.

      Update: Reopened after we discovered this is a regression. See change 22887.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bjnilsen Bjørn Erik Nilsen
            rve Anders Bakken
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes