Why does my tearoff menu have a title bar?
-
I'm displaying a tearoff menu when a specific button is right-clicked. For some reason, the menu is displaying with a title bar, complete with a close button.
Is this the default behavior? If so, can it be changed?
Code that creates the menu:
SettingsButtonContextMenu = QMenu(btn) SettingsButtonContextMenu.addAction("&Settings", settings_action) SettingsButtonContextMenu.addAction("S&ave", save_action) SettingsButtonContextMenu.addAction("E&xit", exit_action)
Event handler that display the menu:
def mouse_press_handler(button, mb, e): print(button.objectName()) if mb == Qt.MouseButton.RightButton: SettingsButtonContextMenu.showTearOffMenu()
-
@Terrence-Mitchem said in Why does my tearoff menu have a title bar?:
QMenu
Try
SettingsButtonContextMenu->setWindowFlags( (Qt::WindowFlags)Qt::FramelessWindowHint ); -
@JoeCFD said in Why does my tearoff menu have a title bar?:
@Terrence-Mitchem said in Why does my tearoff menu have a title bar?:
QMenu
Try
SettingsButtonContextMenu->setWindowFlags( (Qt::WindowFlags)Qt::FramelessWindowHint );So here's the proposed new code:
SettingsButtonContextMenu = QMenu(btn) SettingsButtonContextMenu.setWindowFlags(Qt.WindowType.FramelessWindowHint) SettingsButtonContextMenu.addAction("&Settings", settings_action) SettingsButtonContextMenu.addAction("S&ave", save_action) SettingsButtonContextMenu.addAction("E&xit", exit_action)
This results in the menu being displayed as soon as the main window is:
No idea what's up with that.
-
@Terrence-Mitchem
Try the following and use stylesheet to set color for text and background# Get the position of the button button_pos = self.button.mapToGlobal(self.button.pos()) # Adjust the position as needed menu_position = button_pos + self.button.rect().bottomLeft() SettingsButtonContextMenu.showTearOffMenu( menu_position )
-
Like the documentation says
showTearOffMenu()
places the menu inside its own widget. It has a title bar so it is permanent and can be moved around. Usepopup()
instead. This is the right function for context menus. -
Thank you! This was the solution.
-