Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How do SIGNAL or SLOT know which object I am referring to in a connect function?
Forum Updated to NodeBB v4.3 + New Features

How do SIGNAL or SLOT know which object I am referring to in a connect function?

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
2 Posts 2 Posters 418 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.
  • T Offline
    T Offline
    TheTobruk
    wrote on last edited by
    #1

    I'm coming from C++ area so may be a QT idiosyncrasy, but I noticed something strange to me and wanted to ask you about it.

    In this code:

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
            ui->progressBar, SLOT(setValue(int)));
    

    how do SIGNAL or SLOT know which object I am referring to? Is this some sort of a macro that looks up the previous argument for memory address? How does it inferr that valueChanged is a function of horizontalSlider, or that setValue is a function of progressBar?

    Shouldn't it look like this?

    connect(ui->horizontalSlider, SIGNAL(ui->horizontalSlider->valueChanged(int)),
            ui->progressBar, SLOT(ui->progressBar->setValue(int)));
    

    Thanks for any explanations :)

    raven-worxR 1 Reply Last reply
    0
    • T TheTobruk

      I'm coming from C++ area so may be a QT idiosyncrasy, but I noticed something strange to me and wanted to ask you about it.

      In this code:

      connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
              ui->progressBar, SLOT(setValue(int)));
      

      how do SIGNAL or SLOT know which object I am referring to? Is this some sort of a macro that looks up the previous argument for memory address? How does it inferr that valueChanged is a function of horizontalSlider, or that setValue is a function of progressBar?

      Shouldn't it look like this?

      connect(ui->horizontalSlider, SIGNAL(ui->horizontalSlider->valueChanged(int)),
              ui->progressBar, SLOT(ui->progressBar->setValue(int)));
      

      Thanks for any explanations :)

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by aha_1980
      #2

      @TheTobruk said in How do SIGNAL or SLOT know which object I am referring to in a connect function?:

      connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
      ui->progressBar, SLOT(setValue(int)));

      SIGNAL() / SLOT() are macros which return a char*, which is then used by Qt (MOC) for static lookup in a list. So the (cleaned up) method name is searched in the meta-object of the object you passed in the parameter before.

      You may also checkout the new signa/slot syntax (introduced with Qt5), which also accepts function pointers.
      The old syntax is basically the same, but the "dereferencing" of the function is done each time during runtime and also string based.

      connect(ui->horizontalSlider, SIGNAL(ui->horizontalSlider->valueChanged(int)),
      ui->progressBar, SLOT(ui->progressBar->setValue(int)));

      following your thinking this would actually call the method, which isn't intented.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3

      • Login

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