amol01
amol01

Reputation: 1863

How to read widgets from the .ui file in qt5?

I am trying to get the list of widgets from a .ui files. So here is a bit of code:

    QUiLoader loader;
     QFile file(fname);
     file.open(QFile::ReadOnly);
     QWidget *myWidget = loader.load(&file, this);
     file.close();

     QStringList avlb_wd = loader.availableWidgets();
     QMessageBox msg;

     foreach (const QString &str, avlb_wd)
     {
        msg.setText(str);
        msg.exec();
     }

But as I can see, availableWidgets() gives me all the widgets, not the ones that are in .ui file. How can I achieve it? Thanks forward.

Upvotes: 3

Views: 1990

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

Make a subclass of QUiLoader, and reimplement createWidget, createLayout and createAction (there's also createActionGroup, but it's not really supported any more, unless you manually edit the ui file).

These functions are called every time a new widget, layout or action is created by the ui loader. So just call the base-class implementation to get the created object, and then you can gather whatever information you like, before returning it.

UPDATE:

So the basic QUiLoader subclass would look like this (add other overloads as required):

class UiLoader : public QUiLoader
{
   Q_OBJECT
public:
   UiLoader(QObject *parent = 0) : QUiLoader(parent) { }

   virtual QWidget* createWidget(const QString &className,
   QWidget *parent = 0, const QString &name = QString())
   {
      QWidget* widget = QUiLoader::createWidget(className, parent, name);
      // do stuff with className, name, widget, etc
      return widget;
   }
};

Upvotes: 2

Related Questions