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. Calling an intermediate function when connecting Signals and Slots
Forum Updated to NodeBB v4.3 + New Features

Calling an intermediate function when connecting Signals and Slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 968 Views 2 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.
  • S Offline
    S Offline
    Smeeth
    wrote on last edited by
    #1

    I am trying to connect the valueChanged() signal from QSlider to the setText() slot of a QLabel, but I want to call an intermediate function to format the string and check for some edge cases. For instance, if the QSlider is at its minimum value of 0, the text in the QLabel should be "---". not "0".

    Is there a way I can call this function with the int value from QSlider::valueChanged() signal, and then pass that value to the QLabel::setText() slot?

    I have tried this, but the syntax is not correct. (I get a "No such slot" error).

    connect(mySld, SIGNAL(valueChanged(int)), myLbl, SLOT(setText(getLabelText(int))));
    
    QString MyClass::getLabelText(int value){
        if (value > 0)
            return QString::number(value);
        else
            return "---";
    }
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can use a lambda for this:

      connect(mySld, &QSlider::valueChanged, myLbl,  [=](int v) { myLbl->setText(getLabelText(v)); });
      
      1 Reply Last reply
      8
      • S Offline
        S Offline
        Smeeth
        wrote on last edited by
        #3

        That works great! I was trying to stick to the updated SIGNAL/SLOT syntax but this does exactly what I needed it to do. Thank you.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          SIGNAL/SLOT macros are the remnant of the pre c++11 era. I would suggest to stick to the pointer based syntax - faster, more flexible and less error prone.

          1 Reply Last reply
          1
          • K Offline
            K Offline
            Kien Bui
            wrote on last edited by
            #5

            @Chris-Kawa said in Calling an intermediate function when connecting Signals and Slots:

            [=](int v) { myLbl->setText(getLabelText(v)); }

            I set config on .pro file

            CONFIG   += c++11
            

            But I still can't use SIGNAL/SLOT syntax with lambda

            mrjjM 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What error do you get? What Qt version do you use?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Kien-Bui SIGNAL/SLOT macro syntax is based on parsing strings. It doesn't work with lambdas.

                1 Reply Last reply
                2
                • K Kien Bui

                  @Chris-Kawa said in Calling an intermediate function when connecting Signals and Slots:

                  [=](int v) { myLbl->setText(getLabelText(v)); }

                  I set config on .pro file

                  CONFIG   += c++11
                  

                  But I still can't use SIGNAL/SLOT syntax with lambda

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Kien-Bui
                  hi
                  for an intro to the new syntax , please see here
                  https://wiki.qt.io/New_Signal_Slot_Syntax

                  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