Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    QProcess::start doesn't display a console window for console programs (Windows)

    General and Desktop
    3
    17
    17026
    Loading More Posts
    • 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.
    • V
      Violet Giraffe last edited by

      @QProcess * process = new QProcess;
      process->start("cmd.exe");@

      At first I thought cmd doesn't start at all, but then I noticed it running in background. There's just no window, and it seems to be a common behavior for all console programs on Windows. Can I make QProcess not inhibit the window? Using QProcess::startDetached is problematic because it doesn't allow for output redirection.

      1 Reply Last reply Reply Quote 0
      • Z
        zeljko last edited by

        Maybe this could help:
        https://qt-project.org/forums/viewthread/30083

        1 Reply Last reply Reply Quote 0
        • V
          Violet Giraffe last edited by

          I've had the same idea, trying cmd /C right now. Still having lots of problems. cmd.exe itself does launch, but it seems to ignore the command I want it to execute...

          1 Reply Last reply Reply Quote 0
          • Z
            zeljko last edited by

            Maybe this one:
            http://stackoverflow.com/questions/13467680/qprocess-cannot-write-to-cmd-exe
            It's python but it's easy to convert it to pure c++.

            1 Reply Last reply Reply Quote 0
            • Z
              zeljko last edited by

              btw. Have you tried QProcess::startDetached("cmd.exe"); ?

              1 Reply Last reply Reply Quote 0
              • V
                Violet Giraffe last edited by

                I haven't because I needed to use output redirection (setStandardOutputFile), but now that you've asked I realized that with "cmd /C start" strategy I can use that. But on the other hand, with that strategy I actually shouldn't use startDetached because I'll get two windows: one from cmd and one from the actual command.

                On a second thought, with "cmd /C" and startDetached I won't need to add "start". That's interesting, trying it out right now.

                1 Reply Last reply Reply Quote 0
                • M
                  MuldeR last edited by

                  QProcess is intended to run a "slave" process, so it won't have a console. Would look rather strange, if run a (GUI) program and suddenly all kinds of console windows pop up, right? Instead, the console is hidden and the STDOUT and STDERR streams are redirected by QProcess, so your "main" application can read them. If the child process had a console window, it would be empty anyway, because of the STDOUT/STDERR redirection.

                  Now you say that that you do need output redirection. So QProcess should do exactly what you want. Also, with output redirection enabled, what exactly would you have expected to appear in the child's console window?

                  __

                  BTW: If your "main" application is a console application and thus has its own console, you could still read the child process' STDOUT and/or STDERR from the QProcess object and then write that to your own console window.

                  My OpenSource software at: http://muldersoft.com/

                  Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                  Go visit the coop: http://youtu.be/Jay...

                  1 Reply Last reply Reply Quote 0
                  • V
                    Violet Giraffe last edited by

                    I see your point. I already figured that bare QProcess won't do what I need, so I'm experimenting with QProcess::startDetached("cmd.exe", QStringList() << "/C" << command, workingDir) strategy.

                    1 Reply Last reply Reply Quote 0
                    • M
                      MuldeR last edited by

                      So what is it that you want exactly? I still don't quite get it...

                      Why not something like:
                      @m_process.setProcessChannelMode(QProcess::MergedChannels);
                      m_process.setReadChannel(QProcess::StandardOutput);
                      connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput()));
                      m_process.start( ... );@
                      @MyClass::processOutput()
                      {
                      while(!m_process.canReadLine())
                      {
                      QByteArray line = m_process.readLine();
                      printf("[childproc] %s\n", line.constData());
                      }
                      }@

                      My OpenSource software at: http://muldersoft.com/

                      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                      Go visit the coop: http://youtu.be/Jay...

                      1 Reply Last reply Reply Quote 0
                      • V
                        Violet Giraffe last edited by

                        I want to have a command line in my program that would act exactly as if the commands are executed in a shell, with shell itself being launched implicitly. so if I want to launch any program I just type its name, press Enter and its window should appear, no matter console or GUI. But I should also be able to redirect output to a file with > symbol.

                        1 Reply Last reply Reply Quote 0
                        • M
                          MuldeR last edited by

                          That sounds like you are trying to workaround all the "services" QProcess offers and you just want the plain "system()":http://www.cplusplus.com/reference/cstdlib/system/ command...

                          My OpenSource software at: http://muldersoft.com/

                          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                          Go visit the coop: http://youtu.be/Jay...

                          1 Reply Last reply Reply Quote 0
                          • V
                            Violet Giraffe last edited by

                            std::system would be good because I don't have to guess what shell the OS uses or which flags I need to execute my commands, but I can't set a working dir. That's crucial.

                            1 Reply Last reply Reply Quote 0
                            • M
                              MuldeR last edited by

                              system() probably uses the working dir establish by chdir().

                              But if that doesn't work, you could also simply use a command like:
                              @"pushd c:\my_working_dir && do_something"@

                              The system() string is passed to the shell, so all shell commands work.

                              __

                              Anyway, if you decide to run "cmd.exe" manually, you should be using:
                              "%ComSpec%":http://en.wikipedia.org/wiki/ComSpec

                              My OpenSource software at: http://muldersoft.com/

                              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                              Go visit the coop: http://youtu.be/Jay...

                              1 Reply Last reply Reply Quote 0
                              • V
                                Violet Giraffe last edited by

                                Any specific reason you've suggested pushd and not cd?

                                1 Reply Last reply Reply Quote 0
                                • V
                                  Violet Giraffe last edited by

                                  std::system doesn't cut it for me because it blocks until the shell exits, which is unacceptable. And I don't see a way to pass non-ASCII paths to it. So I'll have to use QProcess::startDetached after all.

                                  1 Reply Last reply Reply Quote 0
                                  • M
                                    MuldeR last edited by

                                    [quote author="Violet Giraffe" date="1399141297"]Any specific reason you've suggested pushd and not cd?[/quote]

                                    Because "cd" only changes the path, not the drive. At least on Windows.

                                    At the same time "push" changes the path and drive, if required.

                                    [quote author="Violet Giraffe" date="1399148562"]std::system doesn't cut it for me because it blocks until the shell exits, which is unacceptable. And I don't see a way to pass non-ASCII paths to it. So I'll have to use QProcess::startDetached after all.[/quote]

                                    You could invoke it in a separate thread, so it doesn't block. And if you need Unicode support, just use _wsystem() on Windows with an UTF16 string. On Linux you can use the standard system() with an UTF8 string.

                                    My OpenSource software at: http://muldersoft.com/

                                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                                    Go visit the coop: http://youtu.be/Jay...

                                    1 Reply Last reply Reply Quote 0
                                    • V
                                      Violet Giraffe last edited by

                                      Thanks! I've googled for wide-string analog of system(), but couldn't find it.

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post