Creating classes



  • Hi,
    I created a myDelegate class using File->NewFile->C++ class.
    It created the following 2 files:
    mydelegate.h:

    #ifndef MYDELEGATE_H
    #define MYDELEGATE_H
    
    #include <QObject>
    #include <QWidget>
    
    class myDelegate : public QItemDelegate
    {
    public:
        myDelegate();
    };
    
    #endif // MYDELEGATE_H
    

    and mydelegate.cpp:

    #include "mydelegate.h"
    
    myDelegate::myDelegate()
    {
    
    }
    
    

    When I run build I keep getting the following error message:
    C:\Programming\Projects\Folkfriends_1_0\mydelegate.h:8: error: expected class-name before '{' token
    {
    ^
    What did I miss?
    Thank you.


  • Moderators

    @gabor53
    You forgot

    #include <QItemDelegate>
    

    in mydelegate.h



  • You are also missing Q_OBJECT and the parent in the constructor. On top of that in Qt5 QItemDelegate is deprecated, use QStyledItemDelagate instead

    mydelegate.h:

    #ifndef MYDELEGATE_H
    #define MYDELEGATE_H
    
    #include <QStyledItemDelegate>
    
    class myDelegate : public QStyledItemDelegate
    {
    Q_OBJECT
    public:
        explicit myDelegate(QObject* parent = nullptr);
    virtual ~myDelegate(); /* if there is the remote chance of your delegate becoming base class for another delegate save yourselves hours of debug for memory leaks and just declare a virtual destructor */
    };
    
    #endif // MYDELEGATE_H
    

    mydelegate.cpp:

    #include "mydelegate.h"
    
    myDelegate::myDelegate(QObject* parent)
    :QStyledItemDelegate(parent)
    {
    
    }
    myDelegate::~myDelegate(){
    
    }
    


  • @VRonin
    Thank you. It still gives me the following error message:
    C:\Programming\Projects\Folkfriends_1_0\mydelegate.h:11: error: expected unqualified-id before '}' token
    }
    ^



  • The last error message was fixed by adding a ; after class myDelegate in mydelegate.h:

    class myDelegate;
    

    In return I got new error messages:
    C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:3: error: undefined reference to `vtable for myDelegate'
    collect2.exe:-1: error: error: ld returned 1 exit status


  • Qt Champions 2016

    @gabor53 said in Creating classes:

    table for myDelegate'

    Clean All
    Run qmake from the build menu.
    Build all

    Should cure it



  • @mrjj
    It did. Thank you.


Log in to reply
 

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