QEventloop canot be used without QApplication - c++

I'm currently developing a Console Unit Test Client with Google Test.
The Tests involves a step where a binary file is downloaded from a server.
To download this file I thought about using Qt.
Now I took the example code from
https://wiki.qt.io/Download_Data_from_URL
This gives me the error QEventloop canot be used without QApplication
I found this thread QEventLoop: Cannot be used without QApplication
However this works for a UI application.
My googletest main is predefined
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from %s\n", __FILE__);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Where would be a good place to add the QApplication?
Could you help me out?
thx for your help :)

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.

After initializing class member app crashes with SIGSEGV [duplicate]

This question already has an answer here:
C++ - segmentation fault when inheritance
(1 answer)
Closed 6 years ago.
I have Qt application with Application class:
class Application : public QApplication {
public:
Application(int argc, char** argv);
~Application();
};
everything works fine until I add
private:
const QString NAME = "QtImageViewer";
after that app crashes 9 of 10 times when I try to run it
main looks like
int main(int argc, char** argv)
{
Application app(argc, argv);
app.setApplicationName("QtImageViewer");
MainWindow mainWindow;
mainWindow.show();
return (app.exec());
}
I'd like to know what's wrong because I have no idea.
Edit:
GDB backtrace
Valgrind
Fixed it, same problem as here.
My Application class constructor signature is Application(int argc, char** argv); but QApplication has QApplication(int &argc, char **argv)
Changed Application(int argc, char** argv); to Application(int& argc, char** argv); and everything works.
Thanks to all for help.
I'd comment, but I don't have enough reputation at the time of this writing. For the record, I cloned your repository you linked to. I notice you don't seem to be using Qt Creator but are probably just using cmake. My Qt 5 installation isn't where yours is, so I just threw everything into Qt Creator and built and ran the project from there.
The program runs and closes just fine, even with the change you suggest breaks it for you. (Built and run on Ubuntu 16 with Qt 5.5, g++ 5.4.0.)
Additionally, I may be misunderstanding Igor's comment, but since Application is made on the stack, no additional heap deallocation is introduced to ~Application().

Disable OpenCV-core's output "Init done"

I just updated my opencv to the latest stable version (3.1.0). I tried to run a programm on which i'm working for a long time now, and everything works fine.
But I've got a new output 'Init done - Opengl support available'coming from Qt or OpenCV I don't really know, when I'm displaying an image. I would like to disable that output, because I've already a lot of outputs of mine.
If you have a quick solution (I mean easy to process), it would be great
Thanks
Looking at the relevant code, this output is only triggered if there is no QApplication instance already present. You can stop the output by creating a QApplication instance before doing any OpenCV related operations:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Perform highgui operations.
}

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();
}