Search code examples
c++qtintellisenseqtguiqlabel

Issue Subclassing a Qlabel C++


I am attempting to subclass a QLabel using a header file and I get the error on constructor

IntelliSense: indirect nonvirtual base class is not allowed

class foo : public QLabel
{
    Q_OBJECT

    foo(QWidget* parent = 0) :  QWidget(parent)
    {

    }

    void mouseMoveEvent( QMouseEvent * event )
    {
        //Capture this
    }

};

Any suggestions why this is happening and how I can fix it ?


Solution

  • The problem is here:

    foo(QWidget* parent = 0) :  QWidget(parent)
    

    You are inheriting from QLabel, but you specify QWidget for the base. You should write this intead:

    explicit foo(QWidget* parent = Q_NULLPTR) :  QLabel(parent)
    //                                           ^^^^^^
    

    Also, please use explicit for that constructor as well as Q_NULL_PTR or at least NULL instead of 0.