How to Run a whole QApplication Asynchronously? - c++

I'm trying to run a modified qt-programm as a library. I need it not not block the main execution.
So what I want to do is to run the QApplication and commence the execution of the main application. How do I achive this?
My first thought was to run it in a seperate thread.
void MyClass::execute() {
someClass = someClass::instance();
std::thread t1(&MyClass::startApp, this);
someClass->someFunction();
someClass->doMoreStuff();
}
void MyClass::startApp() {
QApplication app(argc, argv);
app.exec();
QCoreApplication::quit();
}
but this results in an Call to '__invoke' is ambiguous error. Though I don't know why/where __invoke is overwritten and how to handle this error. :(
So how can I accomplish that the QApplication doesn't block the main execution?

Design pattern is totally wrong, QApplication as well as QCoreApplication are not supposed to be multiplied inside one application. What you should do for example is make your own class like:
class LibraryCore: public QObject {
}
and substitute QApplication to this class inside your future library sources. Then you should implement needed methods in LibraryCore (ones which are used inside application) making them work properly..

Related

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

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

#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

Can't update Text Edit Text QT

I'm having an issue. My textEditBox doesn't seem to be updating when my Addtext function is called.
Here's my Addtext:
void CTextBox::AddText(QString string, QString spriteString)
{
textBrowser->setText(string + spriteString);
update();
}
Another class then calls the function and it should add text to the textbox but it doesn't.
How do you call CTextBox::AddText()? update() only schedules a paintEvent() for later, when the program returns to the event loop. That means that
you actually need to have an event loop, ie. at some point you need to call qApp->exec();
you need to allow the programm some time to qApp->processEvents() (insert that after update()), if you want any paining done within a blocking while() {...} or something like that.
Edit: Come to think of it, you shouldn't even need to call update() nor processEvents() if your program returns to the event loop some time after AddText, so there really seems to be an issue with the event loop. Post your main.cpp, will you?
Here is a trivial example of what it sounds like you are trying to do. Maybe you can see where your design differs?
Notice: no explicit update() is needed. I think that's a red herring. I think it far more likely that (1) you are somehow calling your AddText method with empty strings, or (2) your real text edit is a different variable, and have somehow created two of them and are updating one that just exists in memory and was never added to a layout. Perhaps the code of your FileLoaderQT would help? (You can edit your question rather than posting in comments.)
#include <QtGui>
int main(int argc, char **argv) {
QApplication app(argc, argv);
QMainWindow w;
QTextEdit *edit = new QTextEdit;
w.setCentralWidget(edit);
edit->setText("Hello world!");
w.show();
edit->append("Hello world again!");
return app.exec();
}