Custom ContextMenu

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
dzenita
Newbie Member
Posts: 6
Joined: Thu Oct 19, 2017 2:34 pm

Custom ContextMenu

Post by dzenita » Thu Oct 19, 2017 2:46 pm

Hello,
I am new in QCad, and I am using QCad 13.18.1 pro.
I have to add custom Context Menu for my dxf-files.
Is that possible, and if it is, how can I add it . Is there any example ?

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4935
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Custom ContextMenu

Post by Husky » Thu Oct 19, 2017 7:04 pm

Hello dzenita - welcome to the forum.
dzenita wrote:I have to add custom Context Menu for my dxf-files.
Please give us more details about your goal. If possible with an example screenshot or drawing. Thanks.
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

dzenita
Newbie Member
Posts: 6
Joined: Thu Oct 19, 2017 2:34 pm

Re: Custom ContextMenu

Post by dzenita » Fri Oct 20, 2017 9:54 am

I am coding and I want do extend context menu in a script. Is that possible ?
I am writing company specific extension, and I want to put them inside right click menu.

In attachment is an example.
Attachments
example1.png
example1.png (98.11 KiB) Viewed 4177 times

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Custom ContextMenu

Post by andrew » Fri Oct 20, 2017 11:59 am

Please place this code in a file for example under scripts/Misc/Examples/CustomContextMenu/CustomContextMenu.js:

Code: Select all

include("scripts/Pro/Widgets/ContextMenu/ContextMenu.js");

function CustomContextMenu() {
}

CustomContextMenu.prototype = new Object();

CustomContextMenu.postInit = function(basePath) {
    // disable default context menu:
    ContextMenu.disconnect();

    // initialize custom context menu:
    var appWin = RMainWindowQt.getMainWindow();
    appWin.contextMenu.connect(CustomContextMenu, "showContextMenu");
};

/**
 * This is called when user right clicks into empty space or on an entity.
 *
 * \param entityId ID of entity that was clicked (context) or RObject.INVALID_ID if
 * context is empty space.
 * \param pos Position of the click in drawing coordinates.
 */
CustomContextMenu.showContextMenu = function(entityId, pos) {
    // get the detault context menu from QCAD:
    var menu = ContextMenu.getContextMenu(entityId, pos);

    // add a custom action:
    var action = menu.addAction("My Action");
    action.triggered.connect(CustomContextMenu, "myAction");

    // show context menu:
    if (!isNull(menu) && !menu.isEmpty()) {
        menu.exec(QCursor.pos());
    }
};

/**
 * This is called when user clicks "My Action" in context menu.
 */
CustomContextMenu.myAction = function() {
    EAction.handleUserMessage("My Action Triggered");
};
The code adds a custom entry "My Action" to the standard context menu of QCAD and should hopefully be self-explanatory.

dzenita
Newbie Member
Posts: 6
Joined: Thu Oct 19, 2017 2:34 pm

Re: Custom ContextMenu

Post by dzenita » Wed Nov 01, 2017 8:54 am

Thanks a lot Andrew
It works :)

Post Reply

Return to “QCAD 'How Do I' Questions”