QEventLoop: cannot be used without QApplication

I am trying to check an XML file for a specific schema.
Therefore, I load the schema into the QXmlSchema object. But I get some weird errors.
My code looks like this:

int main() { QUrl url("http://www.schema-example.org/myschema.xsd"); QXmlSchema schema; if (schema.load(url) == true) qDebug() << "schema is valid"; else qDebug() << "schema is invalid"; return 1; } 

When I try to run the above code snippet, Qt errors cause:

QEventLoop: cannot be used without QApplication
QDBusConnection: A D_Bus system connection created prior to QCoreApplication.
The application may be mistaken. QEventLoop: cannot be used without QApplication

My version of Qt Designer: qt4-designer 4: 4.8.1-0ubuntu4.1
My version of Qt Creator: qtcreator 2.4.1-0ubuntu2

Can anyone help me solve this problem.
thanks

+4
source share
1 answer

QXmlSchema creates, among other things, a message handler that inherits from QObject . Since this message handler will use the Qt event system, an event loop is required (a structure that processes the queue and event routing). As error messages report, the main event loop is created along with your QApplication .

If you are creating a GUI application, usually you should have a small amount of code in your main() function, for example:

 int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } 

Run your code, for example, in the MainWindow constructor:

 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QUrl url("http://www.schema-example.org/myschema.xsd"); QXmlSchema schema; if (schema.load(url) == true) qDebug() << "schema is valid"; else qDebug() << "schema is invalid"; } 
+6
source

Source: https://habr.com/ru/post/1413675/


All Articles