[Qt-interest] How can I pop-up a menu when I clikcked a button?

Alessandro Portale Alessandro.Portale at trolltech.com
Tue Jun 9 07:07:48 CEST 2009


QPushButton can easily pop up a menu below itself. No custom slot is 
needed. Did you try QPushButton::setMenu() ?

    http://doc.qtsoftware.com/4.5/qpushbutton.html#setMenu

Alessandro.

Kermit Mei schrieb:
> Hello,community!
> 
> I need to use a button to pop-up a menu. I can't use MenuBar, because it
> can't show as a icon. So I set a button, and connect it to a slot
> showMenu(): 
> 
> void MainWidget::showMenu() {
>   QWidget *senderWidget = qobject_cast<QWidget *>(sender());
>   if(senderWidget == 0)
>     return;
>   QMenu menu(this);
>   foreach(QAction *act,confActions)
>     menu.addAction(act);
>   menu.exec(senderWidget->pos());
> }
> 
> But, when I click the button, the menu always pop-up out of the current
> Widget when I move the widget. I hope the menu can be inside the widget
> which include the button. 
> 
> Thanks.
> Kermit



More information about the Qt-interest-old mailing list