Qt,PushButton,id属性?任何知道单击哪个按钮的方法 [英] Qt, PushButton, id attribute? Any way to know which button was clicked

查看:840
本文介绍了Qt,PushButton,id属性?任何知道单击哪个按钮的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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属性或其他",然后知道单击了哪个按钮?每个按钮都有不同的操作.

I have a few QPushButtons which are added automaticlly. Is there a way to add "id attribute or sth else" to button and next know which button was clicked? I have different action for each button.

推荐答案

QApplication提供了sender(),其中包含发送信号的对象.因此,您可以这样做:

QApplication offers sender() which contains which object sent the signal. So you can do:

//slot, this could also be done in a switch
if(button[X] == QApplication::sender()){
  doX();
}else if(button[Y] == QApplication::sender()){
  doY();
}

http://doc.qt.io/qt-4.8/qobject. html#sender

这篇关于Qt,PushButton,id属性?任何知道单击哪个按钮的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆