0

I am currently having a problem for the right click event for QPushButton in the minesweeper game. From what I understand, if I want to give the right click event for the QPushButton, I have to do something like this:
In the Buttons Class:

class Buttons : public QPushButton
{
    Q_OBJECT

public:
    Buttons(QWidget *parent = Q_NULLPTR);

signals:
    void btnRightClicked();

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

And then in the mainwindow.cpp, create an object like this:

Buttons *mButtons = new Buttons(this);

And connect the btnRightClick signal to a slot in MainWindow Class like this:

connect(mButtons, &Buttons::btnRightClicked, this, &MainWindow::onRightClicked);

This works, but since it's a minesweeper game, I am gonna need a lot of buttons. I wonder each time when I am gonna need a button that has the right click event, do I have to create a new object like above?

For example, if I am want 64 QPushButtons, do I have to create 64 objects like this?

for(int i = 0; i < 8; i++) {
    for(int j = 0; j < 8; j++) {
        Buttons *mButton = new Buttons(mCentralWidget);
        mGridLayout->addWidget(mButton, i * 8, j);
        connect(mButton, &Buttons::btnRightClicked, this, &MainWindow::onRightClicked);
    }
}

For me, creating so many objects may sound a little bit crazy. Is there a way to just create one object which contains many QPushButtons that have the right click event?

Allow me to ask another question, which is even if I have to create so many objects, each time I click one of them, that object should be hidden or disappeared. I tried this:

connect(mButton, &Buttons::clicked, mButton, &Buttons::hide);

which is not working.

Hope I explain my question clear. So how do I solve those problems? Or is there any advice for handling the right click event in the minesweeper game?

Any advice will be appreciated, Thanks.

Theodore Tang
  • 831
  • 2
  • 21
  • 42
  • Do you have the same slot in mainWindow for all the buttons? – JLev Nov 26 '17 at 14:18
  • @JLev yes, I created a `onRightClicked` slot in mainwindow for all the buttons – Theodore Tang Nov 26 '17 at 14:21
  • 64 objects are not so much. It looks like what you are doing right now is ok, if it works for you. About hiding, what is not working? When you click, is the slot in the mainWindow triggered? Where is your `connect(mButton, &Buttons::clicked, mButton, &Buttons::hide);` line? – JLev Nov 26 '17 at 14:23
  • BTW, you can have a vector of your 64 buttons if that helps you `std::vector buttonsVector` – JLev Nov 26 '17 at 14:23
  • @JLev since it's a minesweeper game, so I want the button object be hidden after clicking and show a label beneath the button. But the line I provide is not helping the button to hide after clicking. The line I provide is just under the ` connect(mButton, &Buttons::btnRightClicked, this, &MainWindow::onRightClicked);` line in the loop. – Theodore Tang Nov 26 '17 at 14:26
  • @JLev the `vector` way you provide I tried as well, it worked with the right click event, but did not work for another problem. I still do not know how to hide it after clicking. The `clicked signal` and `hide slot` worked just fine for `qpushbutton`, do not know why this is working for the object I create. – Theodore Tang Nov 26 '17 at 14:29
  • 1
    Add this `QPushButton:mousePressEvent(e);` to `mousePressEvent` function, this forwards the event to the `QPushButton` – Simon Nov 26 '17 at 16:20
  • 1
    Change `void mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::RightButton) { emit btnRightClicked(); } }` to `void mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::RightButton) { emit btnRightClicked(); } QPushButton::mousePressEvent(event); }` – eyllanesc Nov 26 '17 at 16:21

0 Answers0