Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Confused about how connect is passing argument to a function



  • I'm stilling a novice at Qt, and am totally at a loss with the following.

    I have this function:

    void OAuth2Manager::onAuthorizeWithBrowser(const QUrl &url)
    {
       qDebug() << "OAuth2Manager::onAuthorizeWithBrowser:" << url;
    }
    

    The function is only referenced as follows:

    _oAuth2 = new QOAuth2AuthorizationCodeFlow( this);
    
    connect (_oAuth2,
             &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
             this,
             &OAuth2Manager::onAuthorizeWithBrowser);
    

    How does it get the function get the url argument passed in?



  • Although I'm somewhat still confused about the inner working of the slot/signal/emit and how it works with such argument passing, I managed to the functional problem that was causing me to delve into these murky waters.

    For anyone interested see this post for the details:

    https://forum.qt.io/topic/124685/qabstractoauth2-missing-access_type-and-approval_prompt

    As for the matter to hand here, I'm assuming that there emit is buried with the QOAuth2AuthorizationCodeFlow class somewhere which is why I cannot see it.

    I'm closing this as answered.



  • from the function sig. , see functor-based connection

    In contrast, functor-based connections are checked by the compiler. The compiler catches errors at compile-time, enables implicit conversions between compatible types, and recognizes different names of the same type.



  • @6thC Thank you. If I understand you correctly the code above should cause a compilation error, but it doesn't. The code compiles, builds and runs! My issue is I need to make a change (to the url value) but I cannot seem to figure out where this value is set, comes from or how it ends up in this function. Although I am a novice at Qt, I do know my way around programming languages so I think I am drowning in some esoteric waters here.



  • @TenG That url is emitted by _oAuth2 through the authorizeWithBrowser signal, that is, internally that class processes the requests and when it gets the url then it sends it through that signal. I recommend you read https://doc.qt.io/qt-5/signalsandslots.html. I think you have an XY problem.



  • @eyllanesc Thank you. But there must be somewhere whereby something knows/is instructed to pass a variable to that function. I am having difficult trying to trace which source variable it is. I do not understand what the term XY means but I'll try reading the documentation you recommended.


  • Lifetime Qt Champion

    @TenG You need to search for the place where the signal authorizeWithBrowser is emitted, then you will know from where the value for the parameter is coming. So, simply grep in the source code for authorizeWithBrowser.



  • @TenG said in Confused about how connect is passing argument to a function:

    I do not understand what the term XY means but I'll try reading the documentation you recommended.

    YX Problem: You have a problem "X", but asking to do "Y" because you are thinking this will solve your issue "X".
    ==> Right way would be to expose the orignal problem "X".



  • Although I'm somewhat still confused about the inner working of the slot/signal/emit and how it works with such argument passing, I managed to the functional problem that was causing me to delve into these murky waters.

    For anyone interested see this post for the details:

    https://forum.qt.io/topic/124685/qabstractoauth2-missing-access_type-and-approval_prompt

    As for the matter to hand here, I'm assuming that there emit is buried with the QOAuth2AuthorizationCodeFlow class somewhere which is why I cannot see it.

    I'm closing this as answered.



  • @TenG said in Confused about how connect is passing argument to a function:

    Although I'm somewhat still confused about the inner working of the slot/signal/emit and how it works with such argument passing

    That is bad, because signal/slot mechanism is a fundamental functionality of Qt and it is very important to understand it.
    It is not so hard to understand:

    • first, signals, slots and emit are code markup extension used by Qt. In fact they are defines to nothing (check qobjectdefs.h), only signals is a define to public. All signals generated function have to be public for the moc (see bellow)
    • signals and slots are working only with C++ instances which are base on QObject or a derived class with definition of macro Q_OBJECT in his header.
    • during compilation, each header of QObject based class in "pre-compiled" with moc (Meta-Object-Compiler ==> https://doc.qt.io/qt-5/why-moc.html). This step will generate an additional cpp file with all the "magic code" needed to handle signals/slots. This step is automatically done by QMake/CMake and the generated c++ files are also add to the final Makefile.

    The code generated, is the code behind the signals functions which you have defined in your class header.
    This is why the emit keyword you have added in the C++ code is not necessary, it is only for the developer to identify when a signal is emitted.

    So emitting a signal is in fact a function call.




Log in to reply