4

I'm currently developing an app such as the browser using Qt and c++.

I have create a contextual menu to allow on right click action such as delete, rename and add folder.

void MyTreeWidget::createContextMenu() {    
    contextMenu = new QMenu();
    setContextMenuPolicy(Qt::ActionsContextMenu);

    addFolderAction = new QAction("Add Folder", contextMenu);
    addAction(addFolderAction);
    connect(addFolderAction, SIGNAL(triggered()),this,SLOT(onAddFolderActionTree()));

    deleteAction = new QAction("Delete", contextMenu);
    addAction(deleteAction);
    connect(deleteAction, SIGNAL(triggered()),this,SLOT(onDeleteAction()));

    RenameAction = new QAction("Rename", contextMenu);
    addAction(RenameAction);
    connect(RenameAction, SIGNAL(triggered()),this,SLOT(onRenameAction()));

}

This is working fine. This contextual menu is used when you select a file or folder in my treewidget and make a right click. My issue is that I propose the "add folder" option even if I select a file. You can't create a folder inside a file.

What I want is to disable the option when a file is selected and enable it when it's a folder.

I can know if it's file or folder by getting the TreeWidgetItem class I have overloaded:

Thanks

Seb
  • 2,929
  • 4
  • 30
  • 73

2 Answers2

2

You can disable QAction. In this case "Add Folder" menu item will be disabled:

addFolderAction->setEnabled(false);
Andrei Shikalev
  • 702
  • 4
  • 13
2

Use the QAction::setEnabled(bool) method on your 'addFolderAction'.

One way to use it is like this:

void
MyTreeWidget::updateMenuActions()
{
    if(!contextMenu)
        return;
    bool addFolderEnabled = <check TreeWidgetItem here to enable / disable>;
    addFolderAction->setEnabled(bEnabled);
}

Call the updateMenuActions() method just before you display the context menu.

I actually prefer the code below in case you have situations where you can have NULL pointers to actions (for cases where you don't even add them):

void
MyTreeWidget::updateMenuActions()
{
    if(!contextMenu)
        return;
    bool addFolderEnabled = <check TreeWidgetItem here to enable / disable>;
    updateAction(addFolderAction, bEditEnabled);
}

void
MyTreeWidget::updateAction(QAction* pAction, const bool& bEnabled)
{
    if(pAction)
        pAction->setEnabled(bEnabled);
}

Enjoy.

Devan Williams
  • 1,329
  • 2
  • 16
  • 18
  • when do I find the request to display the contextual menu ? I'm just calling the createcontextmenu when instantiate the MyTreeWidget class and after Qt handle it automatically – Seb May 05 '15 at 17:26
  • 1
    Ah, sorry, I thought you had that part... You need to overload 'contextMenuEvent' in MyTreeWidget: virtual void contextMenuEvent(QContextMenuEvent *event); You can call the updateMenuActions method in there, and then just call: QTreeWidget::contextMenuEvent(event); to display the menu as before. – Devan Williams May 05 '15 at 23:10
  • Thnaks it's done. I made at MouseEvent level. In case it not stable will move on contextMenuEvent overload – Seb May 05 '15 at 23:16