To pass a string between functions
-
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
someLabelto 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
-
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
someLabelto 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
@russjohn834 hi
you can create a member QString variable and assign someLabel s value to it in your constructor
-
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
someLabelto 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
@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 declarationconst QString& someLabel. All you do is callQLabel::setText()on this, so see how https://doc.qt.io/qt-5/qlabel.html#text-prop showssetText(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. -
Thank you @LeLev , @JonB for your feedback