How we can connect the signals and slot with different arguments?

前端 未结 4 1079
温柔的废话
温柔的废话 2021-02-02 13:16

In Qt, signals and slots require matching argument types:

QObject::connect: Incompatible sender/receiver arguments QLabel::linkActivated(QString) --> Butt

4条回答
  •  粉色の甜心
    2021-02-02 14:07

    Default values for slot parameters helps very well. This allow to connect signals with different signatures to slot (vice versa to @pnezis answer):

    private slots:
      void slot( int x = 10, int y = 20, QString text = QString() );
    

    may be connected to different signals:

    signal1(int, int, QString)
    signal2(int, int)
    signal3(int)
    signal4()
    

    Also Qt 4.8 suggest useful QSignalMapper class:

    This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.

    But only for one parameter:

    QSignalMapper* mapper = new QSignalMapper(this) ;
    
    connect(action, SIGNAL(triggered()), mapper, SLOT(map())) ;
    mapper->setMapping(action, "param value") ;
    
    connect(mapper, SIGNAL(mapped(const QString &)),
      this, SIGNAL(clicked(const QString &)));
    

提交回复
热议问题