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. Creating classes

Creating classes

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • G gabor53

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gabor53
      You forgot

      #include <QItemDelegate>
      

      in mydelegate.h

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        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(){
        
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        G 1 Reply Last reply
        3
        • VRoninV VRonin

          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(){
          
          }
          
          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          @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
          }
          ^

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            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

            mrjjM 1 Reply Last reply
            0
            • G gabor53

              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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @gabor53 said in Creating classes:

              table for myDelegate'

              Clean All
              Run qmake from the build menu.
              Build all

              Should cure it

              G 1 Reply Last reply
              2
              • mrjjM mrjj

                @gabor53 said in Creating classes:

                table for myDelegate'

                Clean All
                Run qmake from the build menu.
                Build all

                Should cure it

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                @mrjj
                It did. Thank you.

                1 Reply Last reply
                1

                • Login

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