我有相当特殊的情况。我想将QAction放入QToolbar中,并达到以下行为:
QAction。QDialog应该出现在屏幕上,而不是QMenu-like one。现在,我有点困惑于一起实现所有这些东西。
现在,我已经创建了QAction,将其添加到工具栏中,还创建了一个空的QMenu,因为我不知道如何以另一种方式添加“下拉”箭头。
因此,我还将我的插槽连接到QMenu aboutToShow()信号,现在,我可以在QMenu显示之前创建对话框并进行exec()。但是这里出现了一个主要的问题:在我对对话框做了所有事情之后,点击OK按钮QMenu试图显示出来,但是由于它是空的,它显示什么也没有,只有在我左击“关闭”这个菜单之后,进一步的操作才会变得可用。
是否有任何方法强迫QMenu不显示或不能继承QMenu并重新实现其行为(在子类化之后,我尝试使用exec() show() popup()方法QMenu,但当菜单出现在屏幕上时,没有调用它们)?
发布于 2015-09-01 14:03:35
这是对我有用的解决方案。
class QCustomMenu : public QMenu
{
Q_OBJECT
public:
QCustomMenu(QObject *parent = 0):QMenu(parent){};
};代码:
QAction* myActionWithMenu = new QAction ( "ActionText", toolbar);
QCustomMenu* myMenu = new QCustomMenu(toolbar);
connect(myMenu, SIGNAL(aboutToShow()), this, SLOT(execMyMenu()));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
}现在我们必须处理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
}
}它在每个平台上都很好,我想做什么就做什么。希望这个能帮到别人。我花了一周时间试图找到这个解决方案。
https://stackoverflow.com/questions/32333250
复制相似问题