Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [SOLVED]QUiLoader only creates base classes of custom widgets

    General and Desktop
    quiloader custom widget c++
    3
    4
    1122
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      courtjester199 last edited by courtjester199

      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?

      C 1 Reply Last reply Reply Quote 0
      • C
        courtjester199 @courtjester199 last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • A
          alex_malyu last edited by

          Checnk that your CustomButton has Q_OBJECT macro

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi and welcome to devnet,

            How are you calling findChildren ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • First post
              Last post