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. Can't load custom widget using QUiLoader
QtWS25 Last Chance

Can't load custom widget using QUiLoader

Scheduled Pinned Locked Moved Solved General and Desktop
custom widgetquiloader
10 Posts 3 Posters 7.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.
  • P Offline
    P Offline
    Prisco
    wrote on 8 Jun 2016, 10:28 last edited by Prisco 6 Aug 2016, 13:36
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Jun 2016, 12:43 last edited by
      #2

      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?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Prisco
        wrote on 8 Jun 2016, 13:32 last edited by
        #3

        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'."

        K 1 Reply Last reply 8 Jun 2016, 15:04
        0
        • P Prisco
          8 Jun 2016, 13:32

          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'."

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 8 Jun 2016, 15:04 last edited by
          #4

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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • P Offline
            P Offline
            Prisco
            wrote on 8 Jun 2016, 15:55 last edited by
            #5

            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?

            K 1 Reply Last reply 8 Jun 2016, 16:07
            1
            • P Prisco
              8 Jun 2016, 15:55

              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?

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 8 Jun 2016, 16:07 last edited by
              #6

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

              Read and abide by the Qt Code of Conduct

              P 1 Reply Last reply 9 Jun 2016, 07:34
              1
              • K kshegunov
                8 Jun 2016, 16:07

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

                P Offline
                P Offline
                Prisco
                wrote on 9 Jun 2016, 07:34 last edited by
                #7

                @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?

                K 1 Reply Last reply 9 Jun 2016, 10:49
                1
                • P Prisco
                  9 Jun 2016, 07:34

                  @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?

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 9 Jun 2016, 10:49 last edited by
                  #8

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

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • P Offline
                    P Offline
                    Prisco
                    wrote on 9 Jun 2016, 12:55 last edited by
                    #9

                    Great! Now it works

                    thanks a lot for your help

                    K 1 Reply Last reply 9 Jun 2016, 13:03
                    1
                    • P Prisco
                      9 Jun 2016, 12:55

                      Great! Now it works

                      thanks a lot for your help

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 9 Jun 2016, 13:03 last edited by
                      #10

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

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • R Roberrt referenced this topic on 16 Feb 2023, 13:28

                      10/10

                      9 Jun 2016, 13:03

                      • Login

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