Results 1 to 5 of 5

Thread: Can't load custm widget

  1. #1

    Default Can't load custm widget

    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:

    Qt Code:
    1. #ifndef QAXELPANEL_H
    2. #define QAXELPANEL_H
    3. #include <QWidget>
    4.  
    5. class AxelPanel : public QWidget
    6. {
    7. Q_OBJECT
    8. Q_PROPERTY(QColor colorBg READ colorBg WRITE setColorBg)
    9.  
    10. public:
    11.  
    12. AxelPanel(QWidget *parent = 0);
    13. void paintEvent(QPaintEvent *e);
    14. QColor colorBg(){ return _colorBg;}
    15. void setColorBg(QColor value){ _colorBg = value; update();}
    16.  
    17. private:
    18. QColor _colorBg;
    19. int _paintWidth;
    20. int _paintHeight;
    21.  
    22. };
    23.  
    24. #endif // QAXELPANEL_H
    To copy to clipboard, switch view to plain text mode 



    This is the widget cpp file:


    Qt Code:
    1. #include "axelpanel.h"
    2. #include <QPainter>
    3.  
    4. AxelPanel::AxelPanel(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. _colorBg = QColor(50,50,50);
    8. }
    9.  
    10. void AxelPanel::paintEvent(QPaintEvent* event)
    11. {
    12. Q_UNUSED(event);
    13. QPainter painter(this);
    14. painter.setRenderHint(QPainter::Antialiasing, true);
    15.  
    16. painter.setBrush(_colorBg);
    17. painter.drawRect(0,0,width(),height());
    18. }
    To copy to clipboard, switch view to plain text mode 


    Any idea?
    Prisco

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't load custm widget

    QFormBuilder? Are you trying to run-time load the .ui file instead of generating code via UIC?

    Cheers,
    _

  3. #3

    Default Re: Can't load custm widget

    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?

  4. #4

    Default Re: Can't load custm widget

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't load custm widget

    Quote Originally Posted by Prisco View Post
    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,
    _

Similar Threads

  1. Replies: 6
    Last Post: 4th March 2013, 20:20
  2. Replies: 4
    Last Post: 19th June 2012, 13:43
  3. Replies: 1
    Last Post: 30th March 2011, 15:29
  4. widget load event?
    By scarleton in forum Newbie
    Replies: 2
    Last Post: 16th August 2010, 18:47
  5. Replies: 4
    Last Post: 1st March 2006, 23:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.