Hey,

One tip I can offer when approaching this problem is to make sure to break
the code down to the absolute smallest reproduction of the problem. It
really helps to only have to think about a small amount of logic as opposed
to superflous code. Your example, for instance, had extra logic that is not
related to the problem and just means more code has to be looked at to hunt
down the cause.

I reduced your code to the minimum and then focused on the
QCustomMenu.contextMenuEvent implementation. Printing out the value of the
captured action, parent, self, etc, I was able to see that the problem is
that your rename callback is operating on the menu itself, as opposed to
the action. What you probably wanted to do is to rename the action text if
the right click was on an action item, or to rename a menu if the action is
actually a menu (menus are wrapped in an action).

Here is a seemingly working example:
https://pastebin.com/1jSs9cDx

We first look up the action at the event position. If there is no action it
means we right clicked the actual menu area, so we extract the action from
that. Either way, we pass that on to be renamed as an action.

Some other tips about your original code. Be a bit careful with assigning
parents to menus if they are temporary model menus that you exec_() and
throw away. The parent relationship can make them stay alive and leak. So
you can still parent the menu to get the right relative position, but you
should deleteLater() to clean them up. And the use of the custom QAction
subclasses just to be able to compare them, even though they have no custom
behaviour, could probably be replaced by just creating a set of constants;
AddType=1, RemoveType=2, ..., and then using action.setData() and
action.data().toInt() in order to check the type. Its up to you really. Its
easier than having a bunch of subclasses that don't do anything.

Justin


On Wed, Mar 20, 2019 at 5:41 AM kiteh <kiteh.0...@gmail.com> wrote:

> Hi all,
> (once again, I am very sorry that I am posting a PyQt question instead of
> purely Maya ones...)
>
> I have a ‘nested’, a 2-tiered qmenus in which I have created a context
> menu for renaming and deleting. I created a subclass for the QMenus in
> hopes of making my code cleaner as I am unable to use eventFilter in my
> current code as it messes some functionalities…
>
> For the renaming part, while it renames the first tiered, as soon as I
> tried to do the same for the second-tiered item, the prompt actually
> renames the first-tiered item.
>
> The following is a code portion of the QMenu subclass that I did and if
> you do the following:
>
>    - Right click and add in a new object called main
>    - In main, create another object called sub
>    - (This is an extra right-click) If you perform another right-mouse
>    click on main and select rename options, and have it changed to ‘newMain’,
>    this works
>    - Perform the same action as detailed in point #3, but this time,
>    rename on sub to newSub
>    - If you open up the overall menu, noticed that newMain was changed to
>    newSub, while sub remains unchanged.
>    - Could someone kindly shed some light towards my QMenu subclass on
>    where I have done it wrong? Appreciate in advance for any replies.
>
>
> I have been trying to figure out where has it gone wrong but could not
> come up with anything much.
>
> My code link - https://pastebin.com/raw/6HN1friM
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/fa8ed62f-0ec7-454d-b4a5-928438500c34%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/fa8ed62f-0ec7-454d-b4a5-928438500c34%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1Fkf-8ZrEzMv7t0eP92XU5GBgbOkR18wsSXe_0XxEnqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to