3

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
amol01
  • 1,823
  • 4
  • 21
  • 35

1 Answers1

2

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;
   }
};
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Hi, I am quite new to Qt environment. Can you please show me how to make subclass and reimplement above functions? Or link me to docs where I can find info of how to do it? Some examples would really helpful. Thanks anyway. – amol01 Feb 06 '14 at 20:12
  • @user3185491. I've added some code to get you started, but really, it's just very basic C++ stuff. – ekhumoro Feb 06 '14 at 20:56