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. How to call a variable defined in one function in another function
Forum Updated to NodeBB v4.3 + New Features

How to call a variable defined in one function in another function

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 4.3k Views 4 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #6

    hi
    Just a note:
    The arguments for QProcess should be put in a list

    //Example
    QString program = "./path/to/srm";
    QStringList arguments;
    arguments << "file1" << "file 2";
    QProcess myProcess(parent);
    myProcess.start(program, arguments);

    since fileNames is such list, you can use it directly.
    as in
    myProcess.start(program, fileNames );

    Also sorry for cout. what just to write out the names to check
    for (int i = 0; i < fileNames.size(); ++i)
    cout << fileNames.at(i); // <<< this prints the entry [i] from the list

    Fred BarclayF 1 Reply Last reply
    2
    • mrjjM mrjj

      hi
      Just a note:
      The arguments for QProcess should be put in a list

      //Example
      QString program = "./path/to/srm";
      QStringList arguments;
      arguments << "file1" << "file 2";
      QProcess myProcess(parent);
      myProcess.start(program, arguments);

      since fileNames is such list, you can use it directly.
      as in
      myProcess.start(program, fileNames );

      Also sorry for cout. what just to write out the names to check
      for (int i = 0; i < fileNames.size(); ++i)
      cout << fileNames.at(i); // <<< this prints the entry [i] from the list

      Fred BarclayF Offline
      Fred BarclayF Offline
      Fred Barclay
      wrote on last edited by
      #7

      @mrjj Would this be correct? I'm just trying to get the contents of fileName echoed into a text file - before I try calling srm -r fileNames with another button.

      void Taz::on_chooseButton_clicked()
      {
      // New behavior
          QFileDialog dialog(this);
          dialog.setViewMode(QFileDialog::Detail);
          dialog.setFileMode(QFileDialog::ExistingFiles);
          QStringList fileNames;
          if (dialog.exec())
              fileNames = dialog.selectedFiles();
      
          QObject *parent;
          QString program = "/bin/echo";
          QStringList arguments;
          arguments << "fileNames" << ">> /home/fred/fn.txt";
          QProcess *myProcess = new QProcess(parent);
          myProcess->start(program, arguments);
      
      }
      

      When compiling with make I get a warning:

      taz.cpp: In member function ‘void Taz::on_chooseButton_clicked()’:
      taz.cpp:41:46: warning: ‘parent’ may be used uninitialized in this function [-Wmaybe-uninitialized]
           QProcess *myProcess = new QProcess(parent);
      

      The GUI does finish compiling, though. However, nothing is echoed into /home/fred/fn.txt. :(
      Thanks!

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #8

        Hi
        well, this is not ok.
        QObject *parent;
        ..
        QProcess *myProcess = new QProcess(parent);
        as parent is a dangling pointer so its invalid to do.
        ( its just points to random location)
        ( that is what it means with warning: ‘parent’ may be used uninitialized)
        so just use
        QProcess *myProcess = new QProcess(this); // this being Taz ( mainwinow i guess)

        also, you are not really processing the list
        but you should get "fileNames" in the fn.txt?

        maybe you can do
        fileNames << ">> /home/fred/fn.txt";
        and
        myProcess->start(program, fileNames );

        But in any case
        "fileNames" do not expand to the real list as it would in say bash.
        it just is text. like "hello".

        Fred BarclayF 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          well, this is not ok.
          QObject *parent;
          ..
          QProcess *myProcess = new QProcess(parent);
          as parent is a dangling pointer so its invalid to do.
          ( its just points to random location)
          ( that is what it means with warning: ‘parent’ may be used uninitialized)
          so just use
          QProcess *myProcess = new QProcess(this); // this being Taz ( mainwinow i guess)

          also, you are not really processing the list
          but you should get "fileNames" in the fn.txt?

          maybe you can do
          fileNames << ">> /home/fred/fn.txt";
          and
          myProcess->start(program, fileNames );

          But in any case
          "fileNames" do not expand to the real list as it would in say bash.
          it just is text. like "hello".

          Fred BarclayF Offline
          Fred BarclayF Offline
          Fred Barclay
          wrote on last edited by
          #9

          @mrjj Thanks - unallocated error fixed. You're right, I'm definitely not processing the list fileNames. I was just hoping I could get something (even the word "fileNames") to be echoed into a file.

          This yields nothing; is it what you are talking about?

          void Taz::on_chooseButton_clicked()
          {
          // New behavior
              QFileDialog dialog(this);
              dialog.setViewMode(QFileDialog::Detail);
              dialog.setFileMode(QFileDialog::ExistingFiles);
              QStringList fileNames;
              if (dialog.exec())
                  fileNames = dialog.selectedFiles();
          
              QString program = "/bin/echo";
              QStringList arguments;
              fileNames << ">> /home/fred/fn.txt";
              QProcess *myProcess = new QProcess(this);
              myProcess->start(program, fileNames );
          }
          

          BTW: here is my entire taz.cpp file: https://gist.github.com/Fred-Barclay/f0454d31681fe3744659cd18dfd62403

          and here is a screenshot (just in case...)
          0_1473369253927_taz.png

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #10

            Hi
            it looks ok now.

            you should use qDebug() to check stuff out
            (#include <QDebug>)

            and do
            qDebug() << " numfiles:" << fileNames.size();
            It will show in Creator in output window.
            Should give you a non zero value.

            also
            QProcess has a error() function u should also call to see if any errors.

            for test u can also try
            QProcess sh;
            sh.start("sh", QStringList() << "-c" << "ifconfig" << ">> /home/fred/fn.txt");

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

              Hi,

              IIRC, you should split >> and /home/fred/fn.txt and add both separately to your argument list.

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

              Fred BarclayF 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                IIRC, you should split >> and /home/fred/fn.txt and add both separately to your argument list.

                Fred BarclayF Offline
                Fred BarclayF Offline
                Fred Barclay
                wrote on last edited by
                #12

                @SGaist Hmm... that would make sense but even then nothing seems to happen when or after I press the button ("Choose File" in the screenshot above):

                void Taz::on_chooseButton_clicked()
                {
                // New behavior
                    QFileDialog dialog(this);
                    dialog.setViewMode(QFileDialog::Detail);
                    dialog.setFileMode(QFileDialog::ExistingFiles);
                    QStringList fileNames;
                    if (dialog.exec())
                        fileNames = dialog.selectedFiles();
                
                    QString program = "/bin/echo";
                    QStringList arguments;
                    arguments << "fileName" << ">>" << "/home/fred/fn.txt";
                    QProcess *myProcess = new QProcess(this);
                    myProcess->start(program, arguments);
                }
                
                jeremy_kJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  You should add some error checking to your code. That would help pinpoint the problem.

                  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
                  0
                  • Fred BarclayF Fred Barclay

                    @SGaist Hmm... that would make sense but even then nothing seems to happen when or after I press the button ("Choose File" in the screenshot above):

                    void Taz::on_chooseButton_clicked()
                    {
                    // New behavior
                        QFileDialog dialog(this);
                        dialog.setViewMode(QFileDialog::Detail);
                        dialog.setFileMode(QFileDialog::ExistingFiles);
                        QStringList fileNames;
                        if (dialog.exec())
                            fileNames = dialog.selectedFiles();
                    
                        QString program = "/bin/echo";
                        QStringList arguments;
                        arguments << "fileName" << ">>" << "/home/fred/fn.txt";
                        QProcess *myProcess = new QProcess(this);
                        myProcess->start(program, arguments);
                    }
                    
                    jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote on last edited by
                    #14

                    @Fred-Barclay said in How to call a variable defined in one function in another function:

                    @SGaist Hmm... that would make sense but even then nothing seems to happen when or after I press the button ("Choose File" in the screenshot above):

                    void Taz::on_chooseButton_clicked()
                    {
                    // New behavior
                        QFileDialog dialog(this);
                        dialog.setViewMode(QFileDialog::Detail);
                        dialog.setFileMode(QFileDialog::ExistingFiles);
                        QStringList fileNames;
                        if (dialog.exec())
                            fileNames = dialog.selectedFiles();
                    
                        QString program = "/bin/echo";
                        QStringList arguments;
                        arguments << "fileName" << ">>" << "/home/fred/fn.txt";
                        QProcess *myProcess = new QProcess(this);
                        myProcess->start(program, arguments);
                    }
                    

                    Is the expectation that "fileName" will be appended to the file /home/fred/fn.txt?

                    Neither echo nor QProcess have special handling for ">>" as far as I am aware. Redirection to a file using this syntax is usually the domain of a shell such as bash. QProcess either redirects output to the enclosing program's output, or makes it available via QProcess::readChannel().

                    What you'll get instead is the string "fileName >> /home/fred/fn.txt" written to standard output.

                    #include <QCoreApplication>
                    #include <iostream>
                    #include <QProcess>
                    #include <QObject>
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication a(argc, argv);
                        QProcess p;
                        p.setProcessChannelMode(QProcess::ForwardedChannels);
                        QStringList arguments;
                        arguments << "fileName" << ">>" << "/home/fred/fn.txt";
                        QObject::connect<void(QProcess::*)(int)>(&p, &QProcess::finished, [=](){ std::cout << "process done" << std::endl; });
                        p.start("/bin/echo", arguments);
                    
                        return a.exec();
                    }
                    

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mjsurette
                      wrote on last edited by mjsurette
                      #15

                      Hi. For a non c++ programmer, you seem to be catching on. Your posting of...

                      void Taz::on_chooseButton_clicked()
                      {
                      // New behavior
                          QFileDialog dialog(this);
                          dialog.setViewMode(QFileDialog::Detail);
                          dialog.setFileMode(QFileDialog::ExistingFiles);
                          QStringList fileNames;
                          if (dialog.exec())
                              fileNames = dialog.selectedFiles();
                      
                          QString program = "/bin/echo";
                          QStringList arguments;
                          arguments << "fileName" << ">>" << "/home/fred/fn.txt";
                          QProcess *myProcess = new QProcess(this);
                          myProcess->start(program, arguments);
                      }
                      

                      isn't too bad. Just a little overly complex, so this

                      void Taz::on_chooseButton_clicked()
                      {
                      // New behavior
                          QFileDialog dialog(this);
                          dialog.setViewMode(QFileDialog::Detail);
                          dialog.setFileMode(QFileDialog::ExistingFiles);
                          QStringList fileNames;
                          if (dialog.exec())
                          {
                              fileNames = dialog.selectedFiles();
                              QProcess::execute("echo", fileNames);
                          }
                      }
                      

                      should echo the filenames to your Application Output screen. This is actually a lot closer to what you're end game should look like.

                      HTH

                      Mike

                      1 Reply Last reply
                      0

                      • Login

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