mercredi 22 mai 2019

A slot as a paramater in a lambda function in C++ / Qt

I have a lambda method which I would like to receive a slot as a parameter

I have ended up with:

  auto createShortcuts = [ = ]( const QString &sequence, const std::function < void () > &slot  )
  {
    QShortcut *sc = new QShortcut( QKeySequence( sequence ), this );
    sc->setContext( Qt::ApplicationShortcut );
    connect( sc, &QShortcut::activated, mMyObject, slot );
  };

  createShortcuts( QStringLiteral( "whatever" ), [=](){mMyObject->mySlot();} );

But I would prefer avoid using a lambda slot when calling createShortcuts. Rather something like

  auto createShortcuts = [ = ]( const QString &sequence, void ( MyObject::* )() )
  {
    QShortcut *sc = new QShortcut( QKeySequence( sequence ), this );
    sc->setContext( Qt::ApplicationShortcut );
    connect( sc, &QShortcut::activated, mMyObject, ??? );
  };

  createShortcuts( QStringLiteral( "whatever" ), &MyObject::mySlot );

But I couldn't find the proper syntax to call the slot.

Aucun commentaire:

Enregistrer un commentaire