Михаиллл
МихаилллApril 30, 2020, 2:36 p.m.

Как в Qt в qmenu добавить scrollarea

Добрый день.
Как в Qt в qmenu добавить scrollarea?
Эти 2 варианта не работают

    QScrollArea *scroll = new QScrollArea;
    scroll->setWidget(menu);
submenu->setStyleSheet("QMenu { menu-scrollable: 1; }");
We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

11
o
  • May 1, 2020, 7:23 a.m.
  • (edited)
  • The answer was marked as a solution.

Добрый день, не понятно чего вы хотите. Какое меню конкретно: контестное или выпадающее?

    Михаиллл
    • May 1, 2020, 7:34 a.m.

    Мне нужно QMenu, при нажатии на кнопку выпадает это меню

      o
      • May 1, 2020, 7:35 a.m.

      На какую кнопку? QPushButton или обработка QKeyEvent?

        Михаиллл
        • May 1, 2020, 7:37 a.m.

        К QPushButton привязано меню командой setMenu

          o
          • May 1, 2020, 7:42 a.m.

          читайте про слот Public Slots void showMenu()

            Михаиллл
            • May 1, 2020, 7:52 a.m.

            Прочитал тут , но при чем тут скрол?

              o
              • May 1, 2020, 8 a.m.
              • (edited)

              Тогда смотрите в сторону QContextMenuEvent

                o
                • May 1, 2020, 8:02 a.m.

                лучше кончено, полный код выкладывать, что бы было понятно, кого и откуда вызываете.

                  Михаиллл
                  • May 1, 2020, 8:29 a.m.

                  В коде бардак, он явно не поможет. Само событие тоже не поможет.
                  Тут нужно или в наследуемом от меню классе что-нибудь сделать, или еще что то не понятное.

                    o
                    • May 1, 2020, 9:01 a.m.

                    Если в коде бардак, наведите там порядок и выкладывайте.

                      Михаиллл
                      • May 1, 2020, 9:19 a.m.

                      Вот это наследованный класс меню. Но посути это обычное меню.

                      #pragma once
                      
                      #include <QtWidgets>
                      
                      class TransMenu : public QMenu {
                          Q_OBJECT
                      
                      public:
                          TransMenu(QWidget* parent = nullptr) : QMenu(parent) {
                              setFixedSize(300, 600);
                              //setStyleSheet("QMenu { menu-scrollable: 1; }");
                          }
                      
                          QAction*    addBack(const QString& text) {
                              QAction *action = new QAction(text, this);
                      
                              addAction(action);
                              back_actions_.push_back(action);
                              return action;
                          }
                      
                          QAction*    insertBack(QAction* before, const QString& text) {
                              QAction *action = new QAction(text, this);
                              insertAction(before, action);
                              back_actions_.push_back(action);
                              return action;
                          }
                      
                      
                      protected:
                      
                          void showEvent(QShowEvent *event) override {
                      
                              qDebug() << "Show Menu:" << this;
                      
                              prev_menu_ = nullptr;
                              if (menuAction()) {
                                  //Find previous menu of exists;
                                  for (QWidget* w : menuAction()->associatedWidgets()) {
                                      if (TransMenu* menu = qobject_cast<TransMenu*>(w)) {
                                          if (menu->activeAction() == menuAction()) {
                                              prev_menu_ = menu;
                                              move(menu->pos());
                                              menu->hide();
                      
                                              qDebug() << "Detected Previous Menu: " << prev_menu_;
                                              break;
                                          }
                                      }
                                  }
                              }
                      
                              QMouseEvent move_event(QEvent::MouseMove, this->mapFromGlobal(QCursor::pos()), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
                              QApplication::sendEvent(this, &move_event);
                      
                             // if (QAction* at = this->actionAt(this->mapFromGlobal(QCursor::pos()))) {
                             //     qDebug() << "Set Active Action " << this;
                      
                             //     QMouseEvent new_e(QEvent::MouseMove, this->mapFromGlobal(QCursor::pos()), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
                             //     QApplication::sendEvent(this, &new_e);
                      
                             //     bool a = this->isVisible();
                             //     //this->setActiveAction(at);
                      
                             ///*     at->hovered();
                             //     auto r = this->actionGeometry(at);
                             //     this->update(this->actionGeometry(at));*/
                             // }
                             // else {
                             //     qDebug() << "Active Action Not Set " << QCursor::pos();
                             // }
                      
                              QMenu::showEvent(event);
                          }
                      
                          void mousePressEvent(QMouseEvent *event) {
                              //    qDebug() << "QtListWidget::mousePressEvent";
                              if (event->button() == Qt::LeftButton)
                                  mouse_pressed_ = true;
                              QMenu::mousePressEvent(event);
                          }
                      
                          void mouseMoveEvent(QMouseEvent *event) {
                              if (!mouse_pressed_)   // disable click+drag
                                  QMenu::mouseMoveEvent(event);
                          }
                      
                          void mouseReleaseEvent(QMouseEvent *event) override {
                              if (mouse_pressed_)
                                  QMenu::mouseReleaseEvent(event);
                      
                              if (event->button() == Qt::LeftButton)
                                  mouse_pressed_ = false;
                          }
                      
                          void hideEvent(QHideEvent *event) override {
                              qDebug() << "Hide Menu:" << this;
                      
                              //Showed child menu and hiding current. Prevent this;
                              auto a = activeAction();
                              qDebug() << "Hide Action " << a << " " << back_actions_;
                              if (!activeAction() || !activeAction()->menu()) {
                                  qDebug() << "Close menu" << this;
                      
                                  bool back_action = back_actions_.contains(activeAction());
                                  QMenu::hideEvent(event);
                                  if (prev_menu_) {
                                      if (back_action) {
                                          qDebug() << "Popup Previous Menu: " << prev_menu_;
                                          //Clear action to prevent reshow this menu;
                                          prev_menu_->setActiveAction(0);
                                          //Prevent sending releaseMouseEvent;
                                          QMetaObject::invokeMethod(prev_menu_, "show", Qt::QueuedConnection);
                      
                                      }
                                      else {
                                          //Finish hiding;
                                          prev_menu_->dispatchHideEvent();
                                      }
                                  }
                              }
                              else {
                                  emit this->aboutToHide();
                              }
                          }
                      
                          void    actionEvent(QActionEvent *e) override {
                              QMenu::actionEvent(e);
                      
                              if (e->type() == QEvent::ActionRemoved) {
                                  back_actions_.removeAll(e->action());
                              }
                          }
                      
                          void    dispatchHideEvent() {
                      
                              qDebug() << "Finish Menu: " << this;
                              QMenu::hideEvent(&QHideEvent());
                      
                              if (prev_menu_)
                                  prev_menu_->dispatchHideEvent();
                          }
                      
                      private:
                          TransMenu*              prev_menu_ = nullptr;
                          QList<QAction*>         back_actions_;
                          bool                    mouse_pressed_;
                      };
                      
                      
                      

                        Comments

                        Only authorized users can post comments.
                        Please, Log in or Sign up
                        AD

                        C ++ - Test 004. Pointers, Arrays and Loops

                        • Result:50points,
                        • Rating points-4
                        m

                        C ++ - Test 004. Pointers, Arrays and Loops

                        • Result:80points,
                        • Rating points4
                        m

                        C ++ - Test 004. Pointers, Arrays and Loops

                        • Result:20points,
                        • Rating points-10
                        Last comments
                        i
                        innorwallNov. 13, 2024, 11:03 p.m.
                        How to make game using Qt - Lesson 3. Interaction with other objects what is priligy tablets What happens during the LASIK surgery process
                        i
                        innorwallNov. 13, 2024, 8:09 p.m.
                        Using variables declared in CMakeLists.txt inside C ++ files where can i buy priligy online safely Tom Platz How about things like we read about in the magazines like roid rage and does that really
                        i
                        innorwallNov. 11, 2024, 10:12 p.m.
                        Django - Tutorial 055. How to write auto populate field functionality Freckles because of several brand names retin a, atralin buy generic priligy
                        i
                        innorwallNov. 11, 2024, 6:23 p.m.
                        QML - Tutorial 035. Using enumerations in QML without C ++ priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medici…
                        i
                        innorwallNov. 11, 2024, 3:50 p.m.
                        Qt/C++ - Lesson 052. Customization Qt Audio player in the style of AIMP It decreases stress, supports hormone balance, and regulates and increases blood flow to the reproductive organs buy priligy online safe Promising data were reported in a PDX model re…
                        Now discuss on the forum
                        i
                        innorwallNov. 14, 2024, 12:39 a.m.
                        добавить qlineseries в функции Listen intently to what Jerry says about Conditional Acceptance because that s the bargaining chip in the song and dance you will have to engage in to protect yourself and your family from AMI S…
                        i
                        innorwallNov. 11, 2024, 10:55 a.m.
                        Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
                        9
                        9AnonimOct. 25, 2024, 9:10 a.m.
                        Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

                        Follow us in social networks