Programm je nach QCommanLineOption als console oder gui

Verschiedenes zu Qt
Antworten
huckleberry
Beiträge: 115
Registriert: 2. Oktober 2010 17:07

Programm je nach QCommanLineOption als console oder gui

Beitrag 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
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
huckleberry
Beiträge: 115
Registriert: 2. Oktober 2010 17:07

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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?
MichaelS
Beiträge: 240
Registriert: 27. Dezember 2005 12:49

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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
huckleberry
Beiträge: 115
Registriert: 2. Oktober 2010 17:07

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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
huckleberry
Beiträge: 115
Registriert: 2. Oktober 2010 17:07

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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 ...
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
huckleberry
Beiträge: 115
Registriert: 2. Oktober 2010 17:07

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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();
}
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag von Christian81 »

Ja, zumindest für die Option '-c'. Was anderes fällt mir gerade dazu nicht ein.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
hilefoks
Beiträge: 144
Registriert: 13. März 2008 16:09

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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();
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: Programm je nach QCommanLineOption als console oder gui

Beitrag 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.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
Antworten