If you created a (default) QApplication or QMainWindow object then Qt requires the ability to do graphical things. On Linux/UNIX this requires X11 and the DISPLAY variable, which is how that graphical environment is found. If you do not want to be dependent on X11 then you need to avoid creating anything that needs it before you decide you want to use the graphical environment. Nothing stops you checking command line arguments before you create the QApplication/QMainWindow or QCoreApplication/non-GUI worker object. The exact arrangement can vary but it really is quite straightforward.
#include <QtGui>
int main(int argc, char **argv)
{
bool graphical = true;
if (argc > 1) {
if (strcmp(argv[1], "-batch") == 0)
graphical = false;
}
if (graphical) {
w.show();
return app.exec();
}
else {
qDebug() << "Doing non-GUI stuff";
return app.exec(); // if required
// return 0; // otherwise
}
}
#include <QtGui>
int main(int argc, char **argv)
{
bool graphical = true;
if (argc > 1) {
if (strcmp(argv[1], "-batch") == 0)
graphical = false;
}
QApplication app(argc, argv, graphical);
if (graphical) {
QMainWindow w;
w.show();
return app.exec();
}
else {
qDebug() << "Doing non-GUI stuff";
return app.exec(); // if required
// return 0; // otherwise
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks