I currently have something like this in my code:

    QMenu *mabout = new QMenu("About");
QToolButton* tb_about = new QToolButton();
QAction *test= new QAction(mabout);
test->setText("Test");
mabout->addAction(test);
tb_about->setText(mabout->title());
tb_about->setMenu(mabout);
tb_about->setPopupMode(QToolButton::MenuButtonPopup);
ui.toolBar->addWidget(tb_about);

Now, it this gives me the following:

enter image description here

Now, I want to display the drop-down menu if I click on the About button or the down arrow. However nothing happens when I click on the about text. This is what it looks like:

enter image description here

Nothing drops down. The only time something drops down is when I actually click on the down arrow. Is there any way to make the drop down drop when the down arrow is pressed?

有帮助吗?

解决方案

I would suggest you to use QPushButton instead of QToolButton. Here is a working example:

QMainWindow window;
QVBoxLayout *layout = new QVBoxLayout();

QPushButton *button = new QPushButton( "Menu button" );
QMenu *menu = new QMenu();

button->setMenu( menu );
menu->addAction( "teste1" );
layout->addWidget( button );

window.setCentralWidget( new QWidget() );
window.centralWidget()->setLayout( layout );
window.show();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top