Skip to content

Instantly share code, notes, and snippets.

@dirk-thomas
Created April 14, 2016 23:39
Show Gist options
  • Save dirk-thomas/ad2df445e150c0a3545a357ef3da806c to your computer and use it in GitHub Desktop.
Save dirk-thomas/ad2df445e150c0a3545a357ef3da806c to your computer and use it in GitHub Desktop.
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