Can't load custom widget using QUiLoader



  • 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?
    Prisc


  • Qt Champions 2016

    Hi and welcome
    You code just worked for me ?
    Qt 5.6 , mingw, win 10
    https://www.dropbox.com/s/6l6q5jolu5ozvqo/promoted.zip?dl=0

    Could u try this sample?



  • Thank you for the reply, I tried your sample and it's working. Now I see the problem is not in the widget.
    In my project I'm using QUiLoader to dynamically load ui files containing the custom widgets.

    I added this feature to your sample:
    https://drive.google.com/open?id=0B1puCs85aQ9NYzJ6RkFxOU5oWXc

    On Ubuntu / gcc / debug or release doesn't work
    On Windows / mingw / debug doesn't work
    On Windows / mingw / release it works!

    Why does it fail using Ubuntu or windows/debug ?

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


  • Qt Champions 2016

    @Prisco
    Hello,
    What available widgets do you get, is your class present in that list?



  • Hi,
    no it's not present in that list...

    I managed to add it by creating a plugin containing that widget, and adding its build folder to the loader with addPluginPath().
    Do I really need to create a plugin or there is a simpler way?


  • Qt Champions 2016

    @Prisco said:

    Do I really need to create a plugin or there is a simpler way?

    You can subclass the Ui loader and override the createWidget or other needed method(s). That'd be simpler.



  • @kshegunov

    I tryed to subclass QUiLoader and 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?


  • Qt Champions 2016

    @Prisco
    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.

    Kind regards.



  • Great! Now it works

    thanks a lot for your help


  • Qt Champions 2016

    @Prisco
    I'm glad it worked out.
    Good luck with your project!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.