How to load a widget from a ui file saved in resource?
-
I have a
.ui
file saved in the.qrc
namedtreeview.ui
, into this.ui
i have aQTreeView
that have been promoted onQt Designer
, to a subclass calledTreeView
.I'm reading this file in a loop and in each iteration copying this
TreeView
to aQStackedWidget
, 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 thetreeViewContainer
? -
@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.
-
@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.
-
@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 callqDebug() << loader.availableWidgets();
my subclassedTreeView*
is not on the list.I saw that the guy solved her problem by sub classing
Ui loader
and override thecreateWidget
as kshegunov suggested.@kshegunov do you mind telling how should be the subclassed
Ui loader
? -
@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.
-
@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.