call a string from a method
-
Hello,
I want to call a string from another method :void ajouterproduit::on_loadPicture_clicked() { QString nameFile = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }I tried someting like :
void ajouterproduit::on_pushButton_2_clicked() { QString produit_image = nameFile ; }But it doesn't work.
Any idea ?
Thanks -
@Cervo2paille said in call a string from a method:
The error disappear, but the content of my variable is empty. It's normal, i define it in the function next to it.
And i don't know how to proceeded.Show us your updated header and
ajouterproduit::on_loadPicture_clicked()method. If you have the following in youron_loadPicture_clicked()method (or any other method of ajouterproduit) do you know what the output of each debug statement will be?nomFichier = "Hello"; QString nomFichier = "World"; qDebug() << "Local value: " << nomFichier; qDebug() << "Member value:" << this->nomFichier;or what it will be in another method if
on_loadPicture_clicked()has already been called:void ajouterproduit::other_method(){ qDebug() << "nomFichier: " << nomFichier; } -
Hello,
I want to call a string from another method :void ajouterproduit::on_loadPicture_clicked() { QString nameFile = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }I tried someting like :
void ajouterproduit::on_pushButton_2_clicked() { QString produit_image = nameFile ; }But it doesn't work.
Any idea ?
Thanks@Cervo2paille
Of course it does not work, ifnamefileis a local variable inside one method how do you expect it to be visible to the outside world?C++ basics: if you want a method to return a value make it return that value instead of being
void.If it's a slot, like you show, maybe store the chosen filename as a class member (in
ajouterproduit) and provide a getter method toreturnit to the outside world. Or, depending on usage, write a signal with a parameter of the filename which isemitted when the file is chosen so that the outside world can put a slot on that.Or, since both these methods are inside the same
ajouterproduitclass, just makenameFilea class variable and the second method will see the value the first method has previously set. -
Thanks for your precious help.
I'm in the last situation : the two functions are inside the same class.
I tried to to this :
in my .h :private: QString nomFichier;and in my .cpp
ajouterproduit::ajouterproduit(QWidget *parent) : QDialog(parent), ui(new Ui::ajouterproduit) { ui->setupUi(this); QString nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }But now my first function doesn't work.
My code can compile but the value of my QString when i click on the button is emp ty "".
Any idea?
Thanks -
Thanks for your precious help.
I'm in the last situation : the two functions are inside the same class.
I tried to to this :
in my .h :private: QString nomFichier;and in my .cpp
ajouterproduit::ajouterproduit(QWidget *parent) : QDialog(parent), ui(new Ui::ajouterproduit) { ui->setupUi(this); QString nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }But now my first function doesn't work.
My code can compile but the value of my QString when i click on the button is emp ty "".
Any idea?
Thanks@Cervo2paille said in call a string from a method:
Any idea?
Please, nicely, you need to brush up on some C++ basics:
private: QString nomFichier; ... method() { QString nomFichier = ... }Is that second
QString nomFichierreferring to the first one? (Hint: it is not.) It is a quite separate one. What do you have to do to make the second one use the first one? -
In fact...
This is my question : how can i do it ?
I can define my variable nomFichier at the opening of the QDialog, but the moment when i put the good value to this variable if when i use a clicked() function. Si i'm already in a function...
I don't know how to proceed.Thanks
-
In fact...
This is my question : how can i do it ?
I can define my variable nomFichier at the opening of the QDialog, but the moment when i put the good value to this variable if when i use a clicked() function. Si i'm already in a function...
I don't know how to proceed.Thanks
@Cervo2paille
You are asking how to access a class member variable inside the same class in C++. You cannot have got this far without knowing that. Are you asking how to read and set class variables? -
My problem is simple.
I got a value in this when i click on a button :void ajouterproduit::on_loadPicture_clicked() { QString nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }I want to get my nomFichier in another function, which is in the same file, same class :
void ajouterproduit::on_pushButton_2_clicked() { /*QString produit_image = nomFichier;*/ }But my second function doesn't see the first.
No maybe i don't know how to set and get variable in a same class.The difficulty in that i need to put the value in my function, and if i move it i got a problem : i don't get the value.
Someone can explain me exactly why i'm supposed to do ?Thanks
-
My problem is simple.
I got a value in this when i click on a button :void ajouterproduit::on_loadPicture_clicked() { QString nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }I want to get my nomFichier in another function, which is in the same file, same class :
void ajouterproduit::on_pushButton_2_clicked() { /*QString produit_image = nomFichier;*/ }But my second function doesn't see the first.
No maybe i don't know how to set and get variable in a same class.The difficulty in that i need to put the value in my function, and if i move it i got a problem : i don't get the value.
Someone can explain me exactly why i'm supposed to do ?Thanks
@Cervo2paille said in call a string from a method:
No maybe i don't know how to set and get variable in a same class.
Ok, then take a step backwards and search for a good c++ beginners book or online tutorial and learn about c++ member variables.
-
Ok thanks.
I found a course and i will try that.
My difficulty is that i create a variable and affect an ACTION in the same line :QString nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)"));How can i separate them ? thanks
-
Ok thanks.
I found a course and i will try that.
My difficulty is that i create a variable and affect an ACTION in the same line :QString nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)"));How can i separate them ? thanks
@Cervo2paille said in call a string from a method:
My difficulty is that i create a variable and affect an ACTION in the same line :
No, please learn c++
-
My problem is about QT, not C++.
If initialize my QString in my header, this starts the action because it's linked.
To dissociate the creation a my QString :QString nomFichierand the affectation that i gave it :
QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)"));The lesson of C++ doesn't help me to do that. And i need to give the access of this action to all my function in the same class.
Thanks
Edit : @mchinand thanks. In fact, i got an error. I can define in my .h this :
QString nomFichier;The error disappear, but the content of my variable is empty. It's normal, i define it in the function next to it.
And i don't know how to proceeded.
Define my variable in my .h using the affectation doesn't work : it launch at the started of my Qdialog, and i don't want it. (https://en.wikipedia.org/wiki/Member_variable)
Thanks. -
@Cervo2paille said in call a string from a method:
The error disappear, but the content of my variable is empty. It's normal, i define it in the function next to it.
And i don't know how to proceeded.Show us your updated header and
ajouterproduit::on_loadPicture_clicked()method. If you have the following in youron_loadPicture_clicked()method (or any other method of ajouterproduit) do you know what the output of each debug statement will be?nomFichier = "Hello"; QString nomFichier = "World"; qDebug() << "Local value: " << nomFichier; qDebug() << "Member value:" << this->nomFichier;or what it will be in another method if
on_loadPicture_clicked()has already been called:void ajouterproduit::other_method(){ qDebug() << "nomFichier: " << nomFichier; } -
God, Thanks.
I understand what was wrong in what i did...void ajouterproduit::on_loadPicture_clicked() { nomFichier = QFileDialog::getOpenFileName(this, tr("Choisir"), "", tr("Images(*.png *.jpg *.jpeg *.gif)")); }And define for all in my .h the QString.
Thanks again !
-
Hi,
@Cervo2paille said in call a string from a method:
My problem is about QT, not C++.
If initialize my QString in my header, this starts the action because it's linked.Class member variables declaration, their usage in functions and shadowing them are nothing Qt specific, it's pure C++ knowledge. The fact that yours is used in slots does not change that.
You also likely had warnings from the compiler about the the variable being declared again in a function.
Never ignore compiler warnings, they are bugs waiting to bite you.