Article View: pl.comp.programming
Article #217768Re: okienko z cmd.exe w Qt C++
From: Adam M
Date: Fri, 15 Apr 2022 13:53
Date: Fri, 15 Apr 2022 13:53
48 lines
2934 bytes
2934 bytes
On Thursday, April 7, 2022 at 8:26:19 AM UTC-4, Jivanmukta wrote:
> Używam C++ i Qt5.
> Potrzebuje uruchomić pewien program konsolowy z argumentami ale tak,
> żeby było widać okienko cmd.exe z uruchomionym tym programem.
> Próbowałem tak:
>
> std::string terminal = exec_system(L"echo %windir%") +
> "\\system32\\cmd.exe";
> terminal = str_replace("\n", "", str_replace(END_LINE, "", terminal));
> QProcess *process = new QProcess();
> QString exec = QString::fromStdString(terminal);
> QStringList params;
> params << "/C";
> for (std::string s : explode(std::string(" "), command)) {
> params << str_replace("\"", "", s).c_str();
> }
> process->start(exec, params);
> process->waitForFinished();
> delete process;
>
> ale niestety okienko cmd.exe się nie wyświetla.
> Tzn. pojawia się na jakiś czas klepsydra, ale okienka nie ma.
>
> Jak to zrobić?
Do pliku pro dodaj:
CONFIG += console
do pliku CPP ktory startuje twoja aplikacje dodaj
#include <windows.h>
#include <stdio.h>
w funkcji main(..) dodaj
// detach from the current console window
// if launched from a console window, that will still run waiting for the new console (below) to close
// it is useful to detach from Qt Creator's <Application output> panel
FreeConsole();
// create a separate new console window
AllocConsole();
// attach the new console to this application's process
AttachConsole(GetCurrentProcessId());
// reopen the std I/O streams to redirect I/O to the new console
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
freopen("CON", "r", stdin);
Technical Details
Message-ID:
<6a7a1ee7-e05e-43a8-a5ca-cbeec6201cc4n@googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!2.eu.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mailReferences:
<t2ml98$3a56e$2@portraits.wsisiz.edu.pl>Show Full Headers
Complete Article Headers
X-Received: by 2002:a05:620a:2552:b0:67b:32e2:2400 with SMTP id s18-20020a05620a255200b0067b32e22400mr5395315qko.768.1650030789049; Fri, 15 Apr 2022 06:53:09 -0700 (PDT) X-Received: by 2002:a05:6214:c44:b0:444:461e:7dc0 with SMTP id r4-20020a0562140c4400b00444461e7dc0mr7548125qvj.61.1650030788784; Fri, 15 Apr 2022 06:53:08 -0700 (PDT) Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!2.eu.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: pl.comp.programming Date: Fri, 15 Apr 2022 06:53:08 -0700 (PDT) In-Reply-To: <t2ml98$3a56e$2@portraits.wsisiz.edu.pl> Injection-Info: google-groups.googlegroups.com; posting-host=71.24.96.225; posting-account=aFGPVQoAAADngh59qlchYygiuB87zBBU NNTP-Posting-Host: 71.24.96.225 References: <t2ml98$3a56e$2@portraits.wsisiz.edu.pl> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6a7a1ee7-e05e-43a8-a5ca-cbeec6201cc4n@googlegroups.com> Subject: Re: okienko z cmd.exe w Qt C++ From: Adam M <amorawski@magna-power.com> Injection-Date: Fri, 15 Apr 2022 13:53:09 +0000 Lines: 53 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit