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. QObject::connect does not execute a function in SLOT when it is declared as a lambda
Forum Updated to NodeBB v4.3 + New Features

QObject::connect does not execute a function in SLOT when it is declared as a lambda

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 254 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.
  • T Offline
    T Offline
    TrofikdofiK
    wrote on last edited by
    #1

    I have a function with the following code

    void MainWindow::openSettingWindow() noexcept
    {
    	QWidget* settingWidget = new QWidget();
    	settingWidget->setAttribute(Qt::WA_DeleteOnClose);
    	settingWidget->setFixedSize(200, 190);
    	settingWidget->setStyleSheet("QWidget {background-color: red;}");
    
    	QLabel* upLabel = new QLabel("Вверх", settingWidget);
    	setStyleLabelSetting(*upLabel, 20);
    
    	QPushButton* keyUp = new QPushButton("W", settingWidget);
    	setStyleKeyLabelSetting(*keyUp, 20);
    
    	QObject::connect(keyUp, SIGNAL(clicked()), SLOT([keyUp]()
    		{
    			keyUp->setText("");
    		}
    	));
    
    	settingWidget->show();
    }
    

    When I run it lambda is not executed. If I set a breakpoint, it says that the code is not running.
    d67c654f-b4fc-455c-8b2b-3b5fe427bb36-image.png
    How to fix this?

    Christian EhrlicherC 1 Reply Last reply
    0
    • T TrofikdofiK

      I have a function with the following code

      void MainWindow::openSettingWindow() noexcept
      {
      	QWidget* settingWidget = new QWidget();
      	settingWidget->setAttribute(Qt::WA_DeleteOnClose);
      	settingWidget->setFixedSize(200, 190);
      	settingWidget->setStyleSheet("QWidget {background-color: red;}");
      
      	QLabel* upLabel = new QLabel("Вверх", settingWidget);
      	setStyleLabelSetting(*upLabel, 20);
      
      	QPushButton* keyUp = new QPushButton("W", settingWidget);
      	setStyleKeyLabelSetting(*keyUp, 20);
      
      	QObject::connect(keyUp, SIGNAL(clicked()), SLOT([keyUp]()
      		{
      			keyUp->setText("");
      		}
      	));
      
      	settingWidget->show();
      }
      

      When I run it lambda is not executed. If I set a breakpoint, it says that the code is not running.
      d67c654f-b4fc-455c-8b2b-3b5fe427bb36-image.png
      How to fix this?

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @TrofikdofiK said in QObject::connect does not execute a function in SLOT when it is declared as a lambda:

      How to fix this?

      Fix your QObject::connect() by using the new signal/slot syntax. Lambdas can't work with the old-style connect.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        @TrofikdofiK said in QObject::connect does not execute a function in SLOT when it is declared as a lambda:

        How to fix this?

        Fix your QObject::connect() by using the new signal/slot syntax. Lambdas can't work with the old-style connect.

        T Offline
        T Offline
        TrofikdofiK
        wrote on last edited by
        #3

        @Christian-Ehrlicher 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