0

I am trying to somehow detect when QPushButton is right clicked. I do have MacBook and I am wondering if solution presented in this post by Md. Minhazul Haque should work for me too if I am using touchpad with force touch. I copied code that he presented (created additional QRightClickButton class) , but unfortunately it doesn't work for me.

That is my connect()

connect(board->dis_board[i][j], SIGNAL(rightClicked()), this, SLOT(flag()));

Where board is a class object

class Board
{
private:
    int size;
    int bombs;
    int places;
public:
    std::vector<std::vector<QPushButton*>> board;
    std::vector<std::vector<QPushButton*>> dis_board;
    explicit Board(int size, int bombs);
    void move(int x, int y);
    void flag(int,int);
};

Constructor of Board class object

Board::Board(int size, int bombs) : size(size), bombs(bombs), places(size*size)
{

    board.resize(size);
    dis_board.resize(size);
    for (int i=0; i<size; ++i)
    {
        board[i].resize(size);
        dis_board[i].resize(size);

        for(int j=0; j<size; ++j)
        {
            board[i][j] = new QPushButton();
            dis_board[i][j] = new QPushButton();
            board[i][j]->setText(" ");
            dis_board[i][j]->setText(" ");
        }
    }
    QTime time = QTime::currentTime();
    qsrand((uint)time.msec());
    for (int i=0; i<bombs;)
    {
        int x = qrand() % ((size - 1 + 1) - 0) + 0;
        int y = qrand() % ((size - 1 + 1) - 0) + 0;
        if(board[x][y]->text() != "x")
        {
            board[x][y]->setText("x");
            ++i;
        }
    }
}

And here you have header of class of an actual window (I think it is how it is called as I am new to Qt)

#ifndef GAME_H
#define GAME_H

#include <qrightclickbutton.h>
#include <QMainWindow>
#include <vector>
#include <QGridLayout>
#include "board.h"
#include <signal.h>
#include <QMouseEvent>

namespace Ui {
class Game;
}

class Game : public QMainWindow
{
    Q_OBJECT

public:
    explicit Game(QWidget *parent = 0, int x=0, int y=0);
    ~Game();

private slots:
    void flag();

private:
    Ui::Game *ui;
    int size;
    int bombs;
    Board *board;
    QGridLayout *grid;
};

#endif // GAME_H

And the last thing that I think is important is a constructor of the window

Game::Game(QWidget *parent, int size, int bombs) :
    QMainWindow(parent),
    ui(new Ui::Game),
    size(size),
    bombs(bombs)
{
    ui->setupUi(this);
    grid = new QGridLayout();
    board = new Board(size, bombs);

    ui->verticalLayout->addLayout(grid);
    for(int i=0; i<size; ++i)
    {
        for(int j=0; j<size; ++j)
        {
            grid->addWidget(board->dis_board[i][j], i, j);
            connect(board->dis_board[i][j], SIGNAL(rightClicked()), this, SLOT(flag()));
        }
    }
}

Game::~Game()
{
    delete ui;
}

void Game::flag()
{
    QMessageBox msg;
    msg.setText("WARNGING");
    msg.exec();
    QPushButton *buttonSender = qobject_cast<QPushButton*>(sender());
    for(unsigned long x=0; x<board->dis_board.size(); ++x)
    {
        for(unsigned long y=0; y<board->dis_board.size(); ++y)
        {
            if(board->dis_board[x][y] == buttonSender)
            {
                board->flag(x,y);
                return;
            }
        }
    }
}

Also clicked() SIGNAL (left button) works fine with these buttons.

  • Ask a question with a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve). – Vishaal Shankar May 02 '18 at 10:15
  • My question was about situation with MacBook touchpad if it is the same or not, so I don't see need in presenting a code (at this level of problem), especially when everything in my code that is handling with right click event is copied from the post that I linked above. –  May 02 '18 at 10:19
  • A right-click is a right-click. Should work fine. What is `e->button()` in `mousePressEvent`? – Heath Raftery May 02 '18 at 10:19
  • @HeathRaftery I can't be sure as I have been working with Qt only for a week or less, but isn't it an event that refers to a given 'called' button –  May 02 '18 at 10:28
  • @JamesSmith : How are you concluding that the problem is with the connect ? Aren't we entitled to know what `board` and `dis_board` are ? To make sure, nothing is overlooked, it is mostly recommended to post a MCVE. – Vishaal Shankar May 02 '18 at 10:46
  • @VishaalShankar Left button click event works correctly so it would be a weird thing if the problem would be with button itself. I edited post a bit and will be able to do that further but later. Maybe for now it will help somehow –  May 02 '18 at 11:17
  • @VishaalShankar I have edited my post so I hope that now It will be easier to help –  May 02 '18 at 12:45
  • Did you try using the new connect syntax work ? `connect( board->dis_board[i][j], &QRightClickButton::rightClicked, this, &Game::flag );` – Vishaal Shankar May 02 '18 at 15:54
  • Doesn't work neither. Doesn't even compile in that way :/ –  May 02 '18 at 18:23
  • @JamesSmith I meant what is the *value* of `e->button()` during the `mousePressEvent` call. That will tell you whether the right click is being detected, as was your original question. Also, in your expanded code, I can't see where you've used `QRightClickButton` - you seem to only be using instances of `QPushButton`. This has now evolved beyond the Q&A format - you might want to try a discussion forum to work towards a useful question. – Heath Raftery May 06 '18 at 01:32

0 Answers0