What is "QApplication app(argc, argv)" trying to do? - c++

#include <QtGui/QApplication>
#include <QtDeclarative>
#include "qmlapplicationviewer.h"
int main(int argc, char **argv) {
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setMainQmlFile("app/native/assets/main.qml");
viewer.showFullScreen();
return app.exec();
}
My C++ is a bit rusty. Can someone please explain to me what is "QApplication app(argc, argv)" trying to do ?
Is it trying to declare a function which takes in 2 arguments (argc and argv) and return a variable of type QApplication ?

The line
QApplication app(argc, argv);
creates a new instance of type QApplication and invokes the constructor of this class. In your example, the variable app now stores this instance. It is somewhat (semantically) a shorthand of this:
QApplication app = QApplication(argc, argv);

Here's a quote from Qt Docs:
The QApplication class manages the GUI application's control flow and
main settings.
QApplication contains the main event loop, where all events from the
window system and other sources are processed and dispatched. It also
handles the application's initialization, finalization, and provides
session management. In addition, QApplication handles most of the
system-wide and application-wide settings.
For any GUI application using Qt, there is precisely one QApplication
object, no matter whether the application has 0, 1, 2 or more windows
at any given time. For non-GUI Qt applications, use QCoreApplication
instead, as it does not depend on the QtGui library.
The QApplication object is accessible through the instance() function
that returns a pointer equivalent to the global qApp pointer.
So, the line
QApplication app(argc, argv);
creates an instance of the QApplication class.

QApplication is a Qt class that contains the main event loop.
When you write QApplication app(argc, argv);
you are creating a object app of this class, by calling its constructor with argc and argv
When int main(int argc, char **argv) is called while running the program, int argc is intialized to contain the number of arguments passed while running the program. char **argv contains an array of arguments passed to the program when executing it.
char* argv[0] will contain (point to) the name of the program, while the subsequent elements will point to the other arguments passed.
argc and argv are in turn, passed to the constructor of QApplication, so that one can pass Qt specific arguments when running your program.
For an example of such arguments try running ./yourProgramName --help in a terminal window

app() is not a function, it is a constructor call.
If you come from C# or Java or something, imagine it as
QApplication app = new QApplication( argc, argv );
Just that app would be a pointer this way, while it actually is the object itself if it is created like in your example.
In short, Qt needs a QApplication instance to run so signals&slots are processed (if you are using them) and events like painting etc are handled

Related

Use Qts QXmlSchemaValidator without QApplication

I need some help in Qt since I don't really undestand the way Qt libraries can be used in visual studio projects. I try to use the QXmlSchemaValidator class from QtXmlPatterns to validate an xml file against schema, but I can't instantiate a QApplication object since I don't have access to the main.cpp file. I don't want to create a Qt project, just try to use this schemaValidator class in one of a class' method.
This is how I try to load the schema:
QUrl url("http://.../schema.xsd");
QXmlSchema schema;
if (schema.load(url))
qDebug() << "schema is valid";
else
qDebug() << "schema is invalid";
I get this warning: "Please instantiate the QApplication object first".
I found a solution here: QEventLoop: Cannot be used without QApplication that says I need the main function to look like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Is there a way to load the schema and validate my xml files without a QApplication object?
Thanks in advance!
Yes, use QCoreApplication instead.
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//your code here
return a.exec();
}
Seriously, if some Qt features you like require an event loop, you just can't get away with it without one. About not having a "Qt project" (and maybe you mean you're not using qmake) but yet using Qt classes: good luck.

In Qt main function, how does QApplication learn about Mainwindow?

Looking at a simplest Qt Widget sample application that you can find from almost every Qt tutorial:
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
There is one thing puzzles me. There are two major variables a and w here. a.exec() starts Qt's main loop, which suppose to interact with the main GUI component w. However, both of them live on stack and I don't see any code pass w somehow to a. So how does a be aware of the existence of w?
Does the constructor of w initializes a static data structure that a can access to check the top-level widgets?
Qt preprocess your code and build the real c++ code before compiling, its at this moment QApplication wrap all Q object in the main.cpp file and build the rest of the code from it.

QThread: "Destroyed while thread is still running" in a single threaded program

int main(int argc, char** argv)
{
QApplicaiton app(argc, argv);
// parsing other arguments of argc,argv
return app.exec();
}
My problem is the following:
Function may be returned during parsing other arguments (without reaching app.exec()) and when QApplication object is deleted I am getting following error message QThread: Destroyed while thread is still running. As a possible solution I am trying to create QApplication after argument parsing is done.
I have tried app.thread()->quit(); before return statement but it doesn't help.
When QApplication object is created it removes specific arguments (-style, etc.) from argc, argv.
Is it possible to get them from argc, argv manually without creating QApplication object?
It is weird that in a single threaded program I am getting QThread: Destroyed while thread is still running error.
The best "solution" I have found so far is to create QApplication dynamically and not to delete it. The memory leak is not an issue because it is leaked just before program exits.

QTcpSocket not emit any error or connected signals

I have a small project which send some data over network using QTcpSocket. The server works fine but the client(code here) seems does nothing. If I set breakpoint at tcpSocket.connectToHost("127.0.0.1",port); it does jump in, but not any slots I defined.
I can't figure out what's wrong. I think the environment is ok because I can build 2 working examples from Qt GUI Programming
Any ideas are appreciated.
You do not have a QApplication instance and thus no event loop which does all the event / signal&slot handling.
So you at least need a QCoreApplication instance like this in main.cpp:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.connectToServer();
return a.exec();
}

Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display

I'm using Qt5.1 and I'm trying to create a QApplication without a display. I need to draw text with QPainter, so I need to use QApplication (or QGuiApplication), otherwise I get a segfault.
The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:
"QXcbConnection: Could not connect to display".
In Qt 4.8, I was able to use the following constructor with GUIenabled = false to create a QApplication that did not require a display:
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.
I scanned the source code briefly, and there does seem to be a flag in the QApplication constructor, but it is undocumented as to what options can be used in that flag. Using "false" does not work.
How can I create a QApplication without a display? Is there an alternative method to telling QApplication GUIenabled = false? Alternatively, can I create a QCoreApplication that will not segfault when drawing text with QPainter on a QImage?
Yes, that's a Qt 3 (?) thing that is gone in Qt 5. Try running your application with the -platform offscreen command line option instead.
Note that you don't need QApplication or linking to QtWidgets to just draw upon a QImage, using QGuiApplication (and linking to QtGui) is sufficient.
If you want to create an app without GUI, you need to use QCoreApplication instead of QApplication.
Just hit this same issue. Really annoying that it at least isn't a compile error. My solution was just to use pointers and heap objects like,
QCoreApplication* app = 0;
Display* display = XOpenDisplay(NULL);
if (display)
{
XCloseDisplay(display);
app = new QApplication(argc, argv);
qobject_cast<QApplication*>(app)->setQuitOnLastWindowClosed(false);
}
else
{
app = new QCoreApplication(argc, argv);
}
return app->exec();