Question about function pointers in connect() and decltype
-
wrote on 2 Dec 2019, 20:10 last edited by Petross404_Petros S 12 Feb 2019, 20:12
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.
-
wrote on 2 Dec 2019, 20:51 last edited by fcarney 12 Feb 2019, 20:58
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.
-
wrote on 2 Dec 2019, 20:38 last edited by fcarney 12 Feb 2019, 20:45
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);
-
wrote on 2 Dec 2019, 20:51 last edited by fcarney 12 Feb 2019, 20:58
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. -
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.wrote on 2 Dec 2019, 21:52 last edited by@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.
-
wrote on 3 Dec 2019, 16:54 last edited by
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/6