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

I am trying to get a list of widgets from .ui files. So here is some 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 see it, the available widgets () gives me all the widgets, not the ones in the .ui file. How can i achieve this? Thanks, go ahead.

+3
source share
1 answer

Subclass QUiLoaderand redefine createWidget , createLayout and createAction (there is also createActionGroup , but it is no longer supported unless you manually edit the file ui).

, , ui. , , , , .

UPDATE

, QUiLoader ( ):

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;
   }
};
+2

All Articles