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 620 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.
  • Petross404_Petros SP Offline
    Petross404_Petros SP Offline
    Petross404_Petros S
    wrote on last edited by Petross404_Petros S
    #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
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #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
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by fcarney
        #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
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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

            fcarneyF 1 Reply Last reply
            2
            • SGaistS SGaist

              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.

              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on 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
              • Petross404_Petros SP Offline
                Petross404_Petros SP Offline
                Petross404_Petros S
                wrote on 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

                • Login

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