2014-10-31 78 views
0

我已經創建了一個QPushButton的QList,我已經分配給一個槽功能。 但我想獲得在列表中點擊的按鈕的座標。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()" 
     } 

對不起,我的語言,提前致謝!

回答

2

在你插槽,你可以得到已被點擊的按鈕的指針:

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

謝謝夥計! – 2014-10-31 21:47:02