QList中QPushButton的坐标

时间:2014-10-31 21:27:09

标签: c++ qt qpushbutton

我创建了一个QPushButton的QList,我已将其分配给一个槽函数。 但是我想获得列表中点击的按钮的坐标。

例如,点击按钮n°5,它返回我(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()"
        }

对不起我的语言,提前谢谢!

1 个答案:

答案 0 :(得分:2)

在你的插槽中,你可以获得点击按钮的指针:

void selectP()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    if (btn) {
        qDebug() << "The coordinates are:" << btn->geometry();
    }
}