Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

qt - QEventLoop: Cannot be used without QApplication

I'm trying to validate an xml file against a specific schema.
So I'm loading the schema into the QXmlSchema object. But I get some strange errors.
My code looks like:

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 piece of code, Qt errors out saying:

QEventLoop: Cannot be used without QApplication
QDBusConnection: system D_Bus connection created before QCoreApplication.
Application may misbehave.
QEventLoop: Cannot be used without QApplication

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

Could someone please help me to solve this problem.
Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

QXmlSchema creates, among other things, a message handler which inherits from QObject. Since this message handler will be using Qt's event system, an event loop (the structure which handles queueing and routing of events) is required. As the error messages tell you, the main event loop is created along with your QApplication.

If you're creating a GUI application generally you should have a small amount of code in your main() function, something like:

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

  return a.exec();
}

Start your code off in, say, the constructor of MainWindow:

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";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...