Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Customize QMenu Programmatically
Forum Updated to NodeBB v4.3 + New Features

[Solved] Customize QMenu Programmatically

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    Hi,

    My application run on two modes and according to the mode changes I need to customize QMenu.

    I used following steps.

    Create 6 actions using Design Mode of Qt creator.

    Add action_1 and action_2 graphically to view_menu.

    Then programmatically set other four actions as below.

    @if(mode==mode_1)
    {
    ui->view_menu->addActions(modeOneActions());
    }
    else if (mode==mode_2)
    {
    ui->view_menu->addActions(modeTwoActions());
    }@

    @QList<QAction *> modeOneActions()
    {
    QList<QAction *> actionslist;
    actionslist.append(ui->action_3);
    actionslist.append(ui->view_menu->addSeparator());
    actionslist.append(ui->action_4);
    return actionslist;
    }@

    @QList<QAction *> modeTwoActions()
    {
    QList<QAction *> actionslist;
    actionslist.append(ui->view_menu->addSeparator());
    actionslist.append(ui->action_5);
    actionslist.append(ui->action_6);
    return actionslist;
    }@

    But this way can not remove the previously added actions. If go to mode_2 after mode_1 , then all actions displayed in view_menu of mode_2.

    I found that I can clear() all the items from view_menu and add particular one. But it is not simple. (have more actions)

    Dear friends, I have two questions.

    1. Are there any methods?
    2. Is the correct way to add seperators?
      @actionslist.append(ui->view_menu->addSeparator());@
    1 Reply Last reply
    0
    • M Offline
      M Offline
      msue
      wrote on last edited by
      #2

      QMenu::clear() empties the menu.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        You could also hide the actions you don't want to use in your mode instead. That way they are still added to the menu, just not visible.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hareen Laks
          wrote on last edited by
          #4

          Thank you friends. It sounds like I should remove all and add them again or I should add all and hide some of them according to the mode.

          Thanks again.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Did you even try the QAction::setVisible I suggested?

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hareen Laks
              wrote on last edited by
              #6

              Andre,

              I created three methods as
              @QList<QAction *> commonActions();
              QList<QAction *> modeOneActions();
              QList<QAction *> modeTwoActions();@

              When change the modes I clear all the actions and add the suitable actions with common actions.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved