什么情况下可以不创建QCoreApplication

时间:2020-12-12 20:57:23
Under what circumstances do I not need to create a
QApplication/QCoreApplication?
https://snapbrowse.com/browse.php/Oi8vbGlzdHMudHJvbGx0ZWNoLmNvbS9xdC1pbnRlcmVzdC8yMDA2LTAyL3RocmVhZDAxNTQwLTAuaHRtbA_3D_3D/b0/fnorefer/
 
That not really true if said like that. There are quite a bunch of
classes (for example QString and all the containers) which can probably
be safely used without a QCoreApplication object. Also basic
signals/slots work without QCoreApplication (see attached files), from
QCoreApplications documentation it looks to me that as long as you don't
need Qt's event loop (either directly or one of the classes you use
needs it) your safe without the QCoreApplication object. 

However TT recommends to use a QCoreApplication, thus as long as you
don't want to utilize another event-loop (from another lib) you should
either already know that you don't need it or better use a
QCoreApplication object.

Andreas

-- 
 [ signature omitted ] 
#include <myobj.h>
#include <iostream>

int main(int argc, char *argv[])
{
	std::cout << "Creating myobj\n";
	myobj* o = new myobj();
	o->method1();
	std::cout << "Deleting myobj\n";
	delete o;
	exit(0);
}

#include <iostream>
#include <iostream>
#include <myobj.h>

myobj::myobj(QObject* parent)
	: QObject(parent)
{
	connect(this, SIGNAL(signal1()), this, SLOT(slot1()));
}

void myobj::method1()
{
	std::cout << "method1 of myobj called\n";
	emit signal1();
}
void myobj::slot1()
{
	std::cout << "slot1 of myobj called\n";
}

#include <QObject>
#include <QObject>

class myobj : public QObject
{
	Q_OBJECT
	public:
		myobj(QObject* parent = 0);
		void method1();
	public slots:
		void slot1();
	signals:
		void signal1();
};