How to pass variables to slot methods in QT?

后端 未结 6 1311
长情又很酷
长情又很酷 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:07

    I would do it with "relay" objects:

    Create TabSwitchRelay which is a sub-class of QObject with this constructor:

    TabSwitchRelay::TabSwitchRelay(QObject *parent, Messanger * m, const QString & c)
    : QObject(parent), m_messanger(m), m_channel(c)
    {
    }
    

    It also has a slot clicked():

    void TabSwitchRelay::clicked()
    {
        m_messager->switchTab(m_channel);
    }
    

    Now replace the line in your code that does connect with this:

    TabSwitchRelay * tabRelay = new TabSwitchRelay(pushButton, this, channel);
    connect(pushButton, SIGNAL(clicked()), tabRelay, SLOT(clicked()));
    

    It's not tested but you get teh basic idea.

提交回复
热议问题