Seite 1 von 1

Programm je nach QCommanLineOption als console oder gui

Verfasst: 25. Februar 2015 11:34
von huckleberry
Hallo,

ich habe ein MainWidget (MeinProg.exe) Applikation. wenn es mit der Option
MeinProg.exe -console
gestart wird, soll nicht die MainWidget gestartet werden, sondern nur eine Console Version.
Wenn nur
MeinProg.exe
ohne console, dann ganz normal .

Code: Alles auswählen

int main(int argc, char* argv[])
{
  QCoreApplication app(argc, argv);
  app.setApplicationName("Database");
  app.setApplicationVersion("0.1");

  QCommandLineParser parser;
  parser.setApplicationDescription("Database");
  parser.addHelpOption();
  parser.addVersionOption();
  QCommandLineOption consoleOption(QStringList() << "c" << "console", QCoreApplication::translate("main", "Start program in console, NOT (!) in gui!"));
  parser.addOption(consoleOption);

  parser.process(app);

  if (!parser.isSet(consoleOption))
    {
      QApplication a(argc, argv);
      a.setStyle("plastique");
      WidgetWindow w;
      w.show();
      return a.exec();
    }
}
Ich bekomme nach dem Crash:
ASSERT failure in QCoreApplication: "there should be only one application object", file C:\qt\5.3.2\qtbase\src\corelib\kernel\qcoreapplication.cpp, line 721
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Wie bekomme ich so eine Option unter?

VG huck

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 25. Februar 2015 18:15
von Christian81
Du hast schon QCoreApplication instanziiert und instanziierst danach noch eine QApplication - das geht nicht. Also entweder das eine oder das andere - je nachdem was Du als Option übergibst.

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 26. Februar 2015 14:41
von huckleberry
Das hieße ja ich müsste meine QCommandLineoption/Parser schon vorher starten?
Mein QCommandLineParser verlangt doch aber eine

Code: Alles auswählen

parser.process(app);
(app == QCoreAPP) von beiden als Parameter?

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 26. Februar 2015 15:49
von MichaelS
huckleberry hat geschrieben:Das hieße ja ich müsste meine QCommandLineoption/Parser schon vorher starten?
Mein QCommandLineParser verlangt doch aber eine

Code: Alles auswählen

parser.process(app);
(app == QCoreAPP) von beiden als Parameter?
Es gibt noch eine zweite Variante:

Code: Alles auswählen

void QCommandLineParser::​process(const QStringList & arguments)
Die Argumente-Liste liefert Dir die statische Methode QCoreApplication::arguments():

Code: Alles auswählen

parser.process( QCoreApplication::arguments()  )
Gruß Michael

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 9. März 2015 18:49
von huckleberry
MichaelS hat geschrieben:

Code: Alles auswählen

parser.process( QCoreApplication::arguments()  )
Hmm
QCoreApplication::arguments: Please instantiate the QApplication object first
QCommandLineParser: argument list cannot be empty, it should contain at least the executable name

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 10. März 2015 07:51
von huckleberry
Ok, und folgendes

Code: Alles auswählen

#include <QApplication>
#include <QCommandLineParser>


#include "mainwindow.h"

int main(int argc, char* argv[])
{
  QCoreApplication app(argc, argv);
  app.setApplicationName("MyProg");
  app.setApplicationVersion("0.1");

  QCommandLineParser parser;
  parser.setApplicationDescription("MyProg");
  parser.addHelpOption();
  parser.addVersionOption();
  QCommandLineOption consoleOption(QStringList() << "c" << "console", QCoreApplication::translate("main", "Start program in console, NOT (!) in gui!"));
  parser.addOption(consoleOption);

  parser.process( QCoreApplication::arguments() );

  if (!parser.isSet(consoleOption))
  {
    QApplication a(argc, argv);
    a.setStyle("plastique");
    WidgetWindow w;
    w.show();
    return a.exec();
  }
  else
  {
    qDebug() << "Console";
  }
}
shreibt bei -c korrekterweise den String auf die Ausgabe. Ohne und folgende Meldung:
ASSERT failure in QCoreApplication: "there should be only one application object", file C:\qt\qt-everywhere-opensource-src-5.3.2\qtbase\src\corelib\kernel\qcoreapplication.cpp, line 721
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
C:\MyProjects\MyProg.exe beendet, Rückgabewert 3

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 10. März 2015 10:31
von Christian81
Auch wenn hier Qt und C++ verwendet wird sollte es trotzdem nicht schwer sein über alle argv - Argumente zu iterieren und per Stringvergleich zu schauen ob Console oder Gui ...

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 10. März 2015 11:09
von huckleberry
Christian81 hat geschrieben:Auch wenn hier Qt und C++ verwendet wird sollte es trotzdem nicht schwer sein über alle argv - Argumente zu iterieren und per Stringvergleich zu schauen ob Console oder Gui ...
Also würde ich dann ohne QCommandLineParser/options auskommen..

A la Pseudocode:

Code: Alles auswählen

if ( "-c" is in argumentList )
{
  QCoreApplication app(argc, argv);
  app.setApplicationName("MyProg");
  app.setApplicationVersion("0.1");
  //etc..
}
else
{
    QApplication a(argc, argv);
    a.setStyle("plastique");
    WidgetWindow w;
    w.show();
    return a.exec();
}

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 10. März 2015 12:56
von Christian81
Ja, zumindest für die Option '-c'. Was anderes fällt mir gerade dazu nicht ein.

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 29. März 2015 10:27
von hilefoks
Du darfst QApplication auch für Konsolenanwendungen benutzen. ;-)

Will sagen: QCoreApplication will man nutzen, weil man dann nicht gegen GUI-Komponenten linken muss (QtGui, QtWidget). In deinem Fall musst du das aber eh.

Code: Alles auswählen

QApplication app(argc, argv);
//...

if( !parser.isSet( consoleOption ) ) {
    auto *w = new WidgetWindow;
    w->show();
}
else {
    // ....
}

return a.exec();

Re: Programm je nach QCommanLineOption als console oder gui

Verfasst: 29. März 2015 10:41
von Christian81
Unter Linux ist ein großer Unterschied zwischen QApplication und QCoreApplication (keine Ahnung wie es unter Windows aussieht). QGuiApplication benötigt zwingend eine X11-Verbindung zum Starten. Ist als nicht ganz egal.