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. Passing variables while function is triggered by a slot
Forum Updated to NodeBB v4.3 + New Features

Passing variables while function is triggered by a slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 128 Views 2 Watching
  • 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. Per my understanding, many function calls in Qt can be replaced with signals and slots. I have the following code:

    void TestConfig::on_pushButton_New_clicked()
    {
     inputBox = new QDialog(this);
        //Set size of QDialog.
        QGridLayout *gridLayout = new QGridLayout(this);
        inputBox->setLayout(gridLayout);
    
        //QLabel
        QLabel *boxText = new QLabel(this);
        boxText->setText("Input new test presets:");
        boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
        boxText->setAlignment(Qt::AlignCenter);
        gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter);
    
        //Add a QLineEdit
        nameInput = new QLineEdit;
        gridLayout->addWidget(nameInput, 1, 1);
    
       //Add a QLineEdit
        startTempInput = new QLineEdit;
        gridLayout->addWidget(startTempInput, 2, 1);
    
        QLineEdit *expFlashptInput = new QLineEdit;
        gridLayout->addWidget(expFlashptInput, 3, 1);
    
        //Add a QDialogButtonBox
        QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this);
        gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter);
        //Read information
        connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig()));
        connect(boxButton, SIGNAL(accepted()), inputBox, SLOT(accept()));
    }
    
    void TestConfig::setTestConfig(){
        qDebug() << "In sTestC";
        int startTemp = startTempInput->text().toInt();
        int expFlashptTemp = expFlashptInput->text().toInt();
        qDebug() << startTemp;
        qDebug() << endTemp;
        ConfigLibrarian librarian;
        ConfigurationSet config;
        config.name = nameInput->text();
        config.serialNum = "desktop";
        config.startTemp = startTempInput->text().toInt();
        config.endTemp = 86;
        config.tempRate = 5.5;
        config.igniteFreq = 10;
        config.pressureThresh = 20;
        config.minTempAboveIni = 10;
    }
    

    In this code in order to pass data in startTempInput and expFlashptInput through, I had to declare them out of the function, which is something I am trying to avoid since the class they are built in is getting bigger. I was wondering whether there is a way to pass variables through to a slot when triggered by a signal.

    I am aware that in the documentation it is listed I can't do this:
    connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed(QObject*)));
    Hence, I am wondering whether a workaround that doesn't involve declaring the variables in the class.

    Please let me know if more info is required.

    sierdzioS 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      Hi. Per my understanding, many function calls in Qt can be replaced with signals and slots. I have the following code:

      void TestConfig::on_pushButton_New_clicked()
      {
       inputBox = new QDialog(this);
          //Set size of QDialog.
          QGridLayout *gridLayout = new QGridLayout(this);
          inputBox->setLayout(gridLayout);
      
          //QLabel
          QLabel *boxText = new QLabel(this);
          boxText->setText("Input new test presets:");
          boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
          boxText->setAlignment(Qt::AlignCenter);
          gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter);
      
          //Add a QLineEdit
          nameInput = new QLineEdit;
          gridLayout->addWidget(nameInput, 1, 1);
      
         //Add a QLineEdit
          startTempInput = new QLineEdit;
          gridLayout->addWidget(startTempInput, 2, 1);
      
          QLineEdit *expFlashptInput = new QLineEdit;
          gridLayout->addWidget(expFlashptInput, 3, 1);
      
          //Add a QDialogButtonBox
          QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this);
          gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter);
          //Read information
          connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig()));
          connect(boxButton, SIGNAL(accepted()), inputBox, SLOT(accept()));
      }
      
      void TestConfig::setTestConfig(){
          qDebug() << "In sTestC";
          int startTemp = startTempInput->text().toInt();
          int expFlashptTemp = expFlashptInput->text().toInt();
          qDebug() << startTemp;
          qDebug() << endTemp;
          ConfigLibrarian librarian;
          ConfigurationSet config;
          config.name = nameInput->text();
          config.serialNum = "desktop";
          config.startTemp = startTempInput->text().toInt();
          config.endTemp = 86;
          config.tempRate = 5.5;
          config.igniteFreq = 10;
          config.pressureThresh = 20;
          config.minTempAboveIni = 10;
      }
      

      In this code in order to pass data in startTempInput and expFlashptInput through, I had to declare them out of the function, which is something I am trying to avoid since the class they are built in is getting bigger. I was wondering whether there is a way to pass variables through to a slot when triggered by a signal.

      I am aware that in the documentation it is listed I can't do this:
      connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed(QObject*)));
      Hence, I am wondering whether a workaround that doesn't involve declaring the variables in the class.

      Please let me know if more info is required.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can use a lambda for that.

      connect(boxButton, &QButtonBox::accepted, this, [this, startTempInput, expFlashptInput]() {
          int startTemp = startTempInput->text().toInt();
          int expFlashptTemp = expFlashptInput->text().toInt();
          qDebug() << startTemp;
          qDebug() << endTemp;
          // ...
      });
      

      (Z(:^

      1 Reply Last reply
      4

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved