Replacing deprecated QtSignalMapper class to forward Signals in Qt5

后端 未结 1 1188
予麋鹿
予麋鹿 2021-02-11 08:19

I have this code which makes a mdi window written for Qt 4:

class MdiWindow : public QMainWindow
{
    Q_OBJECT
public:
    MdiWindow( QWidget *parent = nullptr)         


        
相关标签:
1条回答
  • 2021-02-11 08:40

    Previously you used QSignalMapper::setMapping() to make sure that you'd be sent data you need when the SLOT() was called. Now you can just encapsulate this logic inside a lamba, so if you did (like in Qt example):

         for (int i = 0; i < texts.size(); ++i) {
             QPushButton *button = new QPushButton(texts[i]);
             connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
             signalMapper->setMapping(button, texts[i]);
         }
         connect(signalMapper, SIGNAL(mapped(const QString &)),
                 this, SIGNAL(clicked(const QString &)));
    

    you can now do (somewhat):

         for (int i = 0; i < texts.size(); ++i) {
             QPushButton *button = new QPushButton(texts[i]);
             connect(button, &QPushButton::clicked, [=]() {
                 emit clicked(texts[i]);
             });
         }
    

    If the setMapping() is not being used, then it probably could have been connected directly to a SLOT() already.

    0 讨论(0)
提交回复
热议问题