call a string from a method
-
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.