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. To pass a string between functions
Forum Update on Monday, May 27th 2025

To pass a string between functions

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 438 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.
  • R Offline
    R Offline
    russjohn834
    wrote on 19 Nov 2019, 10:36 last edited by
    #1

    Hi all,

    I have a basic c++ question:

    I have a class constructor as follows

    StageOne::StageOne(QString someLabel, QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::StageOne)
    {
        ui->setupUi(this);
    
        ui->label->setText(someLabel);
        ui->label->setAlignment(Qt::AlignCenter);
    
    }
    

    How do I pass someLabel to other parts of the class, for example, if I need to reuse it in the following function:

    void StageOne::on_pushButton_One_clicked()
    {
        //----->How do I get "someLabel" from the class constructor here
        
        this->close();
        settingsWindow = new  configsettings (this);
        settingsWindow ->setWindowTitle("Settings window");
        settingsWindow -> show();
    }
    

    Thanks in advance

    O J 2 Replies Last reply 19 Nov 2019, 10:45
    0
    • R russjohn834
      19 Nov 2019, 10:36

      Hi all,

      I have a basic c++ question:

      I have a class constructor as follows

      StageOne::StageOne(QString someLabel, QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::StageOne)
      {
          ui->setupUi(this);
      
          ui->label->setText(someLabel);
          ui->label->setAlignment(Qt::AlignCenter);
      
      }
      

      How do I pass someLabel to other parts of the class, for example, if I need to reuse it in the following function:

      void StageOne::on_pushButton_One_clicked()
      {
          //----->How do I get "someLabel" from the class constructor here
          
          this->close();
          settingsWindow = new  configsettings (this);
          settingsWindow ->setWindowTitle("Settings window");
          settingsWindow -> show();
      }
      

      Thanks in advance

      O Offline
      O Offline
      ODБOï
      wrote on 19 Nov 2019, 10:45 last edited by
      #2

      @russjohn834 hi

      you can create a member QString variable and assign someLabel s value to it in your constructor

      1 Reply Last reply
      5
      • R russjohn834
        19 Nov 2019, 10:36

        Hi all,

        I have a basic c++ question:

        I have a class constructor as follows

        StageOne::StageOne(QString someLabel, QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::StageOne)
        {
            ui->setupUi(this);
        
            ui->label->setText(someLabel);
            ui->label->setAlignment(Qt::AlignCenter);
        
        }
        

        How do I pass someLabel to other parts of the class, for example, if I need to reuse it in the following function:

        void StageOne::on_pushButton_One_clicked()
        {
            //----->How do I get "someLabel" from the class constructor here
            
            this->close();
            settingsWindow = new  configsettings (this);
            settingsWindow ->setWindowTitle("Settings window");
            settingsWindow -> show();
        }
        

        Thanks in advance

        J Offline
        J Offline
        JonB
        wrote on 19 Nov 2019, 10:51 last edited by JonB
        #3

        @russjohn834
        As @LeLev has written if you need to reference a parameter all over the place in a class, copy it to a member variable for re-use.

        While we are here:

        StageOne::StageOne(QString someLabel, QWidget *parent) :

        The usual/optimal way of passing QStrings around which you do not intend to alter is via parameter declaration const QString& someLabel. All you do is call QLabel::setText() on this, so see how https://doc.qt.io/qt-5/qlabel.html#text-prop shows setText(const QString &). It's not vital, but if you are going to end up writing loads of code like this you might wish to change your declarations over now.

        1 Reply Last reply
        3
        • R Offline
          R Offline
          russjohn834
          wrote on 19 Nov 2019, 11:33 last edited by
          #4

          Thank you @LeLev , @JonB for your feedback

          1 Reply Last reply
          0

          1/4

          19 Nov 2019, 10:36

          • Login

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