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. call a string from a method
Qt 6.11 is out! See what's new in the release blog

call a string from a method

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.7k 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.
  • C Offline
    C Offline
    Cervo2paille
    wrote on last edited by
    #5

    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

    JonBJ 1 Reply Last reply
    0
    • C Cervo2paille

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #6

      @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?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cervo2paille
        wrote on last edited by
        #7

        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

        Christian EhrlicherC 1 Reply Last reply
        0
        • C Cervo2paille

          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

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • C Offline
            C Offline
            Cervo2paille
            wrote on last edited by
            #9

            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

            Christian EhrlicherC 1 Reply Last reply
            0
            • C Cervo2paille

              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

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @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++

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mchinand
                wrote on last edited by
                #11

                You have what's called a local shadow variable. If you're using QtCreator, it probably even shows you a warning regarding it.

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  Cervo2paille
                  wrote on last edited by Cervo2paille
                  #12

                  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 nomFichier
                  

                  and 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.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mchinand
                    wrote on last edited by mchinand
                    #13

                    @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 your on_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;
                    }
                    
                    1 Reply Last reply
                    2
                    • C Offline
                      C Offline
                      Cervo2paille
                      wrote on last edited by
                      #14

                      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 !

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2

                        • Login

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