Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:52:56+00:00 2026-06-17T09:52:56+00:00

I have created instance of QAction inside QGraphicsView child class and connected it to

  • 0

I have created instance of QAction inside QGraphicsView child class and connected it to my slot in the same class.

QAction *action   = new QAction(tr("New"), this);
action->setObjectName("addStopAction");
action->setShortcut(QKeySequence(Qt::ControlModifier | Qt::Key_N));
connect(action, SIGNAL(triggered()), this, SLOT(addNew()));
addAction(action);

Slot is a function creating new instance of QGraphicsItem on scene assigned to QGraphicsView.

void MyGraphicsView::addNew() {
    // Insert new item at cursor position
}

I also add this action to a QMenu which serves as my class context menu.

QMenu *contextMenu = new QMenu(this);
contextMenu->addAction(action);

Everything works fine. When I press Command/Ctrl + N new item is created at cursor position. But when I right-click and select action from context menu I want new item to be created at menu positon.

I can, of course, do some little hack to flag if SLOT was called after contextMenuEvent or something like that, but what I would like to know is:

Is there any way to find out what made QAction emit its triggered() signal inside connected SLOT? That way I could handle when I should place new item at cursor position and when at context menu position inside SLOT implementation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:52:57+00:00Added an answer on June 17, 2026 at 9:52 am

    I think you can use custom data that a QAction object can contain.
    You can set it when you create a context menu:

    void showContextMenu(const QPoint &pos)
    {
        ...
        action->setData(pos);
        ...
    }
    

    And in the addNew() function you check if data exists and reset it in the end:

    void addNew()
    {
        QPoint pos;
        QPoint posFromAction = action->data()->toPoint();
        if (posFromAction.isNull())
        {
            pos = QCursor::pos(); ///< pos will be current cursor's position
        }
        else
        {
            pos = posFromAction; ///< pos will be menu's position
        }
    
        doYourStuffAt(pos)
    
        action->setData(QPoint()); ///< reset action's data
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class named insect child of sprite.I have created a instance of
I have created a new instance of the System.Diagnostics.Process class. The StartInfo.Filename property is
I have created my custom MembershipProvider. I have used an instance of the class
I have a student class and every student instance created needs to be stored
I have an instance of the Popen class created through subprocess.Popen. I would like
I have a class (simulation) which creates an instance of another class (GUI). Inside
I have created a Java class called Rectangle that has the two instance variables
I have created a javascript object and a function to build a new instance
I have created an instance class from Ext.Component to use the ul tag. here
I am trying to install redmine on amazon instance. I have created a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.