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 set value to a variable in QtObject::connect

How to set value to a variable in QtObject::connect

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.4k 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.
  • A Offline
    A Offline
    ARASHz4
    wrote on last edited by ARASHz4
    #1

    Hi i want set value to a variable in a connection
    this my code but don't work

    float size; 
    connect(GFS, SIGNAL (completed(float)), size = float);
    

    Is this possible?

    J.HilkJ 1 Reply Last reply
    0
    • A ARASHz4

      Hi i want set value to a variable in a connection
      this my code but don't work

      float size; 
      connect(GFS, SIGNAL (completed(float)), size = float);
      

      Is this possible?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @ARASHz4
      hi,

      yes, what you try to do can be done in QT, take a look at Lambda expressions in c++11 in here

      In the article you should find everything you need to make your code-example work :-)

      gl!


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • B Offline
        B Offline
        BePie
        wrote on last edited by BePie
        #3

        But where is the "size" ?
        Is it a member of a class that is QObject? If yes- the fastest way is to use lambda
        like this

        // code somewhere inside a scope of the QObject instance
        connect(sender, &Sender::someFloatDataChanged, [=](float newData) {
          this->a = newData;
        });
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          ARASHz4
          wrote on last edited by
          #4

          i do this before like this :

          connect(GFS, SIGNAL (completed(float)), this, SLOT (setSize(float)));
          

          completed signal have value & slot setSize get this value but i want set this value to a variable

          J.HilkJ 1 Reply Last reply
          0
          • A ARASHz4

            i do this before like this :

            connect(GFS, SIGNAL (completed(float)), this, SLOT (setSize(float)));
            

            completed signal have value & slot setSize get this value but i want set this value to a variable

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @ARASHz4

            @BePie more or less showed you how exactly to do it.

            But here again:

            First your variable to store/hold the data must either be declared in your header or in the function where you make the QObject::connect();

            float mySize; 
            

            than adjust your connect statement, first part - Pointer to Object and Signal - stays the same as always.

            connect(GFS, &myGfsClass::completed,
            ...
            

            than comes the lambda fuction in {}-brackets + correct syntax for your variable, the float from the completed SIGNAL.

            ...
            [=](float value){mySize =value;});
            

            and done.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BePie
              wrote on last edited by BePie
              #6

              so w/o explicit slot creation you can write:

              connect(GFS, &GFS_T::completed, [=](float newData){setSize(newData);}));
              

              where GFS_T is a type of GFS
              or paste code from setSize() to the body of the lambda

              A 1 Reply Last reply
              0
              • B BePie

                so w/o explicit slot creation you can write:

                connect(GFS, &GFS_T::completed, [=](float newData){setSize(newData);}));
                

                where GFS_T is a type of GFS
                or paste code from setSize() to the body of the lambda

                A Offline
                A Offline
                ARASHz4
                wrote on last edited by ARASHz4
                #7

                @BePie thank you but GFS is type of a class

                FileSize *GFS;
                
                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BePie
                  wrote on last edited by BePie
                  #8

                  so you write

                  connect(GFS, &FileSize::completed, [=](float newData){setSize(newData);}));
                  

                  so &FileSize::completed is a reference to signal-function of type (class) FileSize
                  the last argument is a lambda with copy ([=]) scope that takes one float (must match FileSize::completed arguments) and invokes setSize (implicitely of "this" pointer that is copied ([=])) with passed argument from signal.
                  I am not sure that I am 100% correct, I am not a pro lambda user. For more info check out some lambda tutorials.

                  but FileSize has to inherit from QObject (otherwise signal-slots won't magically work :P)

                  // edited: QOpenGL->QObject and added some notes

                  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