1

I have:

from PySide import QtGui, QtUiTools

What is the difference between:

ui_file = QtCore.QFile('my_ui.ui')
ui_file.open(QtCore.QFile.ReadOnly)
my_ui = QtUiTools.QUiLoader().load(ui_file)
ui_file.close()

and:

self.ui = QtUiTools.QUiLoader().load('my_ui.ui')

?

I'm asking because the latter seems a lot simpler, and it seems to work just as well as the first chunk of code. I googled up the first chunk of code and by mistake didn't implement it right, but the .ui file loaded just fine anyways. This made me a little confused.

fredrik
  • 9,631
  • 16
  • 72
  • 132

1 Answers1

2

Looking into the Documentation of QUILoader::load, it takes as the first argument a QIODevice which is basically an interface class that can handle any block of Data such as QFile, QBuffer.

In the quiloader.cpp you can see that it tries to open the device and read it's content. Basically your first solution wouldn't need to open the file.

QWidget *QUiLoader::load(QIODevice *device, QWidget *parentWidget)
{
Q_D(QUiLoader);
if (!device->isOpen())
    device->open(QIODevice::ReadOnly|QIODevice::Text);
return d->builder.load(device, parentWidget);
}

sourcecode

user1767754
  • 23,311
  • 18
  • 141
  • 164