Search code examples
c++qtwidgetcustom-controlsqtgui

QUiLoader only creates base classes of custom widgets


I'm trying to load a ui file containing custom widgets using QUiLoader.

My custom widgets (called CustomButton) inherit from QPushButton. The ui file is loaded and placed into my main layout, but all CustomButtons are QPushButtons only. It seems, that QUiLoader creates all my custom widgets as instances of their base classes.

This is what I do:

QUILoader loader;
loader.addPluginPath(MY_PLUGIN_PATH);

QStringList availableWidgets = loader.availableWidgets();

//fail if "CustomButton" is not available
if (!availableWidgets.contains("CustomButton")) {
  return false;
}
//here I see that availableWidgets contain my "CustomButton"!

QString qFileName(MY_UI_FILE_PATH); 
QFile file(qFileName);
file.open(QFile::ReadOnly);

//"mainFrame" is a QFrame in my main ui
QWidget *customWidget = loader.load(&file, mainframe);
file.close();

//layout
mainframe->layout()->addWidget(customWidget);

//Note: There are no QPushButtons in my ui file! There are only CustomButtons!

//Now I try to find my custom buttons
QList<QPushButton*> list1 = customWidget->findChildren<QPushButton *>();     //all my CustomButtons are listed here
QList<CustomButton*> list2 = customWidget->findChildren<CustomButton *>();   //this list is empty

// I also have a breakpoint in my CustomButton's constructor which is never hit.

What am I doing wrong?


Solution

  • I found out, that QUiLoader actually creates my CustomButtons!

    Only the findChildren() method seems to be wrong, since it doesn't find my CustomButtons but QPushButtons.