[SOLVED] Add custom actions for menu shown upon clicking the title bar icon



  • Hello all,

    I have a need to add a custom action (say 'About' clicking which a messagebox needs to be displayed) in the context menu (system menu) shown when the icon on the title bar of a QDialog is clicked. How do I achieve this?

    !http://i.msdn.microsoft.com/dynimg/IC163369.png!

    Regards,
    Bharath



  • I tried doing this.

    @ setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(QPoint)),
    this, SLOT(addContextMenuAction(QPoint)));@

    In this case the slot isn't getting called when I click on the window title icon or right click any where on the title bar (which displays the popup menu with Maximize, Minimize and Close actions).

    Regards,
    Bharath



  • Hello,

    I found the way to solve this. The solution is specific to windows platform. Here's what I did.

    1. Add a separator and 'About' text to the system menu
      @HMENU sysMenu = ::GetSystemMenu((HWND) winId(), FALSE);
      if (sysMenu != NULL) {
      ::AppendMenuA(sysMenu, MF_SEPARATOR, 0, 0);
      ::AppendMenuA(sysMenu, MF_STRING, IDM_ABOUTBOX, "About");
      }@

    2. Override QWidget::nativeEvent (in Qt 5.x) or QWidget::winEvent (in Qt 4.x) and handle the event
      @ MSG msg = (MSG) message;
      Q_UNUSED(type)

      if (msg->message == WM_SYSCOMMAND) {
      if ((msg->wParam & 0xfff0) == IDM_ABOUTBOX) {
      *result = 0;
      // Show the About dialog
      displayAboutInformation();

           return true;
       }
      

      }

      return false;@

    3. IDM_ABOUTBOX is defined as below
      @// IDM_ABOUTBOX must be in the system command range
      // (IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX)
      // and (IDM_ABOUTBOX < 0xF000)
      #define IDM_ABOUTBOX 0x0010@

    4. Include user32 in the pro file
      @LIBS += -lUser32@

    I got this information from an older thread "https://qt-project.org/forums/viewthread/21165":https://qt-project.org/forums/viewthread/21165


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.