PDA

View Full Version : Can't load custm widget



Prisco
8th June 2016, 11:44
Hi everyone,
I'm using Qt 5.6 and I'm having some issues with custom widgets. I've added a QWidget to a to a .ui file, promoted it to my custom widget (a simple colored panel) but when I run the application i can't see my widget and get this output message:

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

This is the widget header file:



#ifndef QAXELPANEL_H
#define QAXELPANEL_H
#include <QWidget>

class AxelPanel : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor colorBg READ colorBg WRITE setColorBg)

public:

AxelPanel(QWidget *parent = 0);
void paintEvent(QPaintEvent *e);
QColor colorBg(){ return _colorBg;}
void setColorBg(QColor value){ _colorBg = value; update();}

private:
QColor _colorBg;
int _paintWidth;
int _paintHeight;

};

#endif // QAXELPANEL_H




This is the widget cpp file:




#include "axelpanel.h"
#include <QPainter>

AxelPanel::AxelPanel(QWidget *parent)
: QWidget(parent)
{
_colorBg = QColor(50,50,50);
}

void AxelPanel::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);

painter.setBrush(_colorBg);
painter.drawRect(0,0,width(),height());
}



Any idea?
Prisco

anda_skoa
8th June 2016, 19:37
QFormBuilder? Are you trying to run-time load the .ui file instead of generating code via UIC?

Cheers,
_

Prisco
9th June 2016, 08:39
Hi
Yes, I also verified the problem is not in the widget.
In my project I'm using QUiLoader to dynamically load ui files containing the custom widgets.
My widget is not in the available widgets list.

In other forums they suggest to override QUiLoader::createWidget(const QString &className, QWidget *parent, const QString &name)
But then how can I create an instance of my widget and set its properties contained in the .ui file?

Prisco
9th June 2016, 14:17
I finally managed to do it

This is the hint I was given in another forum:

If I'm not mistaken, you only need to create the widget in that method. It's a factory (don't forget to call the parent class' implementation first, as noted in the documentation) that will create widgets based on class name. So I believe you only have to create the appropriate widget (with the provided parent) that's corresponding to the class name. (the last parameter is the object name, which you should also set, to be fully compliant).

The properties should be set by the Ui loader if I'm not in error.

anda_skoa
9th June 2016, 16:47
Hi
In other forums they suggest to override QUiLoader::createWidget(const QString &className, QWidget *parent, const QString &name)

If you are loading the ui dynamically, then you need to let the UI loader have a way of instantiating your widget, it can't magically create widgets it doesn't know anything about.

So you either do that by reimplementing createWidget() or, I think, by providing a widget plugin.

Cheers,
_