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. Question about function pointers in connect() and decltype
Forum Updated to NodeBB v4.3 + New Features

Question about function pointers in connect() and decltype

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 619 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.
  • P Offline
    P Offline
    Petross404_Petros S
    wrote on 2 Dec 2019, 20:10 last edited by Petross404_Petros S 12 Feb 2019, 20:12
    #1

    I am messing with new connect syntax and I came upon this example:

    connect( _menuItemDrawThinFocus, SIGNAL(toggled(bool)), SLOT(updateChanged()) );
    // It becomes :
    connect( _menuItemDrawThinFocus, &QCheckBox::toggled, this, &StyleConfig::updateChanged);
    

    If I wasn't using an IDE with parsing and autocomplete and I could know what this or _menuItemDrawThinFocus were, how could I implement the syntax with decltype?

    connect( _menuItemDrawThinFocus, (decltype( _menuItemDrawThinFocus)::toggled), 
            this, &(decltype(&this)::updateChanged);
    

    Thank you.

    1 Reply Last reply
    1
    • F Offline
      F Offline
      fcarney
      wrote on 2 Dec 2019, 20:51 last edited by fcarney 12 Feb 2019, 20:58
      #3

      This works though:

      // remove pointer
       std::remove_pointer<decltype(ssptr)>::type sstype;
       QObject::connect(&sstest, &decltype(sstest)::sigOne, &sstest, &decltype(sstype)::slotOne);
      

      Edit:
      So:

          {
              SigSlotTest sstest;
              SigSlotTest *ssptr = &sstest;
              qInfo() << "Before Connect";
              sstest.emitSig();
      
              // remove pointer
              QObject::connect(&sstest, &decltype(sstest)::sigOne, &sstest, &std::remove_pointer<decltype(ssptr)>::type::slotOne);
              qInfo() << "Ater Connect";
              sstest.emitSig();
          }
      

      Ah, that is better...

      @Petross404_Petros-S thanks for asking this. I learned something useful.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fcarney
        wrote on 2 Dec 2019, 20:38 last edited by fcarney 12 Feb 2019, 20:45
        #2

        Wow, didn't know this would work.
        Test class:

        class SigSlotTest : public QObject
        {
            Q_OBJECT
        public:
            SigSlotTest(){}
        
            void emitSig(){
                emit sigOne();
            }
        
        signals:
            void sigOne();
        
        public slots:
            void slotOne(){
                qInfo() << "slotOne()";
            }
        };
        

        Test code:

            {
                SigSlotTest sstest;
                qInfo() << "Before Connect";
                sstest.emitSig();
                QObject::connect(&sstest, &decltype(sstest)::sigOne, &sstest, &decltype(sstest)::slotOne);
                qInfo() << "Ater Connect";
                sstest.emitSig();
            }
        

        Edit:
        Cant get it to work on a pointer though:

        SigSlotTest *ssptr = &sstest;
        QObject::connect(&sstest, &decltype(sstest)::sigOne, &sstest, &decltype(*ssptr)::slotOne);
        

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 2 Dec 2019, 20:51 last edited by fcarney 12 Feb 2019, 20:58
          #3

          This works though:

          // remove pointer
           std::remove_pointer<decltype(ssptr)>::type sstype;
           QObject::connect(&sstest, &decltype(sstest)::sigOne, &sstest, &decltype(sstype)::slotOne);
          

          Edit:
          So:

              {
                  SigSlotTest sstest;
                  SigSlotTest *ssptr = &sstest;
                  qInfo() << "Before Connect";
                  sstest.emitSig();
          
                  // remove pointer
                  QObject::connect(&sstest, &decltype(sstest)::sigOne, &sstest, &std::remove_pointer<decltype(ssptr)>::type::slotOne);
                  qInfo() << "Ater Connect";
                  sstest.emitSig();
              }
          

          Ah, that is better...

          @Petross404_Petros-S thanks for asking this. I learned something useful.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 2 Dec 2019, 21:41 last edited by
            #4

            Hi,

            While this is might be nice to have working, if you really need to use that in your connect statements, it like means that you are doing something wrong. People (and yourself) that are going to read that code are just going to be confused and the first thing they will have to do is track down the type of these variables.

            Also, if you don't know what class this is when writing your code, there's really an issue.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            F 1 Reply Last reply 2 Dec 2019, 21:52
            2
            • S SGaist
              2 Dec 2019, 21:41

              Hi,

              While this is might be nice to have working, if you really need to use that in your connect statements, it like means that you are doing something wrong. People (and yourself) that are going to read that code are just going to be confused and the first thing they will have to do is track down the type of these variables.

              Also, if you don't know what class this is when writing your code, there's really an issue.

              F Offline
              F Offline
              fcarney
              wrote on 2 Dec 2019, 21:52 last edited by
              #5

              @SGaist said in Question about function pointers in connect() and decltype:

              Also, if you don't know what class this is when writing your code, there's really an issue.

              I could see using this is templated code. Maybe for a pub/sub pattern.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              1
              • P Offline
                P Offline
                Petross404_Petros S
                wrote on 3 Dec 2019, 16:54 last edited by
                #6

                Thank you both for your insightful answers. The reason I am asking is because I was curious about the technical C++-related aspect of it. We all agree that not every trick and knowledge is applicable to serious code.

                It would be really nasty if the class whom a method you are tweaking is unknown to you.

                PS. I don't have time currently to test the answers so I may mark it as solved in a few hours.

                1 Reply Last reply
                0

                1/6

                2 Dec 2019, 20:10

                • Login

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