Frage

I'm trying to load a simple .ui file using QUiLoader and I'm getting the following error:

Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document. 

I checked that the .ui file exists and printed its contents.
Code:

QApplication a(argc, argv);
MainWindow w;
w.show();

QUiLoader loader;
qDebug()<< QDir::currentPath();
QFile file("customwidget.ui");
qDebug() <<"File open: "<< file.open(QIODevice::ReadOnly| QIODevice::Text );
QWidget *formWidget;
qDebug() << file.readAll();
qDebug() <<"Loader: "<<(formWidget=loader.load(&file,&w));
file.close();
formWidget->show();

return a.exec();

Output:

"/home" 
File open:  true 
"<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>customWidget</class>
 <widget class="QWidget" name="customWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>200</width>
    <height>200</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>60</y>
     <width>87</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text">
    <string>PushButton</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
" 
Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
Loader:  QObject(0x0)

The customwidget.ui file was generated using the QTDesigner and is placed at /home.
Why is this not working?

War es hilfreich?

Lösung

you already read the entire file, do a file.reset() before the load or just don't read it first:

QUiLoader loader;
qDebug()<< QDir::currentPath();
QFile file("customwidget.ui");
qDebug() <<"File open: "<< file.open(QIODevice::ReadOnly| QIODevice::Text );
QWidget *formWidget;
qDebug() << file.readAll();
file.reset();//or file.seek(0);
qDebug() <<"Loader: "<<(formWidget=loader.load(&file,&w));
file.close();
formWidget->show();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top