Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]QUiLoader only creates base classes of custom widgets

[SOLVED]QUiLoader only creates base classes of custom widgets

Scheduled Pinned Locked Moved General and Desktop
quiloadercustom widgetc++
4 Posts 3 Posters 1.5k Views
  • 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 Offline
    C Offline
    courtjester199
    wrote on 14 Oct 2015, 19:51 last edited by courtjester199
    #1

    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 15 Oct 2015, 19:43
    0
    • C courtjester199
      14 Oct 2015, 19:51

      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 Offline
      C Offline
      courtjester199
      wrote on 15 Oct 2015, 19:43 last edited by
      #2

      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
      1
      • A Offline
        A Offline
        alex_malyu
        wrote on 15 Oct 2015, 21:59 last edited by
        #3

        Checnk that your CustomButton has Q_OBJECT macro

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Oct 2015, 21:59 last edited by
          #4

          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
          0

          4/4

          15 Oct 2015, 21:59

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved