0

I have created a QList of QPushButton that I have assigned to a slot function. But I would like to get coordinates of the button which is clicked in the list.

For example, pushbutton n°5 is clicked, it return me (25,150).

        listButton = new QList<QPushButton*>;

        //some code here

        QPushButton *button = new QPushButton(QString::number(1),ui->widget);
        button->setGeometry(50, 50, 30, 30);
        button->setStyleSheet("background-color:red;");
        QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectP()));
        listeButton->append(button);

        //some code here

        void selectP()
        {
              //I'd like to print here, coordinates of the button which has called "selectP()"
        }

Sorry for my language, thanks in advance !

Templeton Peck
  • 151
  • 1
  • 3
  • 10

1 Answers1

2

In you slot you can get the pointer of the button that had been clicked:

void selectP()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    if (btn) {
        qDebug() << "The coordinates are:" << btn->geometry();
    }
}
vahancho
  • 20,808
  • 3
  • 47
  • 55