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. Passing Parameters to Slots
Forum Updated to NodeBB v4.3 + New Features

Passing Parameters to Slots

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.3k 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.
  • D Offline
    D Offline
    DeanQt
    wrote on last edited by
    #1

    A slot seems to work as expected without a parameter passed to it as follows:

    @ connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(goal_seek()));

    //...code...

    void MainWindow::goal_seek()
    {
    double seek = 5;
    QTableWidgetItem *itab7= new QTableWidgetItem;
    QString A2 = QString::number(seek);
    itab7->setText(A2);
    ui->tableWidget_2->setItem(0,7, itab7);
    }
    @

    However, when I use the same code, the slot does not work with the parameter passed to it, as shown below:

    @
    connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(goal_seek(5.0)));

    void MainWindow::goal_seek(double seek)
    {
    double seek = 5;
    QTableWidgetItem *itab7= new QTableWidgetItem;
    QString A2 = QString::number(seek);
    itab7->setText(A2);
    ui->tableWidget_2->setItem(0,7, itab7);
    }
    @

    Can one use parameters for slots, and if so, how?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are doing it wrong. You can't pass static values to slots in the connect assignment. You need to emit a value together with your signal:
      @
      // header
      signals:
      void clicked(int) const;

      // source
      connect (usomeObject, SIGNAL (clicked(int)), this, SLOT(goal_seek(int)));

      // signal emission:
      emit clicked(5);
      @

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DeanQt
        wrote on last edited by
        #3

        It doesn't work. Where does the emit code go?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          One of solutions is:
          @ connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(some_slot()));
          //code...
          void MainWindow::some_slot()
          {
          goal_seek(5);
          }
          @
          or
          @
          connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(some_slot()));
          connect (ui->pushButton, SIGNAL (clicked(int)), this, SLOT(goal_seek(int)));
          //code...

          void MainWindow::some_slot()
          {
          emit clicked(5);
          }
          @
          it depends from your code structure.

          1 Reply Last reply
          0

          • Login

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