Results 1 to 2 of 2

Thread: Want to find the sender of the currently clicked object and rename and delete that

  1. #1
    Join Date
    Jul 2013
    Posts
    25
    Qt products
    Qt5
    Platforms
    Windows

    Default Want to find the sender of the currently clicked object and rename and delete that

    When the user right clicks to a particular groupbox,menu will appear.
    depends on the menu item triggered it will go to different functions for creating deleting and renaming the button.
    The code is given below


    Qt Code:
    1. QString instance;
    2. QMap<QString, QToolButton*> unselectButtonMap;
    3.  
    4.  
    5. bool MainWindow::eventFilter(QObject *object, QEvent *event)
    6. {
    7. if(ui->groupBox4==object)
    8. {
    9. if(event->type() == QEvent::ContextMenu)
    10. {
    11. // QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
    12. QMenu *menu = new QMenu(this);
    13.  
    14. QAction* myAction1=new QAction(tr("New folder"), this);
    15. QAction* myAction2=new QAction(tr("Delete "), this);
    16. QAction* myAction3=new QAction(tr("Rename"), this);
    17. QAction* myAction4=new QAction(tr("select file type"), this);
    18. //Connect the actions to slots in your main window
    19. connect(myAction1, SIGNAL(triggered()), this, SLOT(Function1()));
    20. connect(myAction2, SIGNAL(triggered()), this, SLOT(Function2()));
    21. connect(myAction3, SIGNAL(triggered()), this, SLOT(Function3()));
    22. connect(myAction4, SIGNAL(triggered()), this, SLOT(Function4()));
    23. //Add the actions to the menubar (or a menu)
    24. menu->addAction(myAction1);
    25. menu->addAction(myAction2);
    26. menu->addAction(myAction3);
    27. menu->addAction(myAction4);
    28. menu->exec(QCursor::pos());
    29.  
    30. return false;
    31. }
    32. }
    33.  
    34. }
    35. void MainWindow::Function1()
    36. {
    37. i++;
    38. index++;
    39. int positionr = layout1->rowCount();
    40. int positionc = layout1->columnCount();
    41. QPixmap* pixmap5 = new QPixmap("../nimhans/yellow2.jpg");
    42. QIcon icon5(*pixmap5);
    43. QToolButton* b = new QToolButton(this);
    44. b->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    45. b->setIcon(icon5);
    46. b->setIconSize(pixmap5->rect().size()/2);
    47. b->setText(QString(" New folder") + QString::number(i));
    48. b-> setObjectName(QString(" New folder") + QString::number(i));
    49. unselectButtonMap[instance] = b;
    50. unselectButtonMap[instance]->installEventFilter(this);
    51. qDebug()<<unselectButtonMap[instance];
    52. qDebug()<<unselectButtonMap[instance]->text();
    53. buttons[index]=unselectButtonMap[instance];
    54. if(i<=5)
    55. layout1->addWidget(b, 0, positionc);
    56. else if(i>5 && i<=10)
    57. layout1->addWidget(b, 1,i-5);
    58. else if(i>10 && i<=15)
    59. layout1->addWidget(b, 2,( i-10));
    60. else
    61. QMessageBox::warning(0,tr("Warning"),tr("Can not create more folders"));
    62. }
    63.  
    64. void MainWindow::Function2()
    65. {
    66. foreach( instance,unselectButtonMap.keys() )
    67. if(unselectButtonMap[instance]== QObject::sender()) {
    68. QMessageBox::warning(0,tr("Warning"),tr("do you want to delete the folder"));
    69. unselectButtonMap[instance]->deleteLater();
    70. }
    71.  
    72. }
    73. }}
    To copy to clipboard, switch view to plain text mode 

    But here QObject::sender() will return QAction which is triggered in the menu.
    How can I get the clicked push button reference(sender object)??
    Can u suggest any method??
    All your suggestions are appreciable .Thanks in advance.
    Last edited by high_flyer; 10th September 2013 at 09:01. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Want to find the sender of the currently clicked object and rename and delete tha

    Quote Originally Posted by Soumya Somasekhar Ram View Post
    But here QObject::sender() will return QAction which is triggered in the menu.
    Of course, it is the sender of the signal that invoked the slot.

    Quote Originally Posted by Soumya Somasekhar Ram View Post
    How can I get the clicked push button reference(sender object)??
    The menu item was clicked, so you get the associated action's triggered() signal to be emitted.
    There is no button involved at all.

    If you are referring to you QToolButton instances: those are not connected to anything.

    Cheers,
    _

    P.S.the easiest way to have a context menu on a widget with your own actions is to use addAction() on the widget and set the context menu policy to Qt::ActionsContextMenu

Similar Threads

  1. TapAndHoldGesture sender object name
    By giovanni.foiani in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2013, 09:15
  2. How can find which button is clicked.
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2011, 14:35
  3. Replies: 3
    Last Post: 30th July 2010, 16:34
  4. [solved] Which object type is QObject::sender()?
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2009, 21:03
  5. Replies: 4
    Last Post: 19th February 2009, 11:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.