[SOLVED]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-loading works in principle. All widgets are loaded and placed into my main layout, but all CustomButtons are just QPushButtons. It seems, that QUiLoader creates all my custom widgets as instances of their base classes.

    This is what I do:

    QUILoader loader;
    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);
    //"mainFrame" is a QFrame in my main ui
    QWidget *customWidget = loader.load(&file, mainframe);
    //add to layout
    //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.

    My ui file includes the CustomButtons like this

    <widget class="CustomButton" name="custombutton">

    What am I doing wrong?

  • 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.

  • Checnk that your CustomButton has Q_OBJECT macro

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you calling findChildren ?