How to pass variables to slot methods in QT?

后端 未结 6 1348
长情又很酷
长情又很酷 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 05:21

    For strings and integers, you can use QSignalMapper. In your Messenger class, you would add a QSignalMapper mapper object, and your function would look like:

    void Messenger::addToActivePanels(std::string& channel)
    {
        activePanelsContents = this->findChild(QString("activePanelsContents"));
        pushButton = new QPushButton(activePanelsContents);
        // ...
        connect(pushButton, SIGNAL(clicked()), &mapper, SLOT(map()));
        mapper.setMapping(pushButton, QString(channel.c_str()));
    }
    

    and after you have added all channels to your active panels, you call

    connect(&mapper, SIGNAL(mapped(const QString &)), this, SLOT(switchTab(const QString &)));
    

提交回复
热议问题