简体   繁体   English

如何以编程方式关闭 QMenu

[英]How to programmatically close QMenu

I have pretty specific situation.我有非常具体的情况。 I want to place a QAction into QToolbar and reach following behaviour:我想将QAction放入QToolbar并达到以下行为:

  1. Checkable QAction with icon.带有图标的可检查QAction
  2. Classic arrow on the right side which is used for showing menu右侧经典箭头,用于显示菜单
  3. By pressing this arrow my QDialog should appears on screen instead of QMenu -like one通过按此箭头,我的QDialog应该出现在屏幕上,而不是QMenu

Now I'm a bit confused with implementing all this things together.现在我对一起实现所有这些事情有点困惑。

For now I've created QAction added it to toolbar and also created an empty QMenu because I didn't get the idea of how to add the "dropdown" arrow another way.现在我已经创建了QAction ,将它添加到工具栏,还创建了一个空的QMenu ,因为我不知道如何以另一种方式添加“下拉”箭头。
So, I also connected my slot to QMenu aboutToShow() signal and now, I can create my dialog and exec() it just before QMenu shows.因此,我还将我的插槽连接到QMenu aboutToShow()信号,现在,我可以在QMenu显示之前创建我的对话框并exec()它。 But here's the main problem appears: after I did everything with my dialog an click OK button QMenu trying to appear, but as it is empty it shows nothing and further actions become available only after I left-click somwhere to "close" this menu.但这里出现了主要问题:在我用我的对话框完成所有操作后,单击OK按钮QMenu试图出现,但由于它是空的,它什么都不显示,并且只有在我左键单击某个位置以“关闭”此菜单后,进一步的操作才可用。

Is there any way to force QMenu not to show or can inherit from QMenu and reimplemnt its behaviour (I've tried to do such trick with exec() show() popup() methods of QMenu after subclassing from it, but none of them are being called when menu appears on the screen) ?有什么方法可以强制QMenu不显示或可以从QMenu继承并重新实现其行为(我尝试在从 QMenu 子类化之后使用QMenuexec() show() popup()方法来做到这一点,但没有一个当菜单出现在屏幕上时被调用)?

Here's the solution, which worked for me.这是解决方案,对我有用。

class QCustomMenu : public QMenu
{
  Q_OBJECT
public:
  QCustomMenu(QObject *parent = 0):QMenu(parent){};
};

In code:在代码中:

QAction* myActionWithMenu = new QAction ( "ActionText", toolbar);
QCustomMenu* myMenu = new QCustomMenu(toolbar);
connect(myMenu, SIGNAL(aboutToShow()), this, SLOT(execMyMenu()));

execMyMenu() implementation: execMyMenu()实现:

void execMyMenu(){
  m_activeMenu = (QCustomMenu*)sender(); // m_activeMenu -- private member of your head class, needs to point to active custom menu
  QMyDialog* dlg = new QMyDialog();
  // setup your dialog with needed information
  dlg->exec();
  // handle return information
  m_myTimer = startTimer(10); // m_myTimer -- private member of your head(MainWindow or smth like that) class
}

Now we have to handle timerEvent and close our menu:现在我们必须处理 timerEvent 并关闭我们的菜单:

void MyHeadClass::timerEvent(QTimerEvent *event)
{
    // Check if it is our "empty"-menu timer 
    if ( event->timerId()==m_myTimer )
    {
        m_activeMenu->close(); // closing empty menu
        killTimer(m_myTimer);  // deactivating timer
        m_myTimer = 0;         // seting timer identifier to zero
        m_activeMenu = NULL;   // as well as active menu pointer to NULL
    }
}

It works great on every platform and does what I wanted.它在每个平台上都运行良好,并且可以满足我的需求。 Hope, this would help someone.希望,这会对某人有所帮助。 I've spent week trying to find this solution.我花了一周的时间试图找到这个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM