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. What does this code block mean?
Qt 6.11 is out! See what's new in the release blog

What does this code block mean?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 625 Views
  • 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.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by
    #1
        AnalTask *analTask = new AnalTask();
        analTask->setOptions(options);
    
        MainWindow *main = this->main;
        connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
        connect(analTask, &AsyncTask::finished, main, [analTask, main]() {
            if (analTask->getOpenFileFailed()) {
                return;
            }
            main->finalizeOpen();
        });
    

    Hi,
    I see such code block in a project, and cannot understand the second connect() function, I didn't see such code before. Can someone tell me what does "analTask, main{}" means?

    Thanks!

    jsulmJ JonBJ 2 Replies Last reply
    0
    • M Mr Pang
          AnalTask *analTask = new AnalTask();
          analTask->setOptions(options);
      
          MainWindow *main = this->main;
          connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
          connect(analTask, &AsyncTask::finished, main, [analTask, main]() {
              if (analTask->getOpenFileFailed()) {
                  return;
              }
              main->finalizeOpen();
          });
      

      Hi,
      I see such code block in a project, and cannot understand the second connect() function, I didn't see such code before. Can someone tell me what does "analTask, main{}" means?

      Thanks!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mr-Pang You should read about C++ lambdas: https://en.cppreference.com/w/cpp/language/lambda

      connect(analTask, &AsyncTask::finished, main, [analTask, main]() {
              if (analTask->getOpenFileFailed()) {
                  return;
              }
              main->finalizeOpen();
          });
      

      In this connect the last parameter is a lambda function. This means it is a function without a function name. In this case this function gets no parameters, but analTask and main are captured and are available inside the lambda function.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      5
      • M Mr Pang
            AnalTask *analTask = new AnalTask();
            analTask->setOptions(options);
        
            MainWindow *main = this->main;
            connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
            connect(analTask, &AsyncTask::finished, main, [analTask, main]() {
                if (analTask->getOpenFileFailed()) {
                    return;
                }
                main->finalizeOpen();
            });
        

        Hi,
        I see such code block in a project, and cannot understand the second connect() function, I didn't see such code before. Can someone tell me what does "analTask, main{}" means?

        Thanks!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Mr-Pang said in What does this code block mean?:

        what does "analTask, main{}" means

        :) [Sorry!]

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Mr-Pang You should read about C++ lambdas: https://en.cppreference.com/w/cpp/language/lambda

          connect(analTask, &AsyncTask::finished, main, [analTask, main]() {
                  if (analTask->getOpenFileFailed()) {
                      return;
                  }
                  main->finalizeOpen();
              });
          

          In this connect the last parameter is a lambda function. This means it is a function without a function name. In this case this function gets no parameters, but analTask and main are captured and are available inside the lambda function.

          M Offline
          M Offline
          Mr Pang
          wrote on last edited by
          #4

          @jsulm
          Great!
          Thanks!

          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