1

I already have this function to retrieve the clicked QPushButton object name:

void MainWindow::clickedBtnInfo()
{
    QPushButton *btnSender = qobject_cast<QPushButton*>(sender()); // retrieve the button you have clicked
    QString *clickedBtnName = btnSender->objectName(); // retrive the object name from the clicked button
    qDebug() << clickedBtnName;
}

And I checked this website to implement the rightClicked signal.

But I want to know what I can do to retrieve the QPushButton object name which is right clicked.
Thanks.

An edit of how I implement the rightClicked signal:

void MainWindow::onRightClicked()
{
    qDebug() << "User right clicked me";
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::RightButton) {
        emit rightClicked();
    }
}

And connect like this:

connect(this, SIGNAL(rightClicked()), this, SLOT(onRightClicked()));

Sorry for not explaining my question completely before. BTW, I am just very new to Qt, So hope to get some specifically explained answers.
Thanks a lot.

Theodore Tang
  • 831
  • 2
  • 21
  • 42
  • You may add more code , the main problem of your question is not visible here – saeed Nov 12 '17 at 07:57
  • @saeed I added the code of how I implement that. I appreciate your answers, and I tried your following answers. But those may seem kind of complicated for me to understand and implement. – Theodore Tang Nov 12 '17 at 08:14
  • You have connected MainWindow signal and slots , as i understand your question you want to recognized pushbutton right click , the slot onRightClicked will enter when you right click on mainwindow form not pushbutton if you want to handle click and right click of pushbutton you should connect pushbutton signals. – saeed Nov 12 '17 at 08:21
  • you can post more codes if not you may mail me at saeedsoleimanifar@gmail to see whole codes you have done, if possible – saeed Nov 12 '17 at 08:22

1 Answers1

0

The link you provided in the question does the right thing. You need to extend QPushButton and not QMainWindow! Simply copy the sources from https://stackoverflow.com/a/15658863/3767076 into your project (if you need both clicks, check the comment below).

After that, you can use the QRightClickButton instead of a QPushButton. If you created your UI with the designer, you can right-click the button in the ui and set it as placeholder for QRightClickButton.

Felix
  • 6,885
  • 1
  • 29
  • 54