Skip to content

Instantly share code, notes, and snippets.

@mitchcurtis
Last active February 26, 2024 08:13
Show Gist options
  • Save mitchcurtis/f4fde329138b3adbffbbec22b08f2bb8 to your computer and use it in GitHub Desktop.
Save mitchcurtis/f4fde329138b3adbffbbec22b08f2bb8 to your computer and use it in GitHub Desktop.
Widgets menu example - QMenuBar, QMenu, QComboBox
#include <QtWidgets>
int main(int argc, char *argv[])
{
// QApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);
// QApplication::setAttribute(Qt::AA_DontUseNativeMenuWindows);
QApplication app(argc, argv);
QMainWindow mainWindow;
QWidget *centralWidget = new QWidget;
auto createLayout = [&mainWindow](QWidget *parent) -> QBoxLayout* {
if (mainWindow.screen()->primaryOrientation() == Qt::LandscapeOrientation)
return new QHBoxLayout(parent);
return new QVBoxLayout(parent);
};
QBoxLayout *mainLayout = createLayout(centralWidget);
centralWidget->setLayout(mainLayout);
// Set up the menu bar.
auto *menuBarMenu = new QMenu("Menu bar menu", mainWindow.menuBar());
QAction action1("Menu item 1", centralWidget);
QAction action2("Menu item 2", centralWidget);
QAction action3("Menu item 3", centralWidget);
menuBarMenu->addAction(&action1);
menuBarMenu->addAction(&action2);
menuBarMenu->addAction(&action3);
mainWindow.menuBar()->addMenu(menuBarMenu);
// Set up the context menu.
QMenu contextMenu("Context menu", centralWidget);
contextMenu.addAction(&action1);
contextMenu.addAction(&action2);
contextMenu.addAction(&action3);
mainWindow.setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(&mainWindow, &QWidget::customContextMenuRequested,
&mainWindow, [&contextMenu, &mainWindow](const QPoint &pos) {
contextMenu.exec(mainWindow.mapToGlobal(pos));
});
// Set up the buttons that interact with/modify the context menu.
auto *contextMenuGroup = new QGroupBox("Context menu", centralWidget);
QBoxLayout *contextMenuGroupLayout = createLayout(contextMenuGroup);
// Useful for opening the menu on systems that apparently don't emit customContextMenuRequested,
// like Android: https://bugreports.qt.io/browse/QTBUG-122732
auto *openContextMenuButton = new QPushButton("Open", contextMenuGroup);
QObject::connect(openContextMenuButton, &QPushButton::clicked, [&contextMenu, &mainWindow, openContextMenuButton](){
contextMenu.move(mainWindow.geometry().center());
contextMenu.show();
});
contextMenuGroupLayout->addWidget(openContextMenuButton);
auto *appendButton = new QPushButton("Append menu item", contextMenuGroup);
QObject::connect(appendButton, &QPushButton::clicked, [&centralWidget, &contextMenu](){
auto action = new QAction("Extra context menu item", centralWidget);
contextMenu.addAction(action);
});
contextMenuGroupLayout->addWidget(appendButton);
auto *removeButton = new QPushButton("Remove menu item", contextMenuGroup);
QObject::connect(removeButton, &QPushButton::clicked, [&contextMenu, removeButton](){
contextMenu.removeAction(contextMenu.actions().last());
removeButton->setEnabled(!contextMenu.actions().isEmpty());
});
contextMenuGroupLayout->addWidget(removeButton);
auto *addSubMenuButton = new QPushButton("Add sub-menu", contextMenuGroup);
auto *addSubMenuItemButton = new QPushButton("Add sub-menu item", contextMenuGroup);
addSubMenuItemButton->setEnabled(false);
QMenu *subMenu = nullptr;
QObject::connect(addSubMenuButton, &QPushButton::clicked, [&](){
subMenu = new QMenu("Sub-menu", centralWidget);
contextMenu.addMenu(subMenu);
addSubMenuButton->setEnabled(false);
addSubMenuItemButton->setEnabled(true);
});
contextMenuGroupLayout->addWidget(addSubMenuButton);
QObject::connect(addSubMenuItemButton, &QPushButton::clicked, [&](){
auto action = new QAction(QString::fromLatin1("Sub-menu item %1").arg(
subMenu->actions().size() + 1), centralWidget);
subMenu->addAction(action);
});
contextMenuGroupLayout->addWidget(addSubMenuItemButton);
contextMenuGroup->setLayout(contextMenuGroupLayout);
mainLayout->addWidget(contextMenuGroup);
// Add ComboBox.
auto *comboBoxGroup = new QGroupBox("Combo box", centralWidget);
QBoxLayout *comboBoxGroupLayout = createLayout(comboBoxGroup);
auto *comboBox = new QComboBox(comboBoxGroup);
for (int i = 0; i < 10; ++i)
comboBox->addItem(QString::fromLatin1("Item %1").arg(i));
comboBoxGroupLayout->addWidget(comboBox);
comboBoxGroup->setLayout(comboBoxGroupLayout);
mainLayout->addWidget(comboBoxGroup);
mainWindow.setCentralWidget(centralWidget);
mainWindow.resize(400, 400);
mainWindow.show();
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment