[SOLVERD] QVector of QPushButton and slots, events



  • Hello,
    I have dynamic array of buttons, labels and so on. Number of those depends on user input. Unfortunately each of buttons is doing something else. Number of buttons is 1-16. I may just make 16 slots and just do not connect all, but is there any way to do it with events? I tried but hitbutton() is protected and isDown is not working...
    @void Test::mousePressEvent(QMouseEvent *event){
    for(int i =0;i<buttons.size();i++){
    if(event->button()==Qt::LeftButton){
    // if(buttons.at(i)->hitButton(QCursor::pos())){
    if(buttons.at(i)->isDown()){
    labels.at(i)->setText("Click!");
    }
    } else {
    QWidget::mousePressEvent(event);
    }
    }
    }@



  • Not 100% clear what you want to do, but maybe this helps:
    sender() returns the QObject that caused a slot to be called (via a signal). So you could connect all buttons to the same slot and distinguish the buttons in there via qobejct_cast<QPushButton*>(sender()).



  • Thanks! It works!

    @void Test::buttonAction(){
    for(int i=0;i<buttons.size();i++){
    if(qobject_cast<QPushButton*>(sender())==buttons.at(i)){
    labels.at(i)->setText("Boom");
    }
    }
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.