Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pass a variable to a function called with a QShortcut?

How to pass a variable to a function called with a QShortcut?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 225 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CesarC Offline
    CesarC Offline
    Cesar
    wrote on last edited by Cesar
    #1

    My attempt:

    void Func(int x)
    {
        qDebug() << x << "\n";
    }
    
    
    
    Test::Test(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
    
        QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_1), this);
        QObject::connect(shortcut, &QShortcut::activated, this, &Func(10));
        return;
    }
    

    I'm getting this error: expression must be an lvalue or a function designator
    at QObject::connect(shortcut, &QShortcut::activated, this, &Func(10));

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You can use std::bind for such trick.

      QObject::connect(shortcut, &QShortcut::activated, this, std::bind(Func, 10));
      
      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved