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. How to load a widget from a ui file saved in resource?
Forum Updated to NodeBB v4.3 + New Features

How to load a widget from a ui file saved in resource?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.4k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    Daniella
    wrote on last edited by
    #1

    I have a .ui file saved in the .qrc named treeview.ui, into this .ui i have a QTreeView that have been promoted on Qt Designer, to a subclass called TreeView.

    I'm reading this file in a loop and in each iteration copying this TreeView to a QStackedWidget, the widget is being added to the container correctly, however, I'm getting this message in the console:

    "QFormBuilder was unable to create a custom widget of the class 'TreeView'; defaulting to base class 'QTreeView'."

    What i'm missing?

    #include "treeview.h" // The file that contains the subclass promoted on Qt Designer
    
    QUiLoader loader;
    QFile file(":/ui/treeview.ui");
    //file.open(QIODevice::ReadOnly);
    
    for (i = map.begin(); i != map.end(); ++i)
    {
        // ...
        file.open(QIODevice::ReadOnly);
        QWidget *form = loader.load(&file);
        file.close();
    
        qDebug() << form;
        if (!form) {
            qDebug() << "Error loading UI file:" << loader.errorString();
        }
        ui->treeViewContainer->addWidget(form);
    }
    

    Also, do i really need to open the file and call loader.load(&file) in each iteration of the loop to be able to make a new 'copy' of the widget in the treeViewContainer?

    M 1 Reply Last reply
    0
    • D Daniella

      I have a .ui file saved in the .qrc named treeview.ui, into this .ui i have a QTreeView that have been promoted on Qt Designer, to a subclass called TreeView.

      I'm reading this file in a loop and in each iteration copying this TreeView to a QStackedWidget, the widget is being added to the container correctly, however, I'm getting this message in the console:

      "QFormBuilder was unable to create a custom widget of the class 'TreeView'; defaulting to base class 'QTreeView'."

      What i'm missing?

      #include "treeview.h" // The file that contains the subclass promoted on Qt Designer
      
      QUiLoader loader;
      QFile file(":/ui/treeview.ui");
      //file.open(QIODevice::ReadOnly);
      
      for (i = map.begin(); i != map.end(); ++i)
      {
          // ...
          file.open(QIODevice::ReadOnly);
          QWidget *form = loader.load(&file);
          file.close();
      
          qDebug() << form;
          if (!form) {
              qDebug() << "Error loading UI file:" << loader.errorString();
          }
          ui->treeViewContainer->addWidget(form);
      }
      

      Also, do i really need to open the file and call loader.load(&file) in each iteration of the loop to be able to make a new 'copy' of the widget in the treeViewContainer?

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @Daniella said in How to load a widget from a ui file saved in resource?:

      defaulting to base class 'QTreeView'."

      Be sure to specify the full path of the header file in Promote To dialog.

      do i really need to open the file and call loader.load(&file) in each iteration

      calling seek(0) or reset() should be enough.

      D 1 Reply Last reply
      0
      • M mpergand

        @Daniella said in How to load a widget from a ui file saved in resource?:

        defaulting to base class 'QTreeView'."

        Be sure to specify the full path of the header file in Promote To dialog.

        do i really need to open the file and call loader.load(&file) in each iteration

        calling seek(0) or reset() should be enough.

        D Offline
        D Offline
        Daniella
        wrote on last edited by
        #3

        @mpergand said in How to load a widget from a ui file saved in resource?:

        @Daniella said in How to load a widget from a ui file saved in resource?:

        defaulting to base class 'QTreeView'."

        Be sure to specify the full path of the header file in Promote To dialog.

        I have tried adding the full path to the treeview.h in the qt designer promote dialog, but i still getting this message.

        M 1 Reply Last reply
        0
        • R Offline
          R Offline
          Roberrt
          wrote on last edited by Roberrt
          #4

          @Daniella I have found a similar post: https://forum.qt.io/topic/68045/can-t-load-custom-widget-using-quiloader/10

          "What available widgets do you get, is your class present in that list?"
          When i call qDebug() << loader.availableWidgets(); my subclassed TreeView* is not on the list.

          I saw that the guy solved her problem by sub classing Ui loader and override the createWidget as kshegunov suggested.

          @kshegunov do you mind telling how should be the subclassed Ui loader?

          D 1 Reply Last reply
          0
          • D Daniella

            @mpergand said in How to load a widget from a ui file saved in resource?:

            @Daniella said in How to load a widget from a ui file saved in resource?:

            defaulting to base class 'QTreeView'."

            Be sure to specify the full path of the header file in Promote To dialog.

            I have tried adding the full path to the treeview.h in the qt designer promote dialog, but i still getting this message.

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            @Daniella said in How to load a widget from a ui file saved in resource?:

            I have tried adding the full path to the treeview.h

            Do you use any namespace ?

            To be honest, I don't get the goal to have a ui file as a resource ...

            kshegunovK 1 Reply Last reply
            0
            • R Roberrt

              @Daniella I have found a similar post: https://forum.qt.io/topic/68045/can-t-load-custom-widget-using-quiloader/10

              "What available widgets do you get, is your class present in that list?"
              When i call qDebug() << loader.availableWidgets(); my subclassed TreeView* is not on the list.

              I saw that the guy solved her problem by sub classing Ui loader and override the createWidget as kshegunov suggested.

              @kshegunov do you mind telling how should be the subclassed Ui loader?

              D Offline
              D Offline
              Daniella
              wrote on last edited by Daniella
              #6

              @Roberrt Thank you for the link, i got it working by subclassing UiLoader

              #pragma once
              #include "stdafx.h"
              #include "pushbutton.h"
              #include "treeview.h"
              
              class UiLoader : public QUiLoader
              {
              public:
                  UiLoader(QObject *parent = nullptr) : QUiLoader(parent) {}
              
                  QWidget *createWidget(const QString &className, QWidget *parent = nullptr, const QString &name = QString()) override
                  {
                      //qDebug() << "className: " << className;
                      if (className == "PushButton")
                          return new PushButton(parent);
                      else if (className == "TreeView")
                          return new TreeView(parent);
                      else
                          return QUiLoader::createWidget(className, parent, name);
                  }
              };
              

              I also have no experience with this topic (uiloader etc), I wonder if the loader subclass is correctly.

              @mpergand no, i'm not using namespace.

              1 Reply Last reply
              0
              • M mpergand

                @Daniella said in How to load a widget from a ui file saved in resource?:

                I have tried adding the full path to the treeview.h

                Do you use any namespace ?

                To be honest, I don't get the goal to have a ui file as a resource ...

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @mpergand said in How to load a widget from a ui file saved in resource?:

                To be honest, I don't get the goal to have a ui file as a resource ...

                There are niche uses where you want the UI to be built at runtime (connecting signals/slots at runtime etc.) instead of compiling it in via the code generated by uic. That way you can change the UI without actually rebuilding the application/library, but it is rare.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                2

                • Login

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