void MainWindow::addRadioToUI() { int button_cunter=4; while(!database.isEmpty()) { button_cunter++; QPushButton *one = new QPushButton("Play: "+name(get_r.getTrackId())); one->setIcon(QIcon(":/images/play_button.png")); one->setMaximumWidth(140); one->setFlat(true); QGroupBox* get_rGB = new QGroupBox("somethink"); QFormLayout* layout = new QFormLayout; if(button_cunter%5 == 0){ layout->addWidget(one); } get_rGB->setLayout(layout); scrollAreaWidgetContents->layout()->addWidget(get_rGB); } }
我有几个自动添加的QPushButtons.
有没有办法在按钮上添加“id属性或者其他”,然后知道点击了哪个按钮?我对每个按钮都有不同的动作.
解决方法
QApplication提供sender(),其中包含发送信号的对象.所以你可以这样做:
//slot,this could also be done in a switch if(button[X] == QApplication::sender()){ doX(); }else if(button[Y] == QApplication::sender()){ doY(); }