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. QProcess::start doesn't display a console window for console programs (Windows)

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

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 18.8k Views
  • 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 Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #3

    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
    0
    • Z Offline
      Z Offline
      zeljko
      wrote on last edited by
      #4

      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
      0
      • Z Offline
        Z Offline
        zeljko
        wrote on last edited by
        #5

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

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by
          #6

          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
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #7

            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
            0
            • V Offline
              V Offline
              Violet Giraffe
              wrote on last edited by
              #8

              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
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #9

                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
                0
                • V Offline
                  V Offline
                  Violet Giraffe
                  wrote on last edited by
                  #10

                  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
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #11

                    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
                    0
                    • V Offline
                      V Offline
                      Violet Giraffe
                      wrote on last edited by
                      #12

                      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
                      0
                      • M Offline
                        M Offline
                        MuldeR
                        wrote on last edited by
                        #13

                        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
                        0
                        • V Offline
                          V Offline
                          Violet Giraffe
                          wrote on last edited by
                          #14

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

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            Violet Giraffe
                            wrote on last edited by
                            #15

                            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
                            0
                            • M Offline
                              M Offline
                              MuldeR
                              wrote on last edited by
                              #16

                              [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
                              0
                              • V Offline
                                V Offline
                                Violet Giraffe
                                wrote on last edited by
                                #17

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

                                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