QSignalMapper and original Sender()

前端 未结 2 1185
滥情空心
滥情空心 2021-01-14 02:43

I have a bunch of QComboBoxes in a table. So that I know which one was triggered I remap the signal to encode the table cell location (as described in Selecting

相关标签:
2条回答
  • 2021-01-14 03:24

    I don't know exact answer, but maybe you should use: QComboBox* combo = qobject_cast(sender()) instead of QComboBox* combo = (QComboBox* )sender(). Someting like this:

    
     QObject* obj = sender();
     QComboBox* combo = qobject_cast<QComboBox*>(obj);
     if(combo)
     {
      doSomethingWithCombo(combo);
     }
     else
     {
      // obj is not QComboBox instance
     }
    
    

    But maybe QSignalMapper really substitutes itself instead of real sender...

    0 讨论(0)
  • 2021-01-14 03:42

    Why not connect the QComboBox's signal straight to your slot?

    QComboBox *combo = ...
    connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(changedType(int)));
    

    And then in your slot you can use the sender() method to retrieve the QComboBox that was changed.

    void myDlg::changedType(int row)
    {
        QComboBox *combo = qobject_cast<QComboBox *> sender();
    
        if(combo != 0){
            // rest of code
        }
    }
    

    Alternatively, to use the QSignalMapper method you would just need to change your slot to use the mapping you set up:

    void myDlg::changedType(int row)
    {
        QComboBox *combo = qobject_cast<QComboBox *>(_signalMapper->mapping(row));
    
        if(combo != 0){
            // rest of code
        }
    }        
    
    0 讨论(0)
提交回复
热议问题