How to pass variables to slot methods in QT?

后端 未结 6 1312
长情又很酷
长情又很酷 2021-02-14 04:19

I\'m making a little chat messenger program, which needs a list of chat channels the user has joined. To represent this list graphically, I have made a list of QPushButton

6条回答
  •  情深已故
    2021-02-14 04:54

    Use QSignalMapper to pass variables;

            QSignalMapper* signalMapper = new QSignalMapper (this) ;
            QPushButton *button = new QPushButton();
    
    
             signalMapper -> setMapping (button, ) ;
             connect (signalMapper, SIGNAL(mapped(QString)), this,   
                       SLOT(buttonClicked(QString))) ;
    

    in slot i.e

    void class::buttonClicked(QString data){
    
        //use data
        // to get sender 
        QSignalMapper *temp = (QSignalMapper *)this->sender();
        QPushButton *btn = (QPushButton *)temp->mapping(data);
        // use btn
    
    }
    

    Hope my ans may help you

提交回复
热议问题