Solved Question about function pointers in connect() and decltype
-
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.
-
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.
-
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);
-
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.
-
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. -
@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.
-
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.