3

In Class Buttons, I have a btnRightClicked signal and a mousePressEvent slot:

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

And in mainwindow.cpp, I connect the btnRightClicked signal to onRightClicked slot like this:

connect(&mButtons, SIGNAL(btnRightClicked()), this, SLOT(onRightClicked()));

The onRightClicked slot is like this:

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

But I ran this program, nothing happened. I guess the reason is because I did not connect to the mousePressEvent slot. I am kind of new to Qt, I do not know if I am right or not.

I set up some buttons on the central widget, I want them to have the right clicked event when right click each of them. So how can I make this work?

Thanks

Edit: in button.h:

class Buttons : public QObject
{
    Q_OBJECT
public:
    Buttons();
    QVector<QPushButton*> buttons;

    void setButtons(int totalBtns) {
        for(int i = 0; i < totalBtns; i++) {
            buttons[i]->setObjectName(QString::number(i));
            buttons[i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        }
    }

public slots:
    void mousePressEvent(QMouseEvent *e) {
        if(e->button() == Qt::RightButton) {
            emit btnRightClicked();
        }
    }

signals:
    void btnRightClicked();
};
Theodore Tang
  • 831
  • 2
  • 21
  • 42
  • please show more code so I can help you, you want to catch the mouse right click event for the button widget ? – Simon Nov 24 '17 at 04:50
  • @Simon yes, I have some pushbuttons in Buttons class and I declared them in mainwindow.cpp. I want to add the right clicked event for those buttons – Theodore Tang Nov 24 '17 at 04:55
  • I want to see basic decleration like, did you use the Q_OBJECT macro for signal slots. does `Buttons` inherit from `QPushButton'...?? – Simon Nov 24 '17 at 04:55
  • @Simon I showed the button.h, is that enough information for solving this problem? Then I just set up the buttons in mainwindow.cpp and connect the btnRightClicked signal. – Theodore Tang Nov 24 '17 at 05:02
  • 1
    Firstly put a breakpoint to `emit btnRightClicked();` and check whether you reach that line. I.e. make sure that your signal is emitted. If yes, check your connection. If connection is ok, check whether the receiving object is properly designed with regards to slots. – vahancho Nov 24 '17 at 08:44
  • Show your code. Possible error is here: `connect(&mButtons, ...`. What is `mButtons`? What you see in output log? – Dmitry Sazonov Nov 24 '17 at 09:00
  • @DmitrySazonov I declare this: Button mButtons in mainwindow.h, I see nothing in the output log. I guess they did not really get connected. – Theodore Tang Nov 24 '17 at 09:06
  • @TheodoreTang adding an `override` keyword to `mousePressEvent` will give you some hint. @Simon provided a correct answer. `mousePressEvent` is a virtual method of `QWidget` that you should override. But you created you own class without any logic how to call your own `mousePressEvent` method. – Dmitry Sazonov Nov 24 '17 at 09:14

1 Answers1

4

To get the mouse right click on your widget, you need to implement your own button widget.

class MyButton : public QPushButton
{
 Q_OBJECT

public:
    MyButton(QWidget *parent = Q_NULLPTR);

private slots:
    void mousePressEvent(QMouseEvent *e);

signals:
    void btnRightClicked();
};

cpp

MyButton:MyButton(QWidget * parent) : 
    QPushButton(parent)
{
}
void MyButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::RightButton)
        emit btnRightClicked();

    //this forwards the event to the QPushButton
    QPushButton::mousePressEvent(e);
}

In your buttons class change the button vector to

 QVector<MyButton*> buttons;

Then register the right click event of your MyButton to your signal in Buttons class then forwared the signal to your mainWindow

connect(&mButtons, &Buttons::btnRightClicked,
        this,      &MainWindow::onRightClicked);
Simon
  • 1,522
  • 2
  • 12
  • 24
  • 1
    Another way is to install an event filter on buttons. – Dmitry Sazonov Nov 24 '17 at 09:14
  • I tried the way you said, I mostly just copied and pasted the code you provided and implemented them in a small new program, I tested when right click the buttons if the signal is emitted, it is. But the problem is: I can not make the onRightClicked slot working. What is the problem here? Should I open a new page to show the code, so you know where it goes wrong? – Theodore Tang Nov 25 '17 at 02:55
  • @TheodoreTang update your question, so we can see relevant changes. And provide a complete code sample. You still didn't do it. – Dmitry Sazonov Dec 04 '17 at 11:37
  • 1
    @DmitrySazonov I solved the problem already. When I first saw the answer and tried to implement this to my code, which did not work. So I did not accept this answer at that time, even though Simon provided the right answer. Later when I understand more and made my code work, the implementation looks just like how Simon provided. I just forgot to accept Simon's answer after that. Maybe that is why you think I still have the problem and need to edit this. I don't think I should edit this anymore after solving the problem already, should I? – Theodore Tang Dec 04 '17 at 13:58
  • @TheodoreTang now I see that you accept an answer. It is a good manner to notify commynity about solution, so they will spent less time to find a correct answer. – Dmitry Sazonov Dec 04 '17 at 14:04