[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;
    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();
    
    //add to 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.


    My ui file includes the CustomButtons like this

    ...
    <widget class="CustomButton" name="custombutton">
    ...
    <customwidgets>
      <customwidget>
        <class>CustomButton</class>
        <extends>QPushButton</extends>
        <header>custombutton.h</header>
      </customwidget>
    </customwidgets>
    

    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 ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.