Skip to content

Instantly share code, notes, and snippets.

@dirk-thomas

dirk-thomas/main.cpp

Created Apr 14, 2016
Embed
What would you like to do?
QMenu bug when removing last action
#include <cstdio>
#include <QtCore/qobject.h>
#include <QtWidgets/qaction.h>
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qmainwindow.h>
#include <QtWidgets/qmenubar.h>
QMenu * more_menu = 0;
void append()
{
printf("append one item to the menu\n");
QAction * action = new QAction("item", more_menu);
more_menu->addAction(action);
}
void pop()
{
QList<QAction *> actions = more_menu->actions();
if (!actions.isEmpty()) {
QAction * action = actions.takeLast();
if (action->isVisible()) {
printf("pop one item from the menu\n");
more_menu->removeAction(action);
} else {
printf("no popping the hidden dummy action\n");
}
} else {
printf("nothing to pop\n");
}
}
int main(int argc, char ** argv)
{
QApplication * app = new QApplication(argc, argv);
QMainWindow * main_window = new QMainWindow();
QMenuBar * menu_bar = main_window->menuBar();
QMenu * file_menu = menu_bar->addMenu("Command");
more_menu = menu_bar->addMenu("List");
{
QAction * action = new QAction("Append", file_menu);
QObject::connect(action, &QAction::triggered, append);
file_menu->addAction(action);
}
{
QAction * action = new QAction("Pop", file_menu);
QObject::connect(action, &QAction::triggered, pop);
file_menu->addAction(action);
}
/*
{
QAction * action = new QAction("Dummy", more_menu);
action->setVisible(false);
more_menu->addAction(action);
}
*/
main_window->resize(400, 100);
main_window->show();
return app->exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment